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



52
53
54
# File 'lib/mongoo/core.rb', line 52

def mongohash
  @mongohash
end

#persisted_mongohashObject



63
64
65
66
67
68
69
# File 'lib/mongoo/core.rb', line 63

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

#to_hashObject



71
72
73
# File 'lib/mongoo/core.rb', line 71

def to_hash
  mongohash.to_hash
end