Class: Helium::Cursor

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cursor

Returns a new instance of Cursor



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

def initialize(opts = {})
  @client = opts.fetch(:client)
  @path   = opts.fetch(:path)
  @klass  = opts.fetch(:klass)
  @params = opts.fetch(:params, {})

  @collection = []
  @next_link  = nil
  @is_last    = false
end

Instance Method Details

#each(start = 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/helium/cursor.rb', line 16

def each(start = 0)
  return to_enum(:each, start) unless block_given?

  Array(@collection[start..-1]).each do |element|
    yield(element)
  end

  unless last?
    start = [@collection.size, start].max

    fetch_next_page

    each(start, &Proc.new)
  end
end

#to_json(*options) ⇒ Object



32
33
34
# File 'lib/helium/cursor.rb', line 32

def to_json(*options)
  self.map(&:as_json).to_json(*options)
end