Class: Dry::Logic::Operations::Each

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

Instance Attribute Summary

Attributes inherited from Unary

#rule

Attributes inherited from Abstract

#options, #rules

Instance Method Summary collapse

Methods inherited from Unary

#ast, #initialize, #to_s

Methods inherited from Abstract

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

Methods included from Dry::Logic::Operators

#and, #or, #then, #xor

Constructor Details

This class inherits a constructor from Dry::Logic::Operations::Unary

Instance Method Details

#[](arr) ⇒ Object



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

def [](arr)
  arr.map { |input| rule[input] }.all?
end

#call(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dry/logic/operations/each.rb', line 11

def call(input)
  results = input.map { |element| rule.(element) }
  success = results.all?(&:success?)

  Result.new(success, id) do
    failures = results
      .map
      .with_index { |result, idx| [:key, [idx, result.ast(input[idx])]] if result.failure? }
      .compact

    [:set, failures]
  end
end

#typeObject



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

def type
  :each
end