Class: IronAdmin::Adapters::Http::Query
- Inherits:
-
Object
- Object
- IronAdmin::Adapters::Http::Query
- Includes:
- Enumerable
- Defined in:
- lib/iron_admin/adapters/http/query.rb
Overview
Chainable lazy query object for HTTP API resources.
Accumulates filters, ordering, limit, and offset as parameters. Only fires an HTTP request when records are actually needed (via #each, #count, #first, #map, etc.).
Implements the interface that IronAdmin controllers and Pagy expect: .count, .offset(n), .limit(n), .first, .map, .each, .merge
Instance Method Summary collapse
- #count ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#initialize(connection, params: {}, offset_value: nil, limit_value: nil, sort_params: {}) ⇒ Query
constructor
A new instance of Query.
- #limit(value) ⇒ Object
- #merge(other) ⇒ Object
- #offset(value) ⇒ Object
- #order(sort_hash) ⇒ Object
- #where(conditions = {}) ⇒ Object
Constructor Details
#initialize(connection, params: {}, offset_value: nil, limit_value: nil, sort_params: {}) ⇒ Query
Returns a new instance of Query.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/iron_admin/adapters/http/query.rb', line 17 def initialize(connection, params: {}, offset_value: nil, limit_value: nil, sort_params: {}) @connection = connection @params = params.dup @offset_value = offset_value @limit_value = limit_value @sort_params = sort_params.dup @fetched = false @records = nil @total = nil end |
Instance Method Details
#count ⇒ Object
35 36 37 38 |
# File 'lib/iron_admin/adapters/http/query.rb', line 35 def count fetch! @total || @records.length end |
#each(&block) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/iron_admin/adapters/http/query.rb', line 28 def each(&block) return to_enum(:each) unless block fetch! @records.each(&block) end |
#empty? ⇒ Boolean
45 46 47 48 |
# File 'lib/iron_admin/adapters/http/query.rb', line 45 def empty? fetch! @records.empty? end |
#first ⇒ Object
40 41 42 43 |
# File 'lib/iron_admin/adapters/http/query.rb', line 40 def first fetch! @records.first end |
#limit(value) ⇒ Object
58 59 60 |
# File 'lib/iron_admin/adapters/http/query.rb', line 58 def limit(value) dup_with(limit_value: value) end |
#merge(other) ⇒ Object
66 67 68 69 70 |
# File 'lib/iron_admin/adapters/http/query.rb', line 66 def merge(other) return dup_with unless other.is_a?(Query) dup_with(params: @params.merge(other.accumulated_params)) end |
#offset(value) ⇒ Object
54 55 56 |
# File 'lib/iron_admin/adapters/http/query.rb', line 54 def offset(value) dup_with(offset_value: value) end |
#order(sort_hash) ⇒ Object
62 63 64 |
# File 'lib/iron_admin/adapters/http/query.rb', line 62 def order(sort_hash) dup_with(sort_params: @sort_params.merge(sort_hash)) end |
#where(conditions = {}) ⇒ Object
50 51 52 |
# File 'lib/iron_admin/adapters/http/query.rb', line 50 def where(conditions = {}) dup_with(params: @params.merge(conditions)) end |