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.



20
21
22
23
# File 'lib/dry/logic/operations/check.rb', line 20

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

Instance Attribute Details

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



7
8
9
# File 'lib/dry/logic/operations/check.rb', line 7

def evaluator
  @evaluator
end

Class Method Details

.new(rule, **options) ⇒ Object



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

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

    super(rule, **options, evaluator: evaluator)
  end
end

Instance Method Details

#[](input) ⇒ Object



40
41
42
# File 'lib/dry/logic/operations/check.rb', line 40

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

#ast(input = Undefined) ⇒ Object



44
45
46
# File 'lib/dry/logic/operations/check.rb', line 44

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

#call(input) ⇒ Object



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

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



25
26
27
# File 'lib/dry/logic/operations/check.rb', line 25

def type
  :check
end