class BabySqueel::Resolver

Public Class Methods

new(table, strategies) click to toggle source
# File lib/baby_squeel/resolver.rb, line 3
def initialize(table, strategies)
  @table       = table
  @strategies  = strategies
end

Public Instance Methods

resolve(name, *args, &block) click to toggle source

Attempt to determine the intent of the method_missing. If this method returns nil, that means one of two things:

  1. The argument signature is invalid.

  2. The name of the method called is not valid (ex. invalid column name)

# File lib/baby_squeel/resolver.rb, line 13
def resolve(name, *args, &block)
  strategy = @strategies.find do |strategy|
    valid?(strategy, name, *args, &block)
  end

  build(strategy, name, *args, &block)
end
resolve!(name, *args, &block) click to toggle source

Try to resolve the method_missing. If we fail to resolve, there are two outcomes:

If any of the configured strategies accept argument signature provided, raise an error. This means we failed to resolve the name. (ex. invalid column name)

Otherwise, a nil return valid indicates that argument signature was not valid for any of the configured strategies. This case should be treated as a NoMethodError.

# File lib/baby_squeel/resolver.rb, line 31
def resolve!(name, *args, &block)
  if resolution = resolve(name, *args, &block)
    return resolution
  end

  if compatible_arguments?(*args, &block)
    raise NotFoundError.new(@table._scope.model_name, name, @strategies)
  end

  return nil
end
resolves?(name) click to toggle source

Used to determine if a table can respond_to? a method.

# File lib/baby_squeel/resolver.rb, line 44
def resolves?(name)
  @strategies.any? do |strategy|
    valid_name? strategy, name
  end
end

Private Instance Methods

build(strategy, name, *args) click to toggle source
# File lib/baby_squeel/resolver.rb, line 52
def build(strategy, name, *args)
  case strategy
  when :function
    @table.func(name, *args)
  when :association
    @table.association(name)
  when :column, :attribute
    @table[name]
  end
end
compatible_arguments?(*args, &block) click to toggle source
# File lib/baby_squeel/resolver.rb, line 90
def compatible_arguments?(*args, &block)
  @strategies.any? do |strategy|
    valid_arguments?(strategy, *args, &block)
  end
end
valid?(strategy, name, *args, &block) click to toggle source
# File lib/baby_squeel/resolver.rb, line 63
def valid?(strategy, name, *args, &block)
  valid_arguments?(strategy, *args, &block) &&
    valid_name?(strategy, name)
end
valid_arguments?(strategy, *args) click to toggle source
# File lib/baby_squeel/resolver.rb, line 79
def valid_arguments?(strategy, *args)
  return false if block_given?

  case strategy
  when :function
    !args.empty?
  when :column, :attribute, :association
    args.empty?
  end
end
valid_name?(strategy, name) click to toggle source
# File lib/baby_squeel/resolver.rb, line 68
def valid_name?(strategy, name)
  case strategy
  when :column
    @table._scope.column_names.include?(name.to_s)
  when :association
    !@table._scope.reflect_on_association(name).nil?
  when :function, :attribute
    true
  end
end