Class: Dry::Logic::Operations::Key

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

Direct Known Subclasses

Attr

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 Abstract

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

Methods included from Dry::Logic::Operators

#and, #or, #then, #xor

Constructor Details

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

Returns a new instance of Key.



25
26
27
28
29
# File 'lib/dry/logic/operations/key.rb', line 25

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

Instance Attribute Details

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



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

def evaluator
  @evaluator
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.evaluator(name) ⇒ Object



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

def self.evaluator(name)
  Evaluator::Key.new(name)
end

.new(rules, **options) ⇒ Object



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

def self.new(rules, **options)
  if options[:evaluator]
    super
  else
    name = options.fetch(:name)
    eval = options.fetch(:evaluator, evaluator(name))
    super(rules, **options, evaluator: eval, path: name)
  end
end

Instance Method Details

#[](hash) ⇒ Object



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

def [](hash)
  rule[evaluator[hash]]
end

#ast(input = Undefined) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/dry/logic/operations/key.rb', line 50

def ast(input = Undefined)
  if input.equal?(Undefined) || !input.is_a?(Hash)
    [type, [path, rule.ast]]
  else
    [type, [path, rule.ast(evaluator[input])]]
  end
end

#call(hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/dry/logic/operations/key.rb', line 35

def call(hash)
  input = evaluator[hash]
  result = rule.(input)

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

#to_sObject



58
59
60
# File 'lib/dry/logic/operations/key.rb', line 58

def to_s
  "#{type}[#{path}](#{rule})"
end

#typeObject



31
32
33
# File 'lib/dry/logic/operations/key.rb', line 31

def type
  :key
end