Class: Cadenya::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cadenya/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, next_cursor, &fetch) ⇒ Page

Returns a new instance of Page.



12
13
14
15
16
# File 'lib/cadenya/pagination.rb', line 12

def initialize(items, next_cursor, &fetch)
  @items = items
  @next_cursor = next_cursor
  @fetch = fetch
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



10
11
12
# File 'lib/cadenya/pagination.rb', line 10

def items
  @items
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



10
11
12
# File 'lib/cadenya/pagination.rb', line 10

def next_cursor
  @next_cursor
end

Instance Method Details

#each(&block) ⇒ Object Also known as: auto_paging_each



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cadenya/pagination.rb', line 29

def each(&block)
  return enum_for(:each) unless block_given?

  page = self
  until page.nil?
    page.items.each(&block)
    page = page.next_page
  end
  # Ruby collection iterators return the receiver in block form.
  self
end

#next_pageObject

The next page, or nil when this is the last one.



23
24
25
26
27
# File 'lib/cadenya/pagination.rb', line 23

def next_page
  return nil unless next_page?

  @fetch.call(@next_cursor)
end

#next_page?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cadenya/pagination.rb', line 18

def next_page?
  !@next_cursor.to_s.empty?
end