Class: Stellr::Search::SearchResults

Inherits:
Object
  • Object
show all
Defined in:
lib/stellr/search/search_results.rb

Overview

Thin wrapper around an array of search results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSearchResults

Returns a new instance of SearchResults.



9
10
11
# File 'lib/stellr/search/search_results.rb', line 9

def initialize
  @results = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



22
23
24
# File 'lib/stellr/search/search_results.rb', line 22

def method_missing(symbol, *args, &block)
  @results.send(symbol, *args, &block)
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



6
7
8
# File 'lib/stellr/search/search_results.rb', line 6

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



6
7
8
# File 'lib/stellr/search/search_results.rb', line 6

def per_page
  @per_page
end

#total_hitsObject Also known as: total_entries

Returns the value of attribute total_hits.



6
7
8
# File 'lib/stellr/search/search_results.rb', line 6

def total_hits
  @total_hits
end

Instance Method Details

#next_pageObject

current_page + 1 or nil if there is no next page



54
55
56
# File 'lib/stellr/search/search_results.rb', line 54

def next_page
  current_page < page_count ? (current_page + 1) : nil
end

#offsetObject

Current offset of the paginated collection. If we’re on the first page, it is always 0. If we’re on the 2nd page and there are 30 entries per page, the offset is 30. This property is useful if you want to render ordinals besides your records: simply start with offset + 1.



44
45
46
# File 'lib/stellr/search/search_results.rb', line 44

def offset
  (current_page - 1) * per_page
end

#previous_pageObject

current_page - 1 or nil if there is no previous page



49
50
51
# File 'lib/stellr/search/search_results.rb', line 49

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/stellr/search/search_results.rb', line 26

def respond_to?(name)
  self.methods.include?(name) || @results.respond_to?(name)
end

#to_jsonObject



13
14
15
16
17
18
19
20
# File 'lib/stellr/search/search_results.rb', line 13

def to_json
  {
    :results => @results,
    :total_hits => total_hits,
    :current_page => current_page,
    :per_page => per_page
  }.to_json
end

#total_pagesObject Also known as: page_count

The total number of pages.



34
35
36
# File 'lib/stellr/search/search_results.rb', line 34

def total_pages
  @total_pages ||= per_page > 0 ? (total_hits / per_page.to_f).ceil : 0
end