Module: Mingo::Persistence::InstanceMethods
- Defined in:
- lib/mingo/persistence.rb
Instance Method Summary collapse
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #initialize(*args) ⇒ Object
- #persisted? ⇒ Boolean
- #reload ⇒ Object
- #save(options = {}) ⇒ Object
- #update(doc, options = {}) ⇒ Object
Instance Method Details
#destroy ⇒ Object
46 47 48 49 50 |
# File 'lib/mingo/persistence.rb', line 46 def destroy self.class.collection.remove('_id' => self.id) @destroyed = true self.freeze end |
#destroyed? ⇒ Boolean
52 53 54 |
# File 'lib/mingo/persistence.rb', line 52 def destroyed? @destroyed end |
#initialize(*args) ⇒ Object
17 18 19 20 |
# File 'lib/mingo/persistence.rb', line 17 def initialize(*args) @destroyed = false super end |
#persisted? ⇒ Boolean
22 23 24 |
# File 'lib/mingo/persistence.rb', line 22 def persisted? !!id end |
#reload ⇒ Object
41 42 43 44 |
# File 'lib/mingo/persistence.rb', line 41 def reload doc = self.class.first(id, :transformer => nil) replace doc end |
#save(options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mingo/persistence.rb', line 26 def save( = {}) if persisted? hash = values_for_update unless %w[$set $unset] == hash.keys && hash.values.all? { |v| v.empty? } update(hash, ) end else self['_id'] = self.class.collection.insert(self.to_hash, ) end end |
#update(doc, options = {}) ⇒ Object
37 38 39 |
# File 'lib/mingo/persistence.rb', line 37 def update(doc, = {}) self.class.collection.update({'_id' => self.id}, doc, ) end |