Class: Dry::Logic::Operations::Check

Inherits:
Unary show all
Defined in:
lib/dry/logic/operations/check.rb

Instance Attribute Summary collapse

Attributes inherited from Unary

#rule

Attributes inherited from Abstract

#options, #rules

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unary

#to_s

Methods inherited from Abstract

#curry, #id, #new, #to_ast, #with

Methods included from Dry::Logic::Operators

#and, #or, #then, #xor

Constructor Details

#initialize(*rules, **options) ⇒ Check

Returns a new instance of Check.



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

def initialize(*rules, **options)
  super
  @evaluator = options[:evaluator]
end

Instance Attribute Details

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



9
10
11
# File 'lib/dry/logic/operations/check.rb', line 9

def evaluator
  @evaluator
end

Class Method Details

.new(rule, **options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/dry/logic/operations/check.rb', line 11

def self.new(rule, **options)
  if options[:evaluator]
    super(rule, options)
  else
    keys = options.fetch(:keys)
    evaluator = Evaluator::Set.new(keys)

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

Instance Method Details

#[](input) ⇒ Object



42
43
44
# File 'lib/dry/logic/operations/check.rb', line 42

def [](input)
  rule[*evaluator[input].reverse]
end

#ast(input = Undefined) ⇒ Object



46
47
48
# File 'lib/dry/logic/operations/check.rb', line 46

def ast(input = Undefined)
  [type, [options[:keys], rule.ast(input)]]
end

#call(input) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/dry/logic/operations/check.rb', line 31

def call(input)
  *head, tail = evaluator[input]
  result = rule.curry(*head).(tail)

  if result.success?
    Result::SUCCESS
  else
    Result.new(false, id) { [type, [options[:keys], result.to_ast]] }
  end
end

#typeObject



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

def type
  :check
end