Module: Fuzzzy::Mongoid::Index::ClassMethods

Defined in:
lib/fuzzzy/orm/mongoid/index.rb

Instance Method Summary collapse

Instance Method Details

#clear_fuzzzy_index(field) ⇒ Object



21
22
23
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 21

def clear_fuzzzy_index field
  self.fuzzzy_indexes.delete(field.to_sym)
end

#default_optionsObject



29
30
31
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 29

def default_options
  {:method => :soundex}
end

#define_fuzzzy_index(field, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 15

def define_fuzzzy_index field, options={}
  self.fuzzzy_indexes ||= {}
  options[:index_name] = index_name(field)
  self.fuzzzy_indexes[field.to_sym] = default_options.merge(options)
end

#has_fuzzzy_indexes?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 25

def has_fuzzzy_indexes?
  !!self.fuzzzy_indexes
end

#index_name(field) ⇒ Object



58
59
60
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 58

def index_name field
  "#{self.name.downcase}:#{field}"
end

#indexer(method) ⇒ Object



33
34
35
36
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 33

def indexer method
  return nil unless has_fuzzzy_indexes?
  _indexer(method)
end

#only_ids?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 54

def only_ids?
  index_context[:only_ids] && !index_context[:with_cache]
end

#search_by(field, query, context = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 43

def search_by field, query, context={}
  index_context = self.fuzzzy_indexes[field.to_sym].dup
  raise "You have not fuzzy index for '#{field}' field" unless index_context

  index_context[:query] = query
  index_context.merge!(context)
  ids = searcher(index_context[:method]).search(index_context)

  (only_ids? ? ids : scoped.find(ids)) if ids
end

#searcher(method) ⇒ Object



38
39
40
41
# File 'lib/fuzzzy/orm/mongoid/index.rb', line 38

def searcher method
  return nil unless has_fuzzzy_indexes?
  _searcher(method)
end