Class: Mongoo::Embedded::HashProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoo/embedded/hash_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(doc, hash, klass) ⇒ HashProxy

Returns a new instance of HashProxy.


5
6
7
8
9
# File 'lib/mongoo/embedded/hash_proxy.rb', line 5

def initialize(doc, hash, klass)
  @doc   = doc
  @hash  = hash
  @klass = klass
end

Instance Method Details

#[](k) ⇒ Object


19
20
21
# File 'lib/mongoo/embedded/hash_proxy.rb', line 19

def [](k)
  build raw[k], k
end

#[]=(k, o) ⇒ Object


27
28
29
# File 'lib/mongoo/embedded/hash_proxy.rb', line 27

def []=(k,o)
  raw[k] = o.to_hash
end

#allObject


51
52
53
# File 'lib/mongoo/embedded/hash_proxy.rb', line 51

def all
  keys.collect { |k| self[k] }
end

#build(hash, k = nil) ⇒ Object


11
12
13
# File 'lib/mongoo/embedded/hash_proxy.rb', line 11

def build(hash, k=nil)
  @klass.new(@doc, hash, k)
end

#delete(k) ⇒ Object


23
24
25
# File 'lib/mongoo/embedded/hash_proxy.rb', line 23

def delete(k)
  raw.delete(k)
end

#eachObject


31
32
33
# File 'lib/mongoo/embedded/hash_proxy.rb', line 31

def each
  raw.each { |k,v| yield(k, build(v, k)) }
end

#firstObject


43
44
45
# File 'lib/mongoo/embedded/hash_proxy.rb', line 43

def first
  self[keys.first]
end

#keysObject


39
40
41
# File 'lib/mongoo/embedded/hash_proxy.rb', line 39

def keys
  raw.keys
end

#lastObject


47
48
49
# File 'lib/mongoo/embedded/hash_proxy.rb', line 47

def last
  self[keys.last]
end

#push(obj) ⇒ Object


55
56
57
58
# File 'lib/mongoo/embedded/hash_proxy.rb', line 55

def push(obj)
  k = BSON::ObjectId.new.to_s
  self[k] = obj; k
end

#rawObject


15
16
17
# File 'lib/mongoo/embedded/hash_proxy.rb', line 15

def raw
  @hash
end

#sizeObject


35
36
37
# File 'lib/mongoo/embedded/hash_proxy.rb', line 35

def size
  raw.size
end