Module: MongoMapper::Support::Find Private

Included in:
Plugins::Associations::InArrayProxy, Plugins::Associations::ManyDocumentsProxy
Defined in:
lib/mongo_mapper/support/find.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: DynamicFinder

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
70
71
72
73
74
75
# File 'lib/mongo_mapper/support/find.rb', line 67

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongo_mapper/support/find.rb', line 6

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