class TalentScout::Criteria

@!visibility private

Attributes

allow_nil[R]
block[R]
names[R]

Public Class Methods

new(names, allow_nil, &block) click to toggle source
# File lib/talent_scout/criteria.rb, line 7
def initialize(names, allow_nil, &block)
  @names = Array(names).map(&:to_s)
  @allow_nil = allow_nil
  @block = block
end

Public Instance Methods

applicable?(attribute_set) click to toggle source
# File lib/talent_scout/criteria.rb, line 31
def applicable?(attribute_set)
  names.all? do |name|
    attribute = attribute_set[name]
    attribute.came_from_user? &&
      (!attribute.value.nil? || (allow_nil && attribute.value_before_type_cast.nil?))
  end
end
apply(scope, attribute_set) click to toggle source
# File lib/talent_scout/criteria.rb, line 13
def apply(scope, attribute_set)
  if applicable?(attribute_set)
    if block
      block_args = names.map{|name| attribute_set[name].value }
      if block.arity == -1 # block from Symbol#to_proc
        scope.instance_exec(scope, *block_args, &block)
      else
        scope.instance_exec(*block_args, &block)
      end || scope
    else
      where_args = names.reduce({}){|h, name| h[name] = attribute_set[name].value; h }
      scope.where(where_args)
    end
  else
    scope
  end
end