Class: SimplyCouch::Model::Persistence::SimpleProperty

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_couch/model/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_clazz, name, options = {}) ⇒ SimpleProperty

Returns a new instance of SimpleProperty.



152
153
154
155
156
157
158
159
# File 'lib/simply_couch/model/persistence.rb', line 152

def initialize(owner_clazz, name, options = {})
  self.name = name
  @setter_name = "#{name}="
  self.type = options[:type]
  @type_caster = TypeCaster.new
  owner_clazz.send(:include, PropertyMethods) unless owner_clazz.ancestors.include?(PropertyMethods)
  define_accessors(accessors_module_for(owner_clazz), name, options)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



150
151
152
# File 'lib/simply_couch/model/persistence.rb', line 150

def name
  @name
end

#typeObject

Returns the value of attribute type.



150
151
152
# File 'lib/simply_couch/model/persistence.rb', line 150

def type
  @type
end

Instance Method Details

#build(object, json) ⇒ Object



161
# File 'lib/simply_couch/model/persistence.rb', line 161

def build(object, json); object.public_send @setter_name, json[name]; end

#changed?(object) ⇒ Boolean

Returns:

  • (Boolean)


162
# File 'lib/simply_couch/model/persistence.rb', line 162

def changed?(object); object.public_send("#{name}_changed?"); end

#serialize(json, object) ⇒ Object Also known as: value



163
# File 'lib/simply_couch/model/persistence.rb', line 163

def serialize(json, object); json[name] = @type_caster.cast_back object.public_send(name); end