Sha256: 0767af471436f5914a629697f0dcbd8d1e8aba745577fe952818245a2524d1c5

Contents?: true

Size: 1.69 KB

Versions: 29

Compression:

Stored size: 1.69 KB

Contents

module MongoMapper
  # @api private
  module Finders
    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 = find(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
  
  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
end

Version data entries

29 entries across 29 versions & 5 rubygems

Version Path
jmonteiro-mongo_mapper-0.1.7 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-unstable-2010.1.18 lib/mongo_mapper/dynamic_finder.rb
jmonteiro-mongo_mapper-0.1.6 lib/mongo_mapper/dynamic_finder.rb
jmonteiro-mongo_mapper-0.1.5 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-unstable-2010.1.17 lib/mongo_mapper/dynamic_finder.rb
jmonteiro-mongo_mapper-0.1.4 lib/mongo_mapper/dynamic_finder.rb
jmonteiro-mongo_mapper-0.1.2 lib/mongo_mapper/dynamic_finder.rb
jmonteiro-mongo_mapper-0.1.1 lib/mongo_mapper/dynamic_finder.rb
jmonteiro-mongo_mapper-0.1.0 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-unstable-2010.1.12 lib/mongo_mapper/dynamic_finder.rb
mrkurt-mongo_mapper-0.6.11 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-unstable-2010.1.6 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-unstable-2010.1.5 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-unstable-2010.1.4 lib/mongo_mapper/dynamic_finder.rb
tpitale-mongo_mapper-0.6.10 lib/mongo_mapper/dynamic_finder.rb
mrkurt-mongo_mapper-0.6.10 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-0.6.10 lib/mongo_mapper/dynamic_finder.rb
mrkurt-mongo_mapper-0.6.9 lib/mongo_mapper/dynamic_finder.rb
mongo_mapper-0.6.9 lib/mongo_mapper/dynamic_finder.rb
mrkurt-mongo_mapper-0.6.8 lib/mongo_mapper/dynamic_finder.rb