class Dry::Logic::Builder::Context

Public Class Methods

new() click to toggle source

Defines methods for operations and predicates

# File lib/dry/logic/builder.rb, line 68
def initialize
  Operations.constants(false).each do |name|
    next if IGNORED_OPERATIONS.include?(name)

    operation = Operations.const_get(name)

    define_singleton_method(name.downcase) do |*args, **kwargs, &block|
      operation.new(*call(&block), *args, **kwargs)
    end
  end

  Predicates::Methods.instance_methods(false).each do |name|
    unless IGNORED_PREDICATES.include?(name)
      predicate(name, &Predicates[name])
    end
  end
end

Public Instance Methods

call(&context) click to toggle source

@see Builder#call

# File lib/dry/logic/builder.rb, line 47
def call(&context)
  instance_eval(&context)
end
predicate(name, &context) click to toggle source

Defines custom predicate

@name [Symbol] Name of predicate @Context [Proc]

# File lib/dry/logic/builder.rb, line 55
def predicate(name, &context)
  if singleton_class.method_defined?(name)
    singleton_class.undef_method(name)
  end

  prerdicate = Rule::Predicate.new(context)

  define_singleton_method(name) do |*args|
    prerdicate.curry(*args)
  end
end