Class: CouchPotato::Persistence::SimpleProperty
- Inherits:
-
Object
- Object
- CouchPotato::Persistence::SimpleProperty
- Defined in:
- lib/couch_potato/persistence/simple_property.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #build(object, json) ⇒ Object
- #destroy(object) ⇒ Object
- #dirty?(object) ⇒ Boolean
-
#initialize(owner_clazz, name, options = {}) ⇒ SimpleProperty
constructor
A new instance of SimpleProperty.
- #save(object) ⇒ Object
- #serialize(json, object) ⇒ Object
Constructor Details
#initialize(owner_clazz, name, options = {}) ⇒ SimpleProperty
Returns a new instance of SimpleProperty.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 6 def initialize(owner_clazz, name, = {}) self.name = name self.type = [:type] owner_clazz.class_eval do attr_reader name, "#{name}_was" def initialize(attributes = {}) super attributes attributes.each do |name, value| self.instance_variable_set("@#{name}_was", value) end if attributes end define_method "#{name}=" do |value| self.instance_variable_set("@#{name}", value) end define_method "#{name}?" do !self.send(name).nil? && !self.send(name).try(:blank?) end define_method "#{name}_changed?" do !self.instance_variable_get("@#{name}_not_changed") && self.send(name) != self.send("#{name}_was") end define_method "#{name}_not_changed" do self.instance_variable_set("@#{name}_not_changed", true) end end end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 4 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 4 def type @type end |
Instance Method Details
#build(object, json) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 37 def build(object, json) value = json[name.to_s] || json[name.to_sym] typecasted_value = if type type.json_create value else value end object.send "#{name}=", typecasted_value end |
#destroy(object) ⇒ Object
55 56 57 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 55 def destroy(object) end |
#dirty?(object) ⇒ Boolean
47 48 49 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 47 def dirty?(object) object.send("#{name}_changed?") end |
#save(object) ⇒ Object
51 52 53 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 51 def save(object) end |
#serialize(json, object) ⇒ Object
59 60 61 |
# File 'lib/couch_potato/persistence/simple_property.rb', line 59 def serialize(json, object) json[name] = object.send name end |