Class: Dry::Types::Array::Member
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, #optional?, #primitive?, #result, #success
Methods included from Builder
#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|
Methods included from Options
#meta, #pristine, #with
Constructor Details
#initialize(primitive, options = {}) ⇒ Member
Returns a new instance of Member.
11
12
13
14
|
# File 'lib/dry/types/array/member.rb', line 11
def initialize(primitive, options = {})
@member = options.fetch(:member)
super
end
|
Instance Attribute Details
#member ⇒ Type
6
7
8
|
# File 'lib/dry/types/array/member.rb', line 6
def member
@member
end
|
Instance Method Details
#call(input, meth = :call) ⇒ Array
Also known as:
[]
19
20
21
|
# File 'lib/dry/types/array/member.rb', line 19
def call(input, meth = :call)
input.map { |el| member.__send__(meth, el) }
end
|
#to_ast(meta: true) ⇒ Object
55
56
57
|
# File 'lib/dry/types/array/member.rb', line 55
def to_ast(meta: true)
[:array, [member.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]]
end
|
#try(input, &block) {|failure| ... } ⇒ Result, Logic::Result
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/dry/types/array/member.rb', line 35
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?(value) ⇒ Boolean
26
27
28
|
# File 'lib/dry/types/array/member.rb', line 26
def valid?(value)
super && value.all? { |el| member.valid?(el) }
end
|