class Dry::Types::Array::Member

Member arrays define their member type that is applied to each element

@api public

Attributes

member[R]

@return [Type]

Public Class Methods

new(primitive, **options) click to toggle source

@param [Class] primitive @param [Hash] options

@option options [Type] :member

@api private

Calls superclass method
# File lib/dry/types/array/member.rb, line 19
def initialize(primitive, **options)
  @member = options.fetch(:member)
  super
end

Public Instance Methods

call_safe(input) { |result| ... } click to toggle source

@param [Object] input @return [Array]

@api private

# File lib/dry/types/array/member.rb, line 45
def call_safe(input)
  if primitive?(input)
    failed = false

    result = input.each_with_object([]) do |el, output|
      coerced = member.call_safe(el) { |out = el|
        failed = true
        out
      }

      output << coerced unless Undefined.equal?(coerced)
    end

    failed ? yield(result) : result
  else
    yield
  end
end
call_unsafe(input) click to toggle source

@param [Object] input

@return [Array]

@api private

Calls superclass method
# File lib/dry/types/array/member.rb, line 29
def call_unsafe(input)
  if primitive?(input)
    input.each_with_object([]) do |el, output|
      coerced = member.call_unsafe(el)

      output << coerced unless Undefined.equal?(coerced)
    end
  else
    super
  end
end
constructor_type() click to toggle source

@api private

# File lib/dry/types/array/member.rb, line 116
def constructor_type
  ::Dry::Types::Array::Constructor
end
lax() click to toggle source

Build a lax type

@return [Lax]

@api public

# File lib/dry/types/array/member.rb, line 100
def lax
  Lax.new(Member.new(primitive, **options, member: member.lax, meta: meta))
end
to_ast(meta: true) click to toggle source

@see Nominal#to_ast

@api public

# File lib/dry/types/array/member.rb, line 107
def to_ast(meta: true)
  if member.respond_to?(:to_ast)
    [:array, [member.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]]
  else
    [:array, [member, meta ? self.meta : EMPTY_HASH]]
  end
end
try(input) { |failure| ... } click to toggle source

@param [Array, Object] input @param [#call,nil] block

@yieldparam [Failure] failure @yieldreturn [Result]

@return [Result,Logic::Result]

@api public

# File lib/dry/types/array/member.rb, line 73
def try(input, &block) # rubocop:disable Metrics/PerceivedComplexity
  if primitive?(input)
    output = []

    result = input.map { |el| member.try(el) }
    result.each do |r|
      output << r.input unless Undefined.equal?(r.input)
    end

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