Class: IndexedSearch::Match::Base

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

Overview

base class for common functionality in match definition classes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, terms) ⇒ Base

Returns a new instance of Base.



110
111
112
113
# File 'lib/indexed_search/match/base.rb', line 110

def initialize(scope, terms)
  @scope = scope
  @terms = terms
end

Class Method Details

.find_attributesObject



145
146
147
148
149
150
151
# File 'lib/indexed_search/match/base.rb', line 145

def self.find_attributes
  if matcher_attribute.kind_of?(Array)
    [:id, :entries_count, :rank_limit] + matcher_attribute
  else
    [:id, :entries_count, :rank_limit] << matcher_attribute
  end
end

.match_against_term?(term) ⇒ Boolean

Whether or not we should do a given algorithm match on indicated input term word text. Rather than overriding, see class attributes: min_term_length, max_term_length, match_against_term

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
# File 'lib/indexed_search/match/base.rb', line 130

def self.match_against_term?(term)
  if ! match_against_term.nil?
    match_against_term.call(term)
  else
    return false if ! min_term_length.nil? && term.length < min_term_length
    return false if ! max_term_length.nil? && term.length > max_term_length
    true
  end
end

Instance Method Details

#findObject



152
153
154
# File 'lib/indexed_search/match/base.rb', line 152

def find
  @find ||= scope.values_of(*self.class.find_attributes)
end

#results(do_all = false) ⇒ Object

override this if a matcher returns multiple kinds of results…



157
158
159
160
161
162
163
# File 'lib/indexed_search/match/base.rb', line 157

def results(do_all=false)
  [].tap do |res|
    if do_all || term_matches.present?
      res << IndexedSearch::Match::Result.new(self, term_map, rank_multiplier, term_multiplier, limit_reduction_factor, type_reduction_factor)
    end
  end
end

#scopeObject

override in subclass to add a match-type-specific where clause to scope



141
142
143
# File 'lib/indexed_search/match/base.rb', line 141

def scope
  @scope
end

#term_matchesObject

the terms we should actually match against for a given match algorithm type for some algorithms this will be shorter than the terms we were initialized with, because some terms are unsuitable for some algorithms (too short, bad characters, etc)



118
119
120
# File 'lib/indexed_search/match/base.rb', line 118

def term_matches
  @term_matches ||= @terms.select { |t| self.class.match_against_term?(t) }
end

#term_non_matchesObject

the inverse of term_matches, a list of the terms that were rejected only used by explain rake task, not the usual searching algorithm



124
125
126
# File 'lib/indexed_search/match/base.rb', line 124

def term_non_matches
  @terms.reject { |t| self.class.match_against_term?(t) }
end