2010-06-07 21:22:31 -04:00
|
|
|
module Fog
|
|
|
|
module Attributes
|
|
|
|
module ClassMethods
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def _load(marshalled)
|
|
|
|
new(Marshal.load(marshalled))
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def aliases
|
|
|
|
@aliases ||= {}
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def attributes
|
|
|
|
@attributes ||= []
|
|
|
|
end
|
|
|
|
|
2010-06-18 17:02:16 -04:00
|
|
|
def attribute(name, options = {})
|
2010-06-07 21:22:31 -04:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
2010-11-19 16:45:45 -05:00
|
|
|
def #{name}
|
|
|
|
attributes[:#{name}]
|
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
EOS
|
2010-06-18 17:02:16 -04:00
|
|
|
case options[:type]
|
|
|
|
when :boolean
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name}=(new_#{name})
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = case new_#{name}
|
2010-06-18 17:02:16 -04:00
|
|
|
when 'true'
|
|
|
|
true
|
|
|
|
when 'false'
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
when :float
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name}=(new_#{name})
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = new_#{name}.to_f
|
2010-06-18 17:02:16 -04:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
when :integer
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name}=(new_#{name})
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = new_#{name}.to_i
|
2010-06-18 17:02:16 -04:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
when :string
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name}=(new_#{name})
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = new_#{name}.to_s
|
2010-06-18 17:02:16 -04:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
when :time
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name}=(new_#{name})
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = if new_#{name}.nil? || new_#{name} == "" || new_#{name}.is_a?(Time)
|
|
|
|
new_#{name}
|
2010-08-13 22:49:30 -04:00
|
|
|
else
|
2010-11-19 16:45:45 -05:00
|
|
|
Time.parse(new_#{name})
|
2010-06-18 21:42:59 -04:00
|
|
|
end
|
2010-06-18 17:02:16 -04:00
|
|
|
end
|
|
|
|
EOS
|
2010-06-17 19:58:09 -04:00
|
|
|
when :array
|
2010-06-18 17:02:16 -04:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
2010-06-17 19:58:09 -04:00
|
|
|
def #{name}=(new_#{name})
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = [*new_#{name}]
|
2010-06-17 19:58:09 -04:00
|
|
|
end
|
2010-06-18 17:02:16 -04:00
|
|
|
EOS
|
2010-06-17 19:58:09 -04:00
|
|
|
else
|
|
|
|
if squash = options[:squash]
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def #{name}=(new_data)
|
|
|
|
if new_data.is_a?(Hash)
|
2010-09-08 11:09:21 -04:00
|
|
|
if new_data[:#{squash}] || new_data["#{squash}"]
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = new_data[:#{squash}] || new_data["#{squash}"]
|
2010-06-17 19:58:09 -04:00
|
|
|
else
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = [ new_data ]
|
2010-06-17 19:58:09 -04:00
|
|
|
end
|
|
|
|
else
|
2010-11-19 16:45:45 -05:00
|
|
|
attributes[:#{name}] = new_data
|
2010-06-17 19:58:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
else
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__
|
2010-11-19 16:45:45 -05:00
|
|
|
def #{name}=(new_#{name})
|
|
|
|
attributes[:#{name}] = new_#{name}
|
|
|
|
end
|
2010-06-17 19:58:09 -04:00
|
|
|
EOS
|
|
|
|
end
|
2010-06-18 17:02:16 -04:00
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
@attributes ||= []
|
|
|
|
@attributes |= [name]
|
2010-06-18 17:02:16 -04:00
|
|
|
for new_alias in [*options[:aliases]]
|
|
|
|
aliases[new_alias] = name
|
2010-06-07 21:22:31 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-09-07 14:30:02 -04:00
|
|
|
def identity(name, options = {})
|
2010-06-07 21:22:31 -04:00
|
|
|
@identity = name
|
2010-09-07 14:30:02 -04:00
|
|
|
self.attribute(name, options)
|
2010-06-17 19:58:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ignore_attributes(*args)
|
|
|
|
@ignored_attributes = args
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_attributes
|
|
|
|
@ignored_attributes ||= []
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
module InstanceMethods
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2011-02-15 14:26:04 -05:00
|
|
|
def _dump(level)
|
2010-06-07 21:22:31 -04:00
|
|
|
Marshal.dump(attributes)
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def attributes
|
2010-11-19 16:45:45 -05:00
|
|
|
@attributes ||= {}
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def identity
|
|
|
|
send(self.class.instance_variable_get('@identity'))
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def identity=(new_identity)
|
|
|
|
send("#{self.class.instance_variable_get('@identity')}=", new_identity)
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def merge_attributes(new_attributes = {})
|
|
|
|
for key, value in new_attributes
|
2010-06-17 19:58:09 -04:00
|
|
|
unless self.class.ignored_attributes.include?(key)
|
2010-12-16 14:25:31 -05:00
|
|
|
if aliased_key = self.class.aliases[key]
|
2010-06-17 19:58:09 -04:00
|
|
|
send("#{aliased_key}=", value)
|
2010-11-03 20:32:06 -04:00
|
|
|
elsif (public_methods | private_methods).detect {|method| ["#{key}=", :"#{key}="].include?(method)}
|
2010-06-17 19:58:09 -04:00
|
|
|
send("#{key}=", value)
|
2010-10-27 19:16:22 -04:00
|
|
|
else
|
2010-12-16 14:25:31 -05:00
|
|
|
attributes[key] = value
|
2010-06-17 19:58:09 -04:00
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
self
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def new_record?
|
|
|
|
!identity
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
|
2010-11-21 05:56:41 -05:00
|
|
|
# check that the attributes specified in args exist and is not nil
|
2010-06-07 21:22:31 -04:00
|
|
|
def requires(*args)
|
|
|
|
missing = []
|
|
|
|
for arg in [:connection] | args
|
|
|
|
missing << arg unless send("#{arg}")
|
|
|
|
end
|
|
|
|
unless missing.empty?
|
|
|
|
if missing.length == 1
|
|
|
|
raise(ArgumentError, "#{missing.first} is required for this operation")
|
|
|
|
else
|
|
|
|
raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
private
|
2010-06-07 11:59:17 -04:00
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
def remap_attributes(attributes, mapping)
|
|
|
|
for key, value in mapping
|
|
|
|
if attributes.key?(key)
|
|
|
|
attributes[value] = attributes.delete(key)
|
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-07 21:22:31 -04:00
|
|
|
end
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
end
|