Class: Dry::Logic::Rule::Check

Inherits:
Value show all
Defined in:
lib/dry/logic/rule/check.rb

Instance Attribute Summary collapse

Attributes inherited from Dry::Logic::Rule

#options, #predicate

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#apply

Methods inherited from Dry::Logic::Rule

#and, #curry, #each?, #negation, #new, #or, #predicate_id, #then, #xor

Constructor Details

#initialize(predicate, options) ⇒ Check

Returns a new instance of Check.



15
16
17
18
19
# File 'lib/dry/logic/rule/check.rb', line 15

def initialize(predicate, options)
  super
  @name = options.fetch(:name)
  @evaluator = options[:evaluator]
end

Instance Attribute Details

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



6
7
8
# File 'lib/dry/logic/rule/check.rb', line 6

def evaluator
  @evaluator
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/dry/logic/rule/check.rb', line 6

def name
  @name
end

Class Method Details

.new(predicate, options) ⇒ Object



8
9
10
11
12
13
# File 'lib/dry/logic/rule/check.rb', line 8

def self.new(predicate, options)
  keys = options.fetch(:keys)
  evaluator = Evaluator::Set.new(keys)

  super(predicate, options.merge(evaluator: evaluator))
end

Instance Method Details

#call(input) ⇒ Object



21
22
23
24
25
# File 'lib/dry/logic/rule/check.rb', line 21

def call(input)
  args = evaluator[input].reverse
  *head, tail = args
  Logic.Result(predicate.curry(*head).(tail), head.size > 0 ? curry(*head) : self, input)
end

#evaluate(input) ⇒ Object



27
28
29
# File 'lib/dry/logic/rule/check.rb', line 27

def evaluate(input)
  evaluator[input].first
end

#to_astObject



35
36
37
# File 'lib/dry/logic/rule/check.rb', line 35

def to_ast
  [type, [name, predicate.to_ast]]
end

#typeObject



31
32
33
# File 'lib/dry/logic/rule/check.rb', line 31

def type
  :check
end