Sha256: 05978b962c1c24db4f5b8acaa044e1201587f5077a1536e20b4aa78fdbca26ee

Contents?: true

Size: 719 Bytes

Versions: 6

Compression:

Stored size: 719 Bytes

Contents

# frozen_string_literal: true

require "dry/logic/operations/unary"
require "dry/logic/result"

module Dry
  module Logic
    module Operations
      class Each < Unary
        def type
          :each
        end

        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

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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dry-logic-1.2.0 lib/dry/logic/operations/each.rb
dry-logic-1.1.1 lib/dry/logic/operations/each.rb
dry-logic-1.1.0 lib/dry/logic/operations/each.rb
dry-logic-1.0.8 lib/dry/logic/operations/each.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/dry-logic-1.0.7/lib/dry/logic/operations/each.rb
dry-logic-1.0.7 lib/dry/logic/operations/each.rb