Class: IndexedSearch::Match::Initials

Inherits:
Base
  • Object
show all
Defined in:
lib/indexed_search/match/initials.rb

Overview

Does an “initials” match for names. Basically, it matches a full name against its initial, and an initial against all full names. This is kind of a variation of a start-with match, but more efficient for this one purpose, and it works properly in both directions.

Instance Method Summary collapse

Methods inherited from Base

#find, find_attributes, #initialize, match_against_term?, #results, #term_matches, #term_non_matches

Constructor Details

This class inherits a constructor from IndexedSearch::Match::Base

Instance Method Details

#scopeObject



17
18
19
20
21
22
23
24
25
# File 'lib/indexed_search/match/initials.rb', line 17

def scope
  arel_atr = @scope.arel_table[self.class.matcher_attribute]
  @scope.where(
    arel_atr.matches_any(term_matches.select { |t| t.length == 1 }.uniq.collect { |t| "#{t}%" }).
    or(
      arel_atr.in(term_matches.select { |t| t.length > 1 }.collect(&:first).uniq)
    )
  )
end

#term_mapObject



27
28
29
30
31
32
33
# File 'lib/indexed_search/match/initials.rb', line 27

def term_map
  @term_map ||= Hash.new { |hash,key| hash[key] = [] }.tap do |map|
    find.each do |id,count,rank,match|
      term_matches.each { |term| map[match] << term if term.length == 1 ? match.start_with?(term) : match == term.first }
    end
  end
end