Sha256: ce04566c85de846be7dde4783e21a6617760c599b0fcdc221ea8d3aab41a8d55

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module DynamicQuerying
      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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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