Class: Mongorilla::Cursor
- Inherits:
-
Object
- Object
- Mongorilla::Cursor
- Includes:
- Enumerable
- Defined in:
- lib/mongorilla/cursor.rb
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #count ⇒ Object
- #each ⇒ Object
-
#initialize(klass, cursor, col, cond, opt) ⇒ Cursor
constructor
A new instance of Cursor.
- #to_a ⇒ Object
- #to_json ⇒ Object
- #to_yaml ⇒ Object
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 |
#count ⇒ Object
37 38 39 |
# File 'lib/mongorilla/cursor.rb', line 37 def count @cursor.count end |
#each ⇒ Object
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_a ⇒ Object
18 19 20 |
# File 'lib/mongorilla/cursor.rb', line 18 def to_a @cursor.map{|c| @klass.new(c)} end |
#to_json ⇒ Object
27 28 29 |
# File 'lib/mongorilla/cursor.rb', line 27 def to_json to_a().map(&:to_hash).to_json end |
#to_yaml ⇒ Object
23 24 25 |
# File 'lib/mongorilla/cursor.rb', line 23 def to_yaml to_a().map(&:to_hash).to_yaml end |