Class: Autorender::Internal::PagePagination
- Inherits:
-
Object
- Object
- Autorender::Internal::PagePagination
- Includes:
- Type::BasePage
- Defined in:
- lib/autorender/internal/page_pagination.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary collapse
- #auto_paging_each(&blk) {|| ... } ⇒ Object
-
#initialize(client:, req:, headers:, page_data:) ⇒ PagePagination
constructor
private
A new instance of PagePagination.
- #inspect ⇒ String private
- #next_page ⇒ self
- #next_page? ⇒ Boolean
Methods included from Type::BasePage
Constructor Details
#initialize(client:, req:, headers:, page_data:) ⇒ PagePagination
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of PagePagination.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/autorender/internal/page_pagination.rb', line 71 def initialize(client:, req:, headers:, page_data:) super case page_data in {data: Array => data} @data = data.map { Autorender::Internal::Type::Converter.coerce(@model, _1) } else end @current_page = page_data[:current_page] @has_next = page_data[:has_next] @total_results = page_data[:total_results] end |
Instance Attribute Details
#current_page ⇒ Integer
23 24 25 |
# File 'lib/autorender/internal/page_pagination.rb', line 23 def current_page @current_page end |
#data ⇒ Array<generic<Elem>>?
20 21 22 |
# File 'lib/autorender/internal/page_pagination.rb', line 20 def data @data end |
#has_next ⇒ Boolean
26 27 28 |
# File 'lib/autorender/internal/page_pagination.rb', line 26 def has_next @has_next end |
#total_results ⇒ Integer
29 30 31 |
# File 'lib/autorender/internal/page_pagination.rb', line 29 def total_results @total_results end |
Instance Method Details
#auto_paging_each(&blk) {|| ... } ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/autorender/internal/page_pagination.rb', line 51 def auto_paging_each(&blk) unless block_given? raise ArgumentError.new("A block must be given to ##{__method__}") end page = self loop do page.data&.each(&blk) break unless page.next_page? page = page.next_page end end |
#inspect ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
87 88 89 90 91 92 93 |
# File 'lib/autorender/internal/page_pagination.rb', line 87 def inspect # rubocop:disable Layout/LineLength model = Autorender::Internal::Type::Converter.inspect(@model, depth: 1) "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)} current_page=#{current_page.inspect} has_next=#{has_next.inspect} total_results=#{total_results.inspect}>" # rubocop:enable Layout/LineLength end |
#next_page ⇒ self
38 39 40 41 42 43 44 45 46 |
# File 'lib/autorender/internal/page_pagination.rb', line 38 def next_page unless next_page? = "No more pages available. Please check #next_page? before calling ##{__method__}" raise RuntimeError.new() end req = Autorender::Internal::Util.deep_merge(@req, {query: {page: (current_page || 1).to_i.succ}}) @client.request(req) end |
#next_page? ⇒ Boolean
32 33 34 |
# File 'lib/autorender/internal/page_pagination.rb', line 32 def next_page? !data.to_a.empty? end |