Class: Pluggy::Lists::BaseList
- Inherits:
-
Object
- Object
- Pluggy::Lists::BaseList
- Includes:
- Enumerable
- Defined in:
- lib/pluggy/lists/base_list.rb
Overview
Shared iteration for all three of Pluggy's pagination shapes. Subclasses
differ only in more? and next_page.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#auto_paging_each(&block) ⇒ Object
Walks every page.
-
#auto_paging_to_a ⇒ Object
Every page, eagerly.
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(payload, klass:, requestor:, path:, filters: {}, client: nil) ⇒ BaseList
constructor
A new instance of BaseList.
- #inspect ⇒ Object
- #length ⇒ Object (also: #size, #count)
- #more? ⇒ Boolean
- #next_page ⇒ Object
Constructor Details
#initialize(payload, klass:, requestor:, path:, filters: {}, client: nil) ⇒ BaseList
Returns a new instance of BaseList.
12 13 14 15 16 17 18 19 20 |
# File 'lib/pluggy/lists/base_list.rb', line 12 def initialize(payload, klass:, requestor:, path:, filters: {}, client: nil) @raw = payload @klass = klass @requestor = requestor @path = path @filters = filters || {} @client = client @results = extract(payload).map { |item| build(item) } end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
10 11 12 |
# File 'lib/pluggy/lists/base_list.rb', line 10 def filters @filters end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/pluggy/lists/base_list.rb', line 10 def path @path end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
10 11 12 |
# File 'lib/pluggy/lists/base_list.rb', line 10 def raw @raw end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
10 11 12 |
# File 'lib/pluggy/lists/base_list.rb', line 10 def results @results end |
Instance Method Details
#auto_paging_each(&block) ⇒ Object
Walks every page. Returns an Enumerator when called without a block, so
.lazy, .first(n) and .to_a all work and only fetch the pages they
actually need.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pluggy/lists/base_list.rb', line 34 def auto_paging_each(&block) return enum_for(:auto_paging_each) unless block_given? page = self loop do page.each(&block) break unless page.more? page = page.next_page break if page.nil? || page.empty? end self end |
#auto_paging_to_a ⇒ Object
Every page, eagerly. Convenient, but unbounded -- prefer auto_paging_each for large histories.
50 |
# File 'lib/pluggy/lists/base_list.rb', line 50 def auto_paging_to_a = auto_paging_each.to_a |
#each ⇒ Object
22 |
# File 'lib/pluggy/lists/base_list.rb', line 22 def each(&) = @results.each(&) |
#empty? ⇒ Boolean
23 |
# File 'lib/pluggy/lists/base_list.rb', line 23 def empty? = @results.empty? |
#inspect ⇒ Object
52 53 54 |
# File 'lib/pluggy/lists/base_list.rb', line 52 def inspect "#<#{self.class.name} results=#{@results.length} more=#{more?}>" end |
#length ⇒ Object Also known as: size, count
24 |
# File 'lib/pluggy/lists/base_list.rb', line 24 def length = @results.length |
#more? ⇒ Boolean
28 |
# File 'lib/pluggy/lists/base_list.rb', line 28 def more? = false |
#next_page ⇒ Object
29 |
# File 'lib/pluggy/lists/base_list.rb', line 29 def next_page = nil |