Module: ActivePStore::Persistence
- Included in:
- Base
- Defined in:
- lib/active_pstore/persistence.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #destroy ⇒ Object
- #new_record? ⇒ Boolean
- #save ⇒ Object
- #update(attrs) ⇒ Object (also: #update_attributes)
- #update_attribute(attr_name, attr_value) ⇒ Object
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/active_pstore/persistence.rb', line 5 def id @id end |
Instance Method Details
#destroy ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/active_pstore/persistence.rb', line 7 def destroy ActivePStore::Base.use_connection do |connection| connection[self.class.pstore_key].delete_if {|obj| obj.id == self.id } end self end |
#new_record? ⇒ Boolean
15 16 17 |
# File 'lib/active_pstore/persistence.rb', line 15 def new_record? @id.nil? end |
#save ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_pstore/persistence.rb', line 19 def save ActivePStore::Base.use_connection do |connection| if new_record? @id = SecureRandom.hex if connection[self.class.pstore_key] connection[self.class.pstore_key] << self else connection[self.class.pstore_key] = [self] end else connection[self.class.pstore_key].map! {|obj| obj.id == self.id ? self : obj } end end true end |
#update(attrs) ⇒ Object Also known as: update_attributes
43 44 45 46 47 48 49 |
# File 'lib/active_pstore/persistence.rb', line 43 def update(attrs) attrs.each do |attr_name, attr_value| self.__send__("#{attr_name}=", attr_value) end save end |
#update_attribute(attr_name, attr_value) ⇒ Object
37 38 39 40 41 |
# File 'lib/active_pstore/persistence.rb', line 37 def update_attribute(attr_name, attr_value) self.__send__("#{attr_name}=", attr_value) save end |