Class: Mongoo::Core

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Validations, Attributes, Embedded::CoreMixin
Defined in:
lib/mongoo/core.rb

Direct Known Subclasses

Base, Embedded::Base

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, persisted = false) ⇒ Core

Returns a new instance of Core.


13
14
15
16
17
# File 'lib/mongoo/core.rb', line 13

def initialize(hash={}, persisted=false)
  @persisted = persisted
  init_from_hash(hash)
  set_persisted_mongohash((persisted? ? mongohash : nil))
end

Class Method Details

.describe(&block) ⇒ Object


9
10
11
# File 'lib/mongoo/core.rb', line 9

def self.describe(&block)
  Mongoo::DescribeDsl.new(self).describe(&block)
end

Instance Method Details

#==(val) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/mongoo/core.rb', line 19

def ==(val)
  if val.class.to_s == self.class.to_s
    if val.persisted?
      val.id == self.id
    else
      self.mongohash.raw_hash == val.mongohash.raw_hash
    end
  end
end

#merge!(hash) ⇒ Object


29
30
31
32
33
34
35
36
37
# File 'lib/mongoo/core.rb', line 29

def merge!(hash)
  if hash.is_a?(Mongoo::Mongohash)
    hash = hash.raw_hash
  end
  hash.deep_stringify_keys!
  hash = mongohash.raw_hash.deep_merge(hash)
  set_mongohash( Mongoo::Mongohash.new(hash) )
  mongohash
end

#mongohashObject


57
58
59
# File 'lib/mongoo/core.rb', line 57

def mongohash
  @mongohash
end

#persisted_mongohashObject


68
69
70
71
72
73
74
# File 'lib/mongoo/core.rb', line 68

def persisted_mongohash
  @persisted_mongohash ||= begin
    if @serialized_persisted_mongohash
      Marshal.load(@serialized_persisted_mongohash)
    end
  end
end

#reset_persisted_mongohashObject


47
48
49
50
# File 'lib/mongoo/core.rb', line 47

def reset_persisted_mongohash
  @persisted = true
  set_persisted_mongohash(mongohash)
end

#to_hashObject


76
77
78
# File 'lib/mongoo/core.rb', line 76

def to_hash
  mongohash.to_hash
end