Module: Mingo::Persistence
Defined Under Namespace
Modules: ClassMethods
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
45 46 47 48 49 |
# File 'lib/mingo/persistence.rb', line 45 def destroy self.class.collection.remove('_id' => self.id) @destroyed = true self.freeze end |
#destroyed? ⇒ Boolean
51 52 53 |
# File 'lib/mingo/persistence.rb', line 51 def destroyed? @destroyed end |
#initialize(*args) ⇒ Object
16 17 18 19 |
# File 'lib/mingo/persistence.rb', line 16 def initialize(*args) @destroyed = false super end |
#persisted? ⇒ Boolean
21 22 23 |
# File 'lib/mingo/persistence.rb', line 21 def persisted? !!id end |
#reload ⇒ Object
40 41 42 43 |
# File 'lib/mingo/persistence.rb', line 40 def reload doc = self.class.first(id, :transformer => nil) replace doc end |
#save(options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mingo/persistence.rb', line 25 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
36 37 38 |
# File 'lib/mingo/persistence.rb', line 36 def update(doc, = {}) self.class.collection.update({'_id' => self.id}, doc, ) end |