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

#destroyObject



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

Returns:

  • (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

Returns:

  • (Boolean)


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

def persisted?
  !!id
end

#reloadObject



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(options = {})
  if persisted?
    hash = values_for_update
    unless %w[$set $unset] == hash.keys && hash.values.all? { |v| v.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