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}
|
2011-10-03 13:30:49 -04:00
|
|
|
when true,'true'
|
2010-06-18 17:02:16 -04:00
|
|
|
true
|
2011-10-03 13:30:49 -04:00
|
|
|
when false,'false'
|
2010-06-18 17:02:16 -04:00
|
|
|
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)
|
2011-09-27 05:26:54 -04:00
|
|
|
if new_data.has_key?(:'#{squash}')
|
|
|
|
attributes[:#{name}] = new_data[:'#{squash}']
|
2011-07-20 18:18:05 -04:00
|
|
|
elsif new_data.has_key?("#{squash}")
|
|
|
|
attributes[:#{name}] = 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
|
|
|
|
|
2011-04-26 18:15:28 -04:00
|
|
|
def dup
|
|
|
|
copy = super
|
2011-04-28 13:17:06 -04:00
|
|
|
copy.dup_attributes!
|
2011-04-26 18:15:28 -04:00
|
|
|
copy
|
|
|
|
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)
|
2011-10-11 05:11:14 -04:00
|
|
|
elsif self.respond_to?("#{key}=",true)
|
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)
|
2011-06-28 18:26:27 -04:00
|
|
|
missing = missing_attributes(args)
|
|
|
|
if missing.length == 1
|
|
|
|
raise(ArgumentError, "#{missing.first} is required for this operation")
|
|
|
|
elsif missing.any?
|
|
|
|
raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def requires_one(*args)
|
|
|
|
missing = missing_attributes(args)
|
|
|
|
if missing.length == args.length
|
|
|
|
raise(ArgumentError, "#{missing[0...-1].join(", ")} or #{missing[-1]} are required for this operation")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def missing_attributes(args)
|
2010-06-07 21:22:31 -04:00
|
|
|
missing = []
|
|
|
|
for arg in [:connection] | args
|
2011-03-09 20:03:15 -05:00
|
|
|
unless send("#{arg}") || attributes.has_key?(arg)
|
|
|
|
missing << arg
|
|
|
|
end
|
2010-06-07 21:22:31 -04:00
|
|
|
end
|
2011-06-28 18:26:27 -04:00
|
|
|
missing
|
2010-06-07 11:59:17 -04:00
|
|
|
end
|
|
|
|
|
2011-04-28 13:17:06 -04:00
|
|
|
def dup_attributes!
|
|
|
|
@attributes = @attributes.dup
|
|
|
|
end
|
2011-04-26 18:15:28 -04:00
|
|
|
|
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
|