Module: Zeng::Document::InstanceMethods
- Defined in:
- lib/zeng/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.
72 73 74 |
# File 'lib/zeng/document.rb', line 72 def id @id end |
Instance Method Details
#assigned_attributes ⇒ Object
101 102 103 |
# File 'lib/zeng/document.rb', line 101 def assigned_attributes self.class.assigned_attributes end |
#assigned_attributes_values ⇒ Object
123 124 125 |
# File 'lib/zeng/document.rb', line 123 def assigned_attributes_values self.assigned_attributes.inject({}){|h,attr| h[attr] = self.send attr; h } end |
#destroy ⇒ Object
destroy self from database
119 120 121 |
# File 'lib/zeng/document.rb', line 119 def destroy self.class.destroy(self) end |
#initialize(attributes = {}, new_id = true) {|_self| ... } ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/zeng/document.rb', line 74 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
114 115 116 |
# File 'lib/zeng/document.rb', line 114 def reload self.class.reload(self) end |
#save ⇒ Object
we protect class’s backend from instance objects
86 87 88 |
# File 'lib/zeng/document.rb', line 86 def save self.class.save(self) end |
#serialized_attributes ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/zeng/document.rb', line 90 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
107 108 109 |
# File 'lib/zeng/document.rb', line 107 def update(attributes={}) self.class.update(self, attributes) end |