Module: MongoidSearch::Util

Defined in:
lib/mongoid_search/util.rb

Class Method Summary collapse

Class Method Details

.keywords(klass, field, stemmer, ignore_list) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongoid_search/util.rb', line 4

def self.keywords(klass, field, stemmer, ignore_list)
  if field.is_a?(Hash)
    field.keys.map do |key|
      attribute = klass.send(key)
      unless attribute.blank?
        method = field[key]
        if attribute.is_a?(Array)
          if method.is_a?(Array)
            method.map {|m| attribute.map { |a| Util.normalize_keywords a.send(m), stemmer, ignore_list } }
          else
            attribute.map(&method).map { |t| Util.normalize_keywords t, stemmer, ignore_list }
          end
        else
          Util.normalize_keywords(attribute.send(method), stemmer, ignore_list)
        end
      end
    end
  else
    value = klass[field]
    value = value.join(' ') if value.respond_to?(:join)
    Util.normalize_keywords(value, stemmer, ignore_list) if value
  end
end

.normalize_keywords(text, stemmer, ignore_list) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid_search/util.rb', line 28

def self.normalize_keywords(text, stemmer, ignore_list)
  return [] if text.blank?
  text = text.to_s.
    mb_chars.
    normalize(:kd).
    downcase.
    to_s.
    gsub(/[._:;'"`,?|+={}()!@#%^&*<>~\$\-\\\/\[\]]/, ' '). # strip punctuation
    gsub(/[^[:alnum:]\s]/,'').   # strip accents
    split(' ').
    reject { |word| word.size < 2 }
  text = text.reject { |word| ignore_list.include?(word) } unless ignore_list.blank?
  text = text.map{ |word| stemmer.call(word) } if stemmer
  text
end