Class: Lite::Validation::Validator::Node::Implementation::Iteration::Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/validation/validator/node/implementation/iteration/iterator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, path, from, strategy, commit) ⇒ Iterator

Returns a new instance of Iterator.



40
41
42
43
44
45
46
47
# File 'lib/lite/validation/validator/node/implementation/iteration/iterator.rb', line 40

def initialize(parent, path, from, strategy, commit)
  @parent = parent
  @path = path
  @from = from
  @strategy = strategy
  @commit = commit
  freeze
end

Class Method Details

.iterate(node, path, from, strategy, commit, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lite/validation/validator/node/implementation/iteration/iterator.rb', line 14

def self.iterate(node, path, from, strategy, commit, &block)
  node.send(:dig!, path, from) do |option, result|
    Helpers::YieldStrategy
      .to_iterate(strategy)
      .maybe_yield(node, option, result) do
      complex = option.to_complex
      next result.refute(node.coordinator.internal_error(:not_iterable)) unless complex.iterable?

      iterable = node.child(path, result.iterable(commit: commit), option: complex)
      updated, meta = reduce(iterable, block)

      [node.merge_strategy.transform_result(updated.navigable, node, path), meta]
    end
  end
end

.reduce(iterable, block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/lite/validation/validator/node/implementation/iteration/iterator.rb', line 30

def self.reduce(iterable, block)
  iterable.send(:option).reduce([iterable.result, iterable.context]) do |(result, context), (value, key)|
    break [result, context] if result.refuted?

    result.navigate(key) do |key_result|
      block.call(iterable, key, value, key_result, context)
    end
  end
end

Instance Method Details

#satisfy(using: nil, severity: :dispute, commit: false, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lite/validation/validator/node/implementation/iteration/iterator.rb', line 69

def satisfy(using: nil, severity: :dispute, commit: false, &block)
  predicate = Predication
              .resolve_predicate(using, from, parent.context, block)
              .definite
              .send(severity)

  self.class.iterate(
    parent,
    path,
    from,
    strategy,
    self.commit
  ) do |iterable, _key, value, result, context|
    Validation.validate(
      iterable.coordinator,
      value,
      result,
      context,
      commit,
      predicate
    )
  end
end

#validate(commit: false, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lite/validation/validator/node/implementation/iteration/iterator.rb', line 49

def validate(commit: false, &block)
  self.class.iterate(
    parent,
    path,
    from,
    strategy,
    self.commit
  ) do |iterable, _key, value, result, context|
    updated = Validation.validate(
      iterable.coordinator,
      value,
      result,
      context,
      commit,
      block
    )
    [updated, context]
  end
end