Module: Mongoid::Matcher::ClassMethods

Defined in:
lib/mongoid/matcher.rb

Instance Method Summary collapse

Instance Method Details

#matching(search_string, opt = {types: :all, fields: :all}) ⇒ Object

Arguments:

String  search_string 
Hash    opt 
  types:  :all|array_of_types           (default: :all,       example: [String, Array])
  fields: :all|array_of_names           (default: :all,       example: [:name, :age])


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mongoid/matcher.rb', line 13

def matching(search_string, opt={types: :all, fields: :all}) 
  fields        = Hash[self.fields]
  keys          = fields.keys

  case opt[:types]
  when Array
    keys.keep_if! { |key| opt[:types].include?(fields[key].type) }
  end
  
  case opt[:fields]
  when Array
    keys.keep_if! { |key| opt[:fields].include?(key) }
  end

  search        = keys.map { |key| Hash[key, search_string] }
  any_of(search)     
end