Class: Dry::Types::Array::Member
- Inherits:
-
Dry::Types::Array
- Object
- Nominal
- Dry::Types::Array
- Dry::Types::Array::Member
- Defined in:
- lib/dry/types/array/member.rb
Overview
Member arrays define their member type that is applied to each element
Constant Summary
Constants inherited from Nominal
Instance Attribute Summary collapse
- #member ⇒ Type readonly
Attributes inherited from Nominal
Attributes included from Options
Instance Method Summary collapse
- #call_safe(input) ⇒ Array private
- #call_unsafe(input) ⇒ Array private
- #constructor_type ⇒ Object private
-
#initialize(primitive, **options) ⇒ Member
constructor
private
A new instance of Member.
-
#lax ⇒ Lax
Build a lax type.
- #to_ast(meta: true) ⇒ Object
- #try(input, &block) {|failure| ... } ⇒ Result, Logic::Result
Methods inherited from Dry::Types::Array
Methods inherited from Nominal
[], #coerce, #constrained?, #default?, #failure, #name, #optional?, #primitive?, #success, #to_proc, #try_coerce
Methods included from Printable
Methods included from Builder
#&, #>, #constrained, #constrained_type, #constructor, #default, #enum, #fallback, #maybe, #optional, #|
Methods included from Meta
Methods included from Options
Methods included from Type
Constructor Details
#initialize(primitive, **options) ⇒ Member
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Member.
19 20 21 22 |
# File 'lib/dry/types/array/member.rb', line 19 def initialize(primitive, **) @member = .fetch(:member) super end |
Instance Attribute Details
#member ⇒ Type (readonly)
11 12 13 |
# File 'lib/dry/types/array/member.rb', line 11 def member @member end |
Instance Method Details
#call_safe(input) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 32 33 34 35 36 37 38 39 |
# 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 ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
116 117 118 |
# File 'lib/dry/types/array/member.rb', line 116 def constructor_type ::Dry::Types::Array::Constructor end |
#lax ⇒ Lax
Build a lax type
100 101 102 |
# File 'lib/dry/types/array/member.rb', line 100 def lax Lax.new(Member.new(primitive, **, member: member.lax, meta: )) end |
#to_ast(meta: true) ⇒ Object
107 108 109 110 111 112 113 |
# 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: ), ? self. : EMPTY_HASH]] else [:array, [member, ? self. : EMPTY_HASH]] end end |
#try(input, &block) {|failure| ... } ⇒ Result, Logic::Result
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 |