Class: LcpRuby::SearchResult
- Inherits:
-
Object
- Object
- LcpRuby::SearchResult
- Includes:
- Enumerable
- Defined in:
- lib/lcp_ruby/search_result.rb
Overview
Kaminari-compatible value object for paginated search results from API data sources. Wraps an array of records with pagination metadata so views/controllers can treat it the same as an ActiveRecord paginated scope.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
Instance Method Summary collapse
- #count ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#error? ⇒ Boolean
Error state.
- #first_page? ⇒ Boolean
-
#initialize(records:, total_count:, current_page: 1, per_page: 25, error: false, stale: false, message: nil) ⇒ SearchResult
constructor
A new instance of SearchResult.
- #last_page? ⇒ Boolean
- #limit_value ⇒ Object
- #size ⇒ Object (also: #length)
- #stale? ⇒ Boolean
- #to_a ⇒ Object
-
#total_pages ⇒ Object
Kaminari compatibility.
Constructor Details
#initialize(records:, total_count:, current_page: 1, per_page: 25, error: false, stale: false, message: nil) ⇒ SearchResult
Returns a new instance of SearchResult.
10 11 12 13 14 15 16 17 18 |
# File 'lib/lcp_ruby/search_result.rb', line 10 def initialize(records:, total_count:, current_page: 1, per_page: 25, error: false, stale: false, message: nil) @records = Array(records) @total_count = total_count.to_i @current_page = [ current_page.to_i, 1 ].max @per_page = [ per_page.to_i, 1 ].max @error = error @stale = stale @message = end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
8 9 10 |
# File 'lib/lcp_ruby/search_result.rb', line 8 def current_page @current_page end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
8 9 10 |
# File 'lib/lcp_ruby/search_result.rb', line 8 def @message end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
8 9 10 |
# File 'lib/lcp_ruby/search_result.rb', line 8 def per_page @per_page end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
8 9 10 |
# File 'lib/lcp_ruby/search_result.rb', line 8 def records @records end |
#total_count ⇒ Object (readonly)
Returns the value of attribute total_count.
8 9 10 |
# File 'lib/lcp_ruby/search_result.rb', line 8 def total_count @total_count end |
Instance Method Details
#count ⇒ Object
56 57 58 |
# File 'lib/lcp_ruby/search_result.rb', line 56 def count @total_count end |
#each(&block) ⇒ Object
20 21 22 |
# File 'lib/lcp_ruby/search_result.rb', line 20 def each(&block) @records.each(&block) end |
#empty? ⇒ Boolean
29 30 31 |
# File 'lib/lcp_ruby/search_result.rb', line 29 def empty? @records.empty? end |
#error? ⇒ Boolean
Error state
62 63 64 |
# File 'lib/lcp_ruby/search_result.rb', line 62 def error? @error == true end |
#first_page? ⇒ Boolean
48 49 50 |
# File 'lib/lcp_ruby/search_result.rb', line 48 def first_page? @current_page <= 1 end |
#last_page? ⇒ Boolean
52 53 54 |
# File 'lib/lcp_ruby/search_result.rb', line 52 def last_page? @current_page >= total_pages end |
#limit_value ⇒ Object
44 45 46 |
# File 'lib/lcp_ruby/search_result.rb', line 44 def limit_value @per_page end |
#size ⇒ Object Also known as: length
24 25 26 |
# File 'lib/lcp_ruby/search_result.rb', line 24 def size @records.size end |
#stale? ⇒ Boolean
66 67 68 |
# File 'lib/lcp_ruby/search_result.rb', line 66 def stale? @stale == true end |
#to_a ⇒ Object
33 34 35 |
# File 'lib/lcp_ruby/search_result.rb', line 33 def to_a @records.dup end |
#total_pages ⇒ Object
Kaminari compatibility
39 40 41 42 |
# File 'lib/lcp_ruby/search_result.rb', line 39 def total_pages return 0 if @total_count.zero? (@total_count.to_f / @per_page).ceil end |