Sha256: b9881e3befddeb5f356ab32a7f6781de8c388edd690088ad1af38c81a3437ecc

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

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

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

        protected
          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

6 entries across 6 versions & 1 rubygems

Version Path
mark_mapper-0.0.6 lib/mark_mapper/plugins/dynamic_querying.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/dynamic_querying.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/dynamic_querying.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/dynamic_querying.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/dynamic_querying.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/dynamic_querying.rb