Class: Waistband::Query
- Inherits:
-
Object
- Object
- Waistband::Query
- Defined in:
- lib/waistband/query.rb
Instance Attribute Summary collapse
-
#page ⇒ Object
Returns the value of attribute page.
-
#page_size ⇒ Object
Returns the value of attribute page_size.
Instance Method Summary collapse
- #hits ⇒ Object
-
#initialize(index, options = {}) ⇒ Query
constructor
A new instance of Query.
- #paginated_results ⇒ Object
- #prepare(hash = {}) ⇒ Object
- #results ⇒ Object
- #total_results ⇒ Object
Constructor Details
#initialize(index, options = {}) ⇒ Query
Returns a new instance of Query.
8 9 10 11 12 13 |
# File 'lib/waistband/query.rb', line 8 def initialize(index, = {}) @index = index @page = ([:page] || 1).to_i @page_size = ([:page_size] || 20).to_i prepare end |
Instance Attribute Details
#page ⇒ Object
Returns the value of attribute page.
6 7 8 |
# File 'lib/waistband/query.rb', line 6 def page @page end |
#page_size ⇒ Object
Returns the value of attribute page_size.
6 7 8 |
# File 'lib/waistband/query.rb', line 6 def page_size @page_size end |
Instance Method Details
#hits ⇒ Object
31 32 33 |
# File 'lib/waistband/query.rb', line 31 def hits execute!['hits']['hits'] rescue [] end |
#paginated_results ⇒ Object
20 21 22 23 |
# File 'lib/waistband/query.rb', line 20 def paginated_results return Kaminari.paginate_array(results, total_count: total_results).page(@page).per(@page_size) if defined?(Kaminari) raise "Please include the `kaminari` gem to use this method!" end |
#prepare(hash = {}) ⇒ Object
15 16 17 18 |
# File 'lib/waistband/query.rb', line 15 def prepare(hash = {}) @hash = hash.with_indifferent_access self end |
#results ⇒ Object
25 26 27 28 29 |
# File 'lib/waistband/query.rb', line 25 def results hits.map do |hit| Waistband::QueryResult.new(hit) end end |
#total_results ⇒ Object
35 36 37 |
# File 'lib/waistband/query.rb', line 35 def total_results execute!['hits']['total'] rescue 0 end |