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

#meta, #options

Instance Method Summary collapse

Methods inherited from Definition

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

Methods included from Builder

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

Methods included from Options

#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



17
18
19
20
21
22
23
24
25
26
# File 'lib/dry/types/array/member.rb', line 17

def try(input, &block)
  result = call(input, :try)

  if result.all?(&:success?)
    success(result.map(&:input))
  else
    failure = failure(input, result.select(&:failure?))
    block ? yield(failure) : failure
  end
end