Module: Mingo::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Mingo
Defined in:
lib/mingo/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/mingo/persistence.rb', line 52

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

Returns:

  • (Boolean)


21
22
23
# File 'lib/mingo/persistence.rb', line 21

def persisted?
  !!id
end

#reload ⇒ Object



40
41
42
43
44
# File 'lib/mingo/persistence.rb', line 40

def reload
  doc = self.class.first(id, :transformer => nil)
  replace doc
  self
end

#save(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/mingo/persistence.rb', line 25

def save(options = {})
  if persisted?
    hash = values_for_update
    unless hash.empty?
      update(hash, options)
    end
  else
    self['_id'] = self.class.collection.insert(self.to_hash, options)
  end
end

#update(doc, options = {}) ⇒ Object



36
37
38
# File 'lib/mingo/persistence.rb', line 36

def update(doc, options = {})
  self.class.collection.update({'_id' => self.id}, doc, options)
end