Class: Cadenya::Page
- Inherits:
-
Object
- Object
- Cadenya::Page
- Includes:
- Enumerable
- Defined in:
- lib/cadenya/pagination.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#next_cursor ⇒ Object
readonly
Returns the value of attribute next_cursor.
Instance Method Summary collapse
- #each(&block) ⇒ Object (also: #auto_paging_each)
-
#initialize(items, next_cursor, &fetch) ⇒ Page
constructor
A new instance of Page.
-
#next_page ⇒ Object
The next page, or nil when this is the last one.
- #next_page? ⇒ Boolean
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
#items ⇒ Object (readonly)
Returns the value of attribute items.
10 11 12 |
# File 'lib/cadenya/pagination.rb', line 10 def items @items end |
#next_cursor ⇒ Object (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_page ⇒ Object
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
18 19 20 |
# File 'lib/cadenya/pagination.rb', line 18 def next_page? !@next_cursor.to_s.empty? end |