class Suggester::Handlers::Base

Attributes

unique_field_name[RW]

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

Public Class Methods

new(params = {}) click to toggle source
# 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

Public Instance Methods

cache() click to toggle source

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

# File lib/suggester/handlers/base.rb, line 33
def cache
  @cache
end
find(params) click to toggle source

Returns an array of data hashes that begin with params

# 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) click to toggle source

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

# 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

Protected Instance Methods

build_cache() click to toggle source

Build a copy of the cache (needs to be specified by subclasses)

# File lib/suggester/handlers/base.rb, line 56
def build_cache
  []
end
find_begin_matches(search_string, limit, params) click to toggle source

returns an array of begins with matches as:

[ 
  {
    <unique_field_name> => <anything>
    ...other data
  }
]
# File lib/suggester/handlers/base.rb, line 72
def find_begin_matches(search_string, limit, params)
  results = []
  lower_bound = find_lower_bound(search_string)

  for i in lower_bound...@cache.length
    # stop looking if we are no longer matching OR we have found enough matches
    break if @cache[i][:search_term].index(search_string) != 0 || (limit && results.length >= limit)
    results << @cache[i]
  end

  results.map{|r| r[:data]}
end
find_exact_matches(search_string, limit, params) click to toggle source

returns an array of exact matches as:

[ 
  {
    <unique_field_name> => <anything>
    ...other data
  }
]
# File lib/suggester/handlers/base.rb, line 92
def find_exact_matches(search_string, limit, params)
  results = []
  lower_bound = find_lower_bound(search_string)

  for i in lower_bound...@cache.length
    # stop looking if we are no longer matching OR we have found enough matches
    break if @cache[i][:search_term] != search_string || (limit && results.length >= limit)
    results << @cache[i]
  end

  results.map{|r| r[:data]}
end
find_lower_bound(string) click to toggle source

do a binary search through the cache to find the lowest index

# File lib/suggester/handlers/base.rb, line 61
def find_lower_bound(string)
  @cache.bsearch_lower_boundary {|x| x[:search_term] <=> string}
end