Sha256: a9b5e39aa5ad1abd93fc99433cb08cb6c9855bc4c0f0dc9c7ae9871f41c3bb47

Contents?: true

Size: 1.85 KB

Versions: 35

Compression:

Stored size: 1.85 KB

Contents

module MongoMapper
  module Support
    # @api private
    module Find
      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

      class DynamicFinder
        attr_reader :method, :attributes, :finder, :bang, :instantiator

        def initialize(method)
          @method = method
          @finder = :first
          @bang   = false
          match
        end

        def found?
          @finder.present?
        end

        def raise?
          bang == true
        end

        protected
          def match
            case method.to_s
              when /^find_(all_by|by)_([_a-zA-Z]\w*)$/
                @finder = :all if $1 == 'all_by'
                names = $2
              when /^find_by_([_a-zA-Z]\w*)\!$/
                @bang = true
                names = $1
              when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
                @instantiator = $1 == 'initialize' ? :new : :create
                names = $2
              else
                @finder = nil
            end

            @attributes = names && names.split('_and_')
          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

Version data entries

35 entries across 35 versions & 7 rubygems

Version Path
pwnash-mongo_mapper-0.7.5 lib/mongo_mapper/support/find.rb
mongo_mapper-0.7.6 lib/mongo_mapper/support/find.rb
mongo_mapper-0.7.5 lib/mongo_mapper/support/find.rb
mongo_mapper_ign-0.7.4 lib/mongo_mapper/support/find.rb
mongo_mapper-0.7.4 lib/mongo_mapper/support/find.rb
mongo_mapper-0.7.3 lib/mongo_mapper/support/find.rb
numon-0.0.1 lib/mongo_mapper/support/find.rb
mongo_mapper-0.7.2 lib/mongo_mapper/support/find.rb
mongo_mapper-0.7.1 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.3.8 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.3.5 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.3.4 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.3.3 lib/mongo_mapper/support/find.rb
drogus-mongo_mapper-0.6.10 lib/mongo_mapper/support/find.rb
mongo_mapper-rails3-0.7.0.1 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.2.28 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/support/find.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/support/find.rb