Adds suppport for ActiveModel::Dirty and ::AttributeMethods
* ActiveModel::Dirty ** Basic support for dirty tracking ** It does not bubble up any changes to casted models currently * ActiveModel::AttributeMethods ** Attributes are now read and written through ActiveModel ** This also allows you to add your own attribute methods with prefix suffix and affix names. For more information check out ActiveModel::AttributeMethods::ClassMethods
This commit is contained in:
parent
5c21de8586
commit
d333133319
12 changed files with 333 additions and 124 deletions
|
@ -1,4 +1,5 @@
|
|||
# encoding: utf-8
|
||||
require 'set'
|
||||
module CouchRest
|
||||
module Model
|
||||
module Properties
|
||||
|
@ -22,16 +23,6 @@ module CouchRest
|
|||
self.class.properties
|
||||
end
|
||||
|
||||
def read_attribute(property)
|
||||
prop = find_property!(property)
|
||||
self[prop.to_s]
|
||||
end
|
||||
|
||||
def write_attribute(property, value)
|
||||
prop = find_property!(property)
|
||||
self[prop.to_s] = prop.cast(self, value)
|
||||
end
|
||||
|
||||
def apply_all_property_defaults
|
||||
return if self.respond_to?(:new?) && (new? == false)
|
||||
# TODO: cache the default object
|
||||
|
@ -94,8 +85,7 @@ module CouchRest
|
|||
type = [type] # inject as an array
|
||||
end
|
||||
property = Property.new(name, type, options)
|
||||
create_property_getter(property)
|
||||
create_property_setter(property) unless property.read_only == true
|
||||
create_property_alias(property) if property.alias
|
||||
if property.type_class.respond_to?(:validates_casted_model)
|
||||
validates_casted_model property.name
|
||||
end
|
||||
|
@ -103,49 +93,15 @@ module CouchRest
|
|||
property
|
||||
end
|
||||
|
||||
# defines the getter for the property (and optional aliases)
|
||||
def create_property_getter(property)
|
||||
# meth = property.name
|
||||
def create_property_alias(property)
|
||||
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
||||
def #{property.name}
|
||||
read_attribute('#{property.name}')
|
||||
def #{property.alias.to_s}
|
||||
#{property.name}
|
||||
end
|
||||
EOS
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
if property.alias
|
||||
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
||||
alias #{property.alias.to_sym} #{property.name.to_sym}
|
||||
EOS
|
||||
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
|
||||
|
||||
if property.alias
|
||||
class_eval <<-EOS
|
||||
alias #{property.alias.to_sym}= #{property_name.to_sym}=
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
end # module ClassMethods
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue