Sha256: dbab8b9b6b0345ff2ae18e7bedcef8576da8c752d6ba76e75dc7d76a02b01234

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

# encoding: UTF-8
require 'mongo_mapper/plugins/dynamic_querying/dynamic_finder'

module MongoMapper
  module Plugins
    module DynamicQuerying
      extend ActiveSupport::Concern

      module ClassMethods
        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

      private

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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mongo_mapper-0.17.0 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.16.0 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.6 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.5 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.4 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.1 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.15.0 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.14.0 lib/mongo_mapper/plugins/dynamic_querying.rb
mongo_mapper-0.14.0.rc1 lib/mongo_mapper/plugins/dynamic_querying.rb