Class: Suggester::Handlers::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers::Refresh
Defined in:
lib/suggester/handlers/base.rb

Direct Known Subclasses

ActiveRecord, Marshal, Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Refresh

#force_refresh!, #last_refreshed_at, #needs_refresh?, #refresh!, #refresh_interval, #refresh_interval=

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
# File 'lib/suggester/handlers/base.rb', line 17

def initialize(params = {})
  @unique_field_name = params[:unique_field_name] || :display_string
  @refresh_interval = params.delete(:refresh_interval)
  @last_refreshed_at = Time.now
  @cache = build_cache
end

Instance Attribute Details

#unique_field_nameObject

name of the field in the data hash that should be unique



14
15
16
# File 'lib/suggester/handlers/base.rb', line 14

def unique_field_name
  @unique_field_name
end

Instance Method Details

#cacheObject

Returns an array of hashes with the following format:

[
  :search_term    =>  <string>,
  :data           =>  {
    <unique_field_name> =>  <anything>
    ...other data to be returned
  }
]

NOTE: must be sorted by :search_term



33
34
35
# File 'lib/suggester/handlers/base.rb', line 33

def cache
  @cache
end

#find(params) ⇒ Object

Returns an array of data hashes that begin with params



46
47
48
49
50
51
# File 'lib/suggester/handlers/base.rb', line 46

def find(params)
  query = params[:query].downcase
  limit = params[:limit]
  limit = limit.to_i unless limit.nil?
  results = find_begin_matches(query, limit, params)
end

#match(params) ⇒ Object

Returns an array of data hashes that are an exact match for params



38
39
40
41
42
43
# File 'lib/suggester/handlers/base.rb', line 38

def match(params)
  query = params[:query].downcase
  limit = params[:limit]
  limit = limit.to_i unless limit.nil?
  results = find_exact_matches(query, limit, params)
end