Module: MongoMapper::Plugins::Clortho::InstanceMethods

Defined in:
lib/clortho.rb

Instance Method Summary collapse

Instance Method Details

#filter_and_normalize(text) ⇒ Object


92
93
94
# File 'lib/clortho.rb', line 92

def filter_and_normalize(text)
  filter_out_punctuation(text).downcase
end

#filter_on_exclusions(field_and_options, keywords) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/clortho.rb', line 64

def filter_on_exclusions(field_and_options, keywords)
  field, options = field_and_options
  if options[:exclude]
    
    options[:exclude].each do |exclusion|
      if exclusion.is_a? String
        keywords.gsub!(/(\b#{exclusion}\b)/, '')
      elsif exclusion.is_a? Symbol
        klass = self.class
        ex = exclusion.to_s.upcase.to_sym
        if Clortho::ExclusionConstants::const_get(ex)
          Clortho::ExclusionConstants::const_get(ex).each do |e|
            keywords.gsub!(/(\b#{e}\b)/, '')
          end
        end
      end
    end
  end
  # strip punctuation and extra spaces
  unless keywords.nil?
    filter_out_punctuation(keywords)
  end
end

#filter_out_punctuation(text) ⇒ Object


88
89
90
# File 'lib/clortho.rb', line 88

def filter_out_punctuation(text)
  text.gsub(/\b\W/, ' ').strip.gsub(/\s+/, ' ')
end

#inject_default_keywordsObject


53
54
55
56
57
58
59
60
61
62
# File 'lib/clortho.rb', line 53

def inject_default_keywords
  searchable_with_options.each do |field|
    if !self.send(field[0]).nil?
      text = self.send(field[0]).to_s
      keywords = filter_and_normalize(!field[1][:exclude].nil? ? filter_on_exclusions(field, filter_and_normalize(text)) : text)
      self.send("#{field[0].to_s}_keywords=".to_sym, keywords) if keywords
      self.send("#{field[0].to_s}_keywords_array=".to_sym, keywords.split.each{ |kw| kw.downcase }) if keywords
    end
  end
end