Class: Dry::Logic::Builder::Context

Inherits:
Object
  • Object
show all
Includes:
Dry::Logic, Singleton
Defined in:
lib/dry/logic/builder.rb

Defined Under Namespace

Modules: Predicates

Constant Summary

Constants included from Dry::Logic

VERSION

Instance Method Summary collapse

Methods included from Dry::Logic

Rule, loader

Constructor Details

#initializeContext

Defines methods for operations and predicates



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Method Details

#call(&context) ⇒ Object



47
48
49
# File 'lib/dry/logic/builder.rb', line 47

def call(&context)
  instance_eval(&context)
end

#predicate(name, &context) ⇒ Object

Defines custom predicate



55
56
57
58
59
60
61
62
63
64
65
# 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