Class: Mongorilla::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongorilla/cursor.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, cursor, col, cond, opt) ⇒ Cursor

Returns a new instance of Cursor.



41
42
43
44
45
46
47
48
# File 'lib/mongorilla/cursor.rb', line 41

def initialize(klass,cursor,col,cond,opt)
  @members = {}
  @klass = klass
  @cursor = cursor
  @col = col
  @cond = cond
  @opt = opt
end

Instance Method Details

#[](idx) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mongorilla/cursor.rb', line 5

def [](idx)
  if idx < 0
    idx = @cursor.count + idx
    return nil if idx < 0
  else
    return nil if idx >= @cursor.count
  end
  return @members[idx] if @members[idx]
  ret = @cursor.skip(idx).limit(1).first
  @cursor = @col.find(@cond,@opt)
  @members[idx] = ret ? @klass.new(ret) : nil
end

#countObject



37
38
39
# File 'lib/mongorilla/cursor.rb', line 37

def count
  @cursor.count
end

#eachObject



31
32
33
34
35
# File 'lib/mongorilla/cursor.rb', line 31

def each
  @cursor.each do|v|
    yield @klass.new(v)
  end
end

#to_aObject



18
19
20
# File 'lib/mongorilla/cursor.rb', line 18

def to_a
  @cursor.map{|c| @klass.new(c)}
end

#to_jsonObject



27
28
29
# File 'lib/mongorilla/cursor.rb', line 27

def to_json
  to_a().map(&:to_hash).to_json
end

#to_yamlObject



23
24
25
# File 'lib/mongorilla/cursor.rb', line 23

def to_yaml
  to_a().map(&:to_hash).to_yaml
end