Sha256: 99aa9eaefbfeed60e30bbee6f4c6e5cfe823801e37e13fa2bf9d6880820e20f6

Contents?: true

Size: 1004 Bytes

Versions: 3

Compression:

Stored size: 1004 Bytes

Contents

module Dry
  module Types
    class Array < Definition
      class Member < Array
        attr_reader :member

        def initialize(primitive, options = {})
          @member = options.fetch(:member)
          super
        end

        def call(input, meth = :call)
          input.map { |el| member.__send__(meth, el) }
        end
        alias_method :[], :call

        def valid?(type)
          super && type.all? { |el| member.valid?(el) }
        end

        def try(input, &block)
          if input.is_a?(::Array)
            result = call(input, :try)
            output = result.map(&:input)

            if result.all?(&:success?)
              success(output)
            else
              failure = failure(output, result.select(&:failure?))
              block ? yield(failure) : failure
            end
          else
            failure = failure(input, "#{input} is not an array")
            block ? yield(failure) : failure
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-types-0.9.2 lib/dry/types/array/member.rb
dry-types-0.9.1 lib/dry/types/array/member.rb
dry-types-0.9.0 lib/dry/types/array/member.rb