Module: Norman::Model::InstanceMethods
- Defined in:
- lib/norman/model.rb
Instance Method Summary collapse
-
#<=>(instance) ⇒ Object
Norman models implement the <=> method and mix in Comparable to provide sorting methods.
-
#delete ⇒ Object
Tell the mapper to delete the data for this instance.
-
#id_changed? ⇒ Boolean
Returns true is the model’s id field has been updated.
-
#initialize(attributes = nil) {|_self| ... } ⇒ Object
Norman models can be instantiated with a hash of attribures, a block, or both.
-
#save ⇒ Object
Tell the mapper to save the data for this model instance.
-
#to_hash ⇒ Object
Get a hash of the instance’s model attributes.
-
#to_id(use_old = false) ⇒ Object
Invoke the model’s id method to return this instance’s unique key.
-
#update(attributes) ⇒ Object
Update this instance’s attributes and invoke #save.
Instance Method Details
#<=>(instance) ⇒ Object
Norman models implement the <=> method and mix in Comparable to provide sorting methods. This default implementation compares the result of #to_id. If the items being compared are not of the same kind.
120 121 122 |
# File 'lib/norman/model.rb', line 120 def <=>(instance) to_id <=> instance.to_id if instance.kind_of? self.class end |
#delete ⇒ Object
Tell the mapper to delete the data for this instance.
159 160 161 |
# File 'lib/norman/model.rb', line 159 def delete self.class.delete(self.to_id) end |
#id_changed? ⇒ Boolean
Returns true is the model’s id field has been updated.
132 133 134 |
# File 'lib/norman/model.rb', line 132 def id_changed? to_id != @attributes[self.class.id_method] end |
#initialize(attributes = nil) {|_self| ... } ⇒ Object
Norman models can be instantiated with a hash of attribures, a block, or both. If both a hash and block are given, then the values set inside the block will take precedence over those set in the hash.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/norman/model.rb', line 105 def initialize(attributes = nil, &block) @attributes = {}.freeze return unless attributes || block_given? if attributes self.class.attribute_names.each do |name| value = attributes[name] || attributes[name.to_s] send("#{name}=", value) if value end end yield self if block_given? end |
#save ⇒ Object
Tell the mapper to save the data for this model instance.
145 146 147 |
# File 'lib/norman/model.rb', line 145 def save self.class.mapper.put(self) end |
#to_hash ⇒ Object
Get a hash of the instance’s model attributes.
125 126 127 128 129 |
# File 'lib/norman/model.rb', line 125 def to_hash self.class.attribute_names.inject({}) do |hash, key| hash[key] = self.send(key); hash end end |
#to_id(use_old = false) ⇒ Object
Invoke the model’s id method to return this instance’s unique key. If true is passed, then the id will be read from the attributes hash rather than from an instance variable. This allows you to retrieve the old id, in the event that the id has been changed.
140 141 142 |
# File 'lib/norman/model.rb', line 140 def to_id(use_old = false) use_old ? @attributes[self.class.id_method] : send(self.class.id_method) end |
#update(attributes) ⇒ Object
Update this instance’s attributes and invoke #save.
150 151 152 153 154 155 156 |
# File 'lib/norman/model.rb', line 150 def update(attributes) self.class.attribute_names.each do |name| value = attributes[name] || attributes[name.to_s] send("#{name}=", value) if value end save end |