Class: Waistband::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/waistband/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @index      = index
  @page       = (options[:page] || 1).to_i
  @page_size  = (options[:page_size] || 20).to_i
  prepare
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



6
7
8
# File 'lib/waistband/query.rb', line 6

def page
  @page
end

#page_sizeObject

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

#hitsObject



31
32
33
# File 'lib/waistband/query.rb', line 31

def hits
  execute!['hits']['hits'] rescue []
end

#paginated_resultsObject



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

#resultsObject



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_resultsObject



35
36
37
# File 'lib/waistband/query.rb', line 35

def total_results
  execute!['hits']['total'] rescue 0
end