Class: Dry::Types::Array::Member

Inherits:
Dry::Types::Array show all
Defined in:
lib/dry/types/array/member.rb

Instance Attribute Summary collapse

Attributes inherited from Definition

#options, #primitive

Attributes included from Options

#options

Instance Method Summary collapse

Methods inherited from Definition

[], #constrained?, #default?, #failure, #name, #primitive?, #result, #success

Methods included from Builder

#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Options

#meta, #with

Constructor Details

#initialize(primitive, options = {}) ⇒ Member

Returns a new instance of Member.



7
8
9
10
# File 'lib/dry/types/array/member.rb', line 7

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

Instance Attribute Details

#memberObject (readonly)

Returns the value of attribute member.



5
6
7
# File 'lib/dry/types/array/member.rb', line 5

def member
  @member
end

Instance Method Details

#call(input, meth = :call) ⇒ Object Also known as: []



12
13
14
# File 'lib/dry/types/array/member.rb', line 12

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

#try(input, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dry/types/array/member.rb', line 21

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

#valid?(type) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/dry/types/array/member.rb', line 17

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