class MongoModel::DynamicFinder

Public Class Methods

match(scope, method) click to toggle source
# File lib/mongomodel/support/dynamic_finder.rb, line 32
def self.match(scope, method)
  finder = :first
  bang = false

  case method.to_s
  when /^find_(all_by|last_by|by)_([_a-zA-Z]\w*)$/
    finder = :last if $1 == 'last_by'
    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*)$/
    finder = ($1 == 'initialize' ? :new : :create)
    names = $2
  else
    return nil
  end

  names = names.split('_and_')
  if names.all? { |n| scope.klass.properties.include?(n.to_sym) }
    new(scope, names, finder, bang)
  end
end
new(scope, attribute_names, finder=:first, bang=false) click to toggle source
# File lib/mongomodel/support/dynamic_finder.rb, line 3
def initialize(scope, attribute_names, finder=:first, bang=false)
  @scope, @attribute_names, @finder, @bang = scope, attribute_names, finder, bang
end

Public Instance Methods

bang?() click to toggle source
# File lib/mongomodel/support/dynamic_finder.rb, line 24
def bang?
  @bang
end
execute(*args) click to toggle source
# File lib/mongomodel/support/dynamic_finder.rb, line 7
def execute(*args)
  options = args.extract_options!
  conditions = build_conditions(args)

  result = @scope.where(conditions).send(instantiator? ? :first : @finder)

  if result.nil?
    if bang?
      raise DocumentNotFound, "Couldn't find #{@scope.klass.to_s} with #{conditions.inspect}"
    elsif instantiator?
      return @scope.send(@finder, conditions)
    end
  end

  result
end
instantiator?() click to toggle source
# File lib/mongomodel/support/dynamic_finder.rb, line 28
def instantiator?
  @finder == :new || @finder == :create
end

Private Instance Methods

build_conditions(args) click to toggle source
# File lib/mongomodel/support/dynamic_finder.rb, line 58
def build_conditions(args)
  result = {}

  @attribute_names.zip(args) do |attribute, value|
    result[attribute.to_sym] = value
  end

  result
end