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



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

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

#[]=(k, o) ⇒ Object



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

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

#allObject



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

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

#build(hash, k = nil) ⇒ Object



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

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

#delete(k) ⇒ Object



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

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

#eachObject



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

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

#firstObject



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

def first
  self[keys.first]
end

#keysObject



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

def keys
  raw.keys
end

#lastObject



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

def last
  self[keys.last]
end

#push(obj) ⇒ Object



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

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

#rawObject



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

def raw
  @hash
end

#sizeObject



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

def size
  raw.size
end