Module: Perry::Persistence::InstanceMethods
- Defined in:
- lib/perry/persistence.rb
Instance Method Summary collapse
- #[]=(attribute, value) ⇒ Object
- #attributes=(attributes) ⇒ Object
- #destroy ⇒ Object (also: #delete)
- #destroy! ⇒ Object (also: #delete!)
-
#freeze ⇒ Object
Calls Object#freeze on the model and on the attributes hash in addition to calling #freeze! to prevent the model from being saved or destroyed.
-
#freeze! ⇒ Object
Prevents the model from being saved or destroyed in the future while still allowing the model and its attributes hash to be modified.
-
#frozen? ⇒ Boolean
Returns true if ether #freeze or #freeze! has been called on the model.
- #reload ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
- #update_attributes(attributes) ⇒ Object
- #update_attributes!(attributes) ⇒ Object
Instance Method Details
#[]=(attribute, value) ⇒ Object
20 21 22 |
# File 'lib/perry/persistence.rb', line 20 def []=(attribute, value) set_attribute(attribute, value) end |
#attributes=(attributes) ⇒ Object
24 25 26 |
# File 'lib/perry/persistence.rb', line 24 def attributes=(attributes) set_attributes(attributes) end |
#destroy ⇒ Object Also known as: delete
46 47 48 49 50 51 |
# File 'lib/perry/persistence.rb', line 46 def destroy raise Perry::PerryError.new("cannot destroy a frozen object") if frozen? unless self.new_record? || self[primary_key].nil? write_adapter.call(:delete, :object => self).success end end |
#destroy! ⇒ Object Also known as: delete!
54 55 56 |
# File 'lib/perry/persistence.rb', line 54 def destroy! destroy or raise Perry::RecordNotSaved end |
#freeze ⇒ Object
Calls Object#freeze on the model and on the attributes hash in addition to calling #freeze! to prevent the model from being saved or destroyed.
66 67 68 69 70 |
# File 'lib/perry/persistence.rb', line 66 def freeze freeze! attributes.freeze super end |
#freeze! ⇒ Object
Prevents the model from being saved or destroyed in the future while still allowing the model and its attributes hash to be modified.
75 76 77 |
# File 'lib/perry/persistence.rb', line 75 def freeze! @frozen = true end |
#frozen? ⇒ Boolean
Returns true if ether #freeze or #freeze! has been called on the model.
81 82 83 |
# File 'lib/perry/persistence.rb', line 81 def frozen? !!@frozen end |
#reload ⇒ Object
59 60 61 |
# File 'lib/perry/persistence.rb', line 59 def reload self.attributes = self.class.where(primary_key => self.send(primary_key)).first.attributes end |
#save ⇒ Object
28 29 30 31 |
# File 'lib/perry/persistence.rb', line 28 def save raise Perry::PerryError.new("cannot write a frozen object") if frozen? write_adapter.call(:write, :object => self).success end |
#save! ⇒ Object
33 34 35 |
# File 'lib/perry/persistence.rb', line 33 def save! save or raise Perry::RecordNotSaved end |
#update_attributes(attributes) ⇒ Object
37 38 39 40 |
# File 'lib/perry/persistence.rb', line 37 def update_attributes(attributes) self.attributes = attributes save end |
#update_attributes!(attributes) ⇒ Object
42 43 44 |
# File 'lib/perry/persistence.rb', line 42 def update_attributes!(attributes) update_attributes(attributes) or raise Perry::RecordNotSaved end |