Class: ScalarRubyTest::Runtime::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/amritk-scalar-test/runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:, response:, next_cursor: nil, fetch_next: nil) ⇒ Page

Returns a new instance of Page.



45
46
47
48
49
50
# File 'lib/amritk-scalar-test/runtime.rb', line 45

def initialize(items:, response:, next_cursor: nil, fetch_next: nil)
  @items = items
  @response = response
  @next_cursor = next_cursor
  @fetch_next = fetch_next
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



43
44
45
# File 'lib/amritk-scalar-test/runtime.rb', line 43

def items
  @items
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



43
44
45
# File 'lib/amritk-scalar-test/runtime.rb', line 43

def next_cursor
  @next_cursor
end

#responseObject (readonly)

Returns the value of attribute response.



43
44
45
# File 'lib/amritk-scalar-test/runtime.rb', line 43

def response
  @response
end

Instance Method Details

#auto_paging_eachObject



67
68
69
70
# File 'lib/amritk-scalar-test/runtime.rb', line 67

def auto_paging_each
  return enum_for(:auto_paging_each) unless block_given?
  each_page { |page| page.each { |item| yield item } }
end

#each(&block) ⇒ Object



52
53
54
55
# File 'lib/amritk-scalar-test/runtime.rb', line 52

def each(&block)
  return enum_for(:each) unless block
  @items.each(&block)
end

#each_pageObject



57
58
59
60
61
62
63
64
65
# File 'lib/amritk-scalar-test/runtime.rb', line 57

def each_page
  return enum_for(:each_page) unless block_given?
  page = self
  loop do
    yield page
    break unless page.has_next_page?
    page = page.next_page
  end
end

#has_next_page?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/amritk-scalar-test/runtime.rb', line 72

def has_next_page?
  !@next_cursor.nil? && !@fetch_next.nil?
end

#next_pageObject

Raises:

  • (RuntimeError)


76
77
78
79
# File 'lib/amritk-scalar-test/runtime.rb', line 76

def next_page
  raise RuntimeError, "No next page available" unless has_next_page?
  @fetch_next.call(@next_cursor)
end