Module: MongoMapper::Plugins::DynamicQuerying::ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



34
35
36
37
38
39
40
41
42
# File 'lib/mongo_mapper/plugins/dynamic_querying.rb', line 34

def method_missing(method, *args, &block)
  finder = DynamicFinder.new(method)

  if finder.found?
    dynamic_find(finder, args)
  else
    super
  end
end

Instance Method Details

#dynamic_find(finder, args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongo_mapper/plugins/dynamic_querying.rb', line 10

def dynamic_find(finder, args)
  attributes = {}

  finder.attributes.each_with_index do |attr, index|
    attributes[attr] = args[index]
  end

  options = args.extract_options!.merge(attributes)

  if result = send(finder.finder, options)
    result
  else
    if finder.raise?
      raise DocumentNotFound, "Couldn't find Document with #{attributes.inspect} in collection named #{collection.name}"
    end

    if finder.instantiator
      self.send(finder.instantiator, attributes)
    end
  end
end