Class: Mongoo::Embedded::ArrayProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(doc, array, klass) ⇒ ArrayProxy

Returns a new instance of ArrayProxy.



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

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

Instance Method Details

#<<(obj) ⇒ Object



62
63
64
# File 'lib/mongoo/embedded/array_proxy.rb', line 62

def <<(obj)
  push(obj.to_hash)
end

#[](i) ⇒ Object



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

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

#[]=(i, o) ⇒ Object



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

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

#allObject Also known as: to_a



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

def all
  raw.each_with_index { |v,i| build(v, i) }
end

#build(hash, i = nil) ⇒ Object



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

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

#delete_at(i) ⇒ Object



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

def delete_at(i)
  raw.delete_at(i)
end

#eachObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mongoo/embedded/array_proxy.rb', line 66

def empty?
  raw.empty?
end

#firstObject



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

def first
  build raw.first, 0
end

#keysObject



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

def keys
  (0..size-1).to_a
end

#lastObject



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

def last
  build raw.last, -1
end

#push(obj) ⇒ Object



58
59
60
# File 'lib/mongoo/embedded/array_proxy.rb', line 58

def push(obj)
  raw << obj.to_hash; raw.index(obj.to_hash)
end

#rawObject



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

def raw
  @array
end

#sizeObject



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

def size
  raw.size
end