Class: Zaikio::Client::Helpers::Pagination::Relation
- Inherits:
-
Spyke::Relation
- Object
- Spyke::Relation
- Zaikio::Client::Helpers::Pagination::Relation
- Defined in:
- lib/zaikio/client/helpers/pagination.rb
Instance Method Summary collapse
- #clone ⇒ Object
-
#each(&block) ⇒ Object
Unlike the default implementation in Spyke, this version of #each is recursive and will repeatedly paginate through the remote API until it runs out of records.
- #first_page? ⇒ Boolean
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
- #supports_pagination? ⇒ Boolean
Instance Method Details
#clone ⇒ Object
114 115 116 117 118 |
# File 'lib/zaikio/client/helpers/pagination.rb', line 114 def clone # We use cloning when fetching a second page using the same scope/query, however # we want to clear any loaded records from @find_some before doing so. super.tap { |obj| obj.instance_variable_set(:@find_some, nil) } end |
#each(&block) ⇒ Object
Unlike the default implementation in Spyke, this version of #each is recursive and will repeatedly paginate through the remote API until it runs out of records.
To avoid this behaviour, you can ask for a Lazy enumerator to take just the records you need, e.g.:
User.all.lazy.take(3).to_a
#=> [.., .., ..]
104 105 106 107 108 109 110 111 112 |
# File 'lib/zaikio/client/helpers/pagination.rb', line 104 def each(&block) return to_enum(:each) unless block_given? find_some.each(&block) return if !supports_pagination? || last_page? puts "There are #{total_pages} pages, I will load more pages automatically" if first_page? clone.page(next_page).each(&block) end |
#first_page? ⇒ Boolean
76 77 78 |
# File 'lib/zaikio/client/helpers/pagination.rb', line 76 def first_page? current_page == 1 end |
#last_page? ⇒ Boolean
84 85 86 87 88 89 90 |
# File 'lib/zaikio/client/helpers/pagination.rb', line 84 def last_page? if total_pages.nil? find_some.empty? else current_page >= total_pages end end |
#next_page ⇒ Object
80 81 82 |
# File 'lib/zaikio/client/helpers/pagination.rb', line 80 def next_page current_page + 1 end |
#supports_pagination? ⇒ Boolean
92 93 94 |
# File 'lib/zaikio/client/helpers/pagination.rb', line 92 def supports_pagination? current_page.present? end |