Module: Ximate::InstanceMethods

Defined in:
lib/ximate/search.rb

Instance Method Summary collapse

Instance Method Details

#add_text(text, priority = 1) ⇒ Object



83
84
85
86
87
# File 'lib/ximate/search.rb', line 83

def add_text(text, priority = 1)
  @words ||= {}
  @words[priority] ||= []
  @words[priority] += text.to_s.gsub(/<[^>]*>/i, ' ').gsub(/[\.,'":;!\?\(\)]/, ' ').split(' ').map{|word| word.downcase}.uniq
end

#remove_index(locale) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/ximate/search.rb', line 103

def remove_index(locale)
  table = self.class.table_name.to_sym
  @words = {}
  DATA[locale.to_sym][table].each do |word, ids_ranks|
    ids_ranks.delete(self.id)
    DATA[locale.to_sym][table].delete(word) if ids_ranks.empty?
  end
end

#update_index(locale = I18n.default_locale, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ximate/search.rb', line 89

def update_index(locale = I18n.default_locale, &block)
  if DATA[locale]
    table = self.class.table_name.to_sym
    remove_index(locale)
    instance_eval(&block)
    @words.each do |priority, words|
      words.each do |word|
        ids_ranks = (DATA[locale.to_sym][table][word] ||= {})
        ids_ranks[self.id] = ids_ranks[self.id].to_i + priority
      end
    end
  end
end