Module: CuteKV::Document::InstanceMethods
- Defined in:
- lib/cute_kv/document.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #assigned_attributes ⇒ Object
- #assigned_attributes_values ⇒ Object
-
#destroy ⇒ Object
destroy self from database.
- #initialize(attributes = {}, new_id = true) {|_self| ... } ⇒ Object
-
#reload ⇒ Object
reload object from database.
-
#save ⇒ Object
we protect class’s backend from instance objects.
- #serialized_attributes ⇒ Object
-
#update(attributes = {}) ⇒ Object
update object’s attributes
attributes
are the attributes needed to be updated.
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
77 78 79 |
# File 'lib/cute_kv/document.rb', line 77 def id @id end |
Instance Method Details
#assigned_attributes ⇒ Object
106 107 108 |
# File 'lib/cute_kv/document.rb', line 106 def assigned_attributes self.class.assigned_attributes end |
#assigned_attributes_values ⇒ Object
128 129 130 |
# File 'lib/cute_kv/document.rb', line 128 def assigned_attributes_values self.assigned_attributes.inject({}){|h,attr| h[attr] = self.send attr; h } end |
#destroy ⇒ Object
destroy self from database
124 125 126 |
# File 'lib/cute_kv/document.rb', line 124 def destroy self.class.destroy(self) end |
#initialize(attributes = {}, new_id = true) {|_self| ... } ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cute_kv/document.rb', line 79 def initialize(attributes={}, new_id=true) if new_id @id = UUID.new.generate assigned_attrs = self.class.read_assigned_attributes_with_default_values attributes.each_key {|key| assigned_attrs.delete(key)} attributes.merge!(assigned_attrs) end attributes.each {|attr, value| self.send "#{attr}=", value if self.respond_to? "#{attr}=" } yield self if block_given? end |
#reload ⇒ Object
reload object from database
119 120 121 |
# File 'lib/cute_kv/document.rb', line 119 def reload self.class.reload(self) end |
#save ⇒ Object
we protect class’s backend from instance objects
91 92 93 |
# File 'lib/cute_kv/document.rb', line 91 def save self.class.save(self) end |
#serialized_attributes ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cute_kv/document.rb', line 95 def serialized_attributes seri_attrs = assigned_attributes.inject({}){|h,attr| h[attr] = self.send attr if self.respond_to? attr h } # #don't use :id, or you will get @id=nil when you use marshal to serialize object's attributes seri_attrs["id"] = @id self.class.serialize seri_attrs end |
#update(attributes = {}) ⇒ Object
update object’s attributes attributes
are the attributes needed to be updated
112 113 114 |
# File 'lib/cute_kv/document.rb', line 112 def update(attributes={}) self.class.update(self, attributes) end |