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.



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

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

Instance Attribute Details

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



11
12
13
# File 'lib/dry/logic/operations/key.rb', line 11

def evaluator
  @evaluator
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/dry/logic/operations/key.rb', line 13

def path
  @path
end

Class Method Details

.evaluator(name) ⇒ Object



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

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

.new(rules, options) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dry/logic/operations/key.rb', line 15

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

Instance Method Details

#[](hash) ⇒ Object



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

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

#ast(input = Undefined) ⇒ Object



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

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/dry/logic/operations/key.rb', line 39

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



62
63
64
# File 'lib/dry/logic/operations/key.rb', line 62

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

#typeObject



35
36
37
# File 'lib/dry/logic/operations/key.rb', line 35

def type
  :key
end