lib/couchrest/model/properties.rb in couchrest_model-1.1.0.rc1 vs lib/couchrest/model/properties.rb in couchrest_model-1.1.0

- old
+ new

@@ -147,31 +147,29 @@ # on the document whenever saving occurs. # # These properties are casted as Time objects, so they should always # be set to UTC. def timestamps! - class_eval <<-EOS, __FILE__, __LINE__ - property(:updated_at, Time, :read_only => true, :protected => true, :auto_validation => false) - property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false) + property(:updated_at, Time, :read_only => true, :protected => true, :auto_validation => false) + property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false) - set_callback :save, :before do |object| - write_attribute('updated_at', Time.now) - write_attribute('created_at', Time.now) if object.new? - end - EOS + set_callback :save, :before do |object| + write_attribute('updated_at', Time.now) + write_attribute('created_at', Time.now) if object.new? + end end protected # This is not a thread safe operation, if you have to set new properties at runtime # make sure a mutex is used. def define_property(name, options={}, &block) # check if this property is going to casted type = options.delete(:type) || options.delete(:cast_as) if block_given? - type = Class.new(Hash) do - include CastedModel + type = Class.new do + include Embeddable end if block.arity == 1 # Traditional, with options type.class_eval { yield type } else type.instance_exec(&block) @@ -189,45 +187,35 @@ property end # defines the getter for the property (and optional aliases) def create_property_getter(property) - # meth = property.name - class_eval <<-EOS, __FILE__, __LINE__ + 1 - def #{property.name} - read_attribute('#{property.name}') - end - EOS + define_method(property.name) do + read_attribute(property.name) + end if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase) - class_eval <<-EOS, __FILE__, __LINE__ - def #{property.name}? - value = read_attribute('#{property.name}') - !(value.nil? || value == false) - end - EOS + define_method("#{property.name}?") do + value = read_attribute(property.name) + !(value.nil? || value == false) + end end if property.alias - class_eval <<-EOS, __FILE__, __LINE__ + 1 - alias #{property.alias.to_sym} #{property.name.to_sym} - EOS + alias_method(property.alias, property.name.to_sym) end end # defines the setter for the property (and optional aliases) def create_property_setter(property) - property_name = property.name - class_eval <<-EOS - def #{property_name}=(value) - write_attribute('#{property_name}', value) - end - EOS + name = property.name + define_method("#{name}=") do |value| + write_attribute(name, value) + end + if property.alias - class_eval <<-EOS - alias #{property.alias.to_sym}= #{property_name.to_sym}= - EOS + alias_method "#{property.alias}=", "#{name}=" end end end # module ClassMethods