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.



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

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

Instance Attribute Details

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



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

def evaluator
  @evaluator
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.evaluator(name) ⇒ Object



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

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

.new(rules, options) ⇒ Object



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

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



48
49
50
# File 'lib/dry/logic/operations/key.rb', line 48

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

#ast(input = nil) ⇒ Object



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

def ast(input = nil)
  if input
    [type, [path, rule.ast(evaluator[input])]]
  else
    [type, [path, rule.ast]]
  end
end

#call(hash) ⇒ Object



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

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



60
61
62
# File 'lib/dry/logic/operations/key.rb', line 60

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

#typeObject



33
34
35
# File 'lib/dry/logic/operations/key.rb', line 33

def type
  :key
end