Class: Dry::Types::Tuple
- Inherits:
-
Array
- Object
- Array
- Dry::Types::Tuple
- Defined in:
- lib/dry/types/tuple.rb
Overview
Class Method Summary collapse
-
.Tuple.build(*fixed_types, rest_type) ⇒ Dry::Types::Tuple
Build a tuple type.
-
.build_index(types) ⇒ Dry::Core::Constants::Undefined, Dry::Types::Type
Prepares types index for the Tuple.
- .build_unsplat(types) ⇒ Object private
-
.extract_rest(types) ⇒ Dry::Core::Constants::Undefined, Dry::Types::Type
Extracts and unwraps the rest type.
Instance Method Summary collapse
- #call_safe(tuple) ⇒ Array private
- #call_unsafe(tuple) ⇒ Array private
- #constrained? ⇒ Boolean
- #fixed_types ⇒ Array<Type>
-
#initialize(_primitive, types_index: EMPTY_HASH, meta: EMPTY_HASH) ⇒ Tuple
constructor
A new instance of Tuple.
-
#lax ⇒ Lax
Build a lax type.
- #name ⇒ String
- #of(*types) ⇒ Dry::Types::Tuple
- #rest_type ⇒ Type
-
#to_ast(meta: true) ⇒ Array
An AST representation.
- #try(tuple) {|result| ... } ⇒ Result
- #types_index ⇒ Hash
Constructor Details
#initialize(_primitive, types_index: EMPTY_HASH, meta: EMPTY_HASH) ⇒ Tuple
Returns a new instance of Tuple.
55 56 57 |
# File 'lib/dry/types/tuple.rb', line 55 def initialize(_primitive, types_index: EMPTY_HASH, meta: EMPTY_HASH) super(_primitive, types_index: types_index, meta: ) end |
Class Method Details
.Tuple.build(*fixed_types, rest_type) ⇒ Dry::Types::Tuple
Build a tuple type.
20 21 22 |
# File 'lib/dry/types/tuple.rb', line 20 def self.build(*types) build_unsplat(types) end |
.build_index(types) ⇒ Dry::Core::Constants::Undefined, Dry::Types::Type
Prepares types index for the Tuple
33 34 35 36 37 38 |
# File 'lib/dry/types/tuple.rb', line 33 def self.build_index(types) rest_type = extract_rest(types) types_index = ::Hash[types.size.times.zip(types)] types_index.default = Undefined.default(rest_type, nil) types_index end |
.build_unsplat(types) ⇒ 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.
25 26 27 |
# File 'lib/dry/types/tuple.rb', line 25 def self.build_unsplat(types) new(::Array, types_index: build_index(types)) end |
.extract_rest(types) ⇒ Dry::Core::Constants::Undefined, Dry::Types::Type
Extracts and unwraps the rest type
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dry/types/tuple.rb', line 43 def self.extract_rest(types) if !types[-1].is_a?(::Array) return Undefined end if types[-1].size > 1 raise ArgumentError, "rest_type should be an Array with single element to apply to the rest of items: #{types[-1]}" end types.pop[0] end |
Instance Method Details
#call_safe(tuple) ⇒ 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.
109 110 111 |
# File 'lib/dry/types/tuple.rb', line 109 def call_safe(tuple) try(tuple) { return yield }.input end |
#call_unsafe(tuple) ⇒ 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.
98 99 100 101 102 |
# File 'lib/dry/types/tuple.rb', line 98 def call_unsafe(tuple) try(tuple) { |failure| raise MapError, failure.error. }.input end |
#constrained? ⇒ Boolean
150 151 152 |
# File 'lib/dry/types/tuple.rb', line 150 def constrained? rest_type&.constrained? || [:types_index].each_value.any?(&:constrained?) end |
#fixed_types ⇒ Array<Type>
75 76 77 |
# File 'lib/dry/types/tuple.rb', line 75 def fixed_types [:types_index].values end |
#lax ⇒ Lax
Build a lax type
130 131 132 133 |
# File 'lib/dry/types/tuple.rb', line 130 def lax lax_types_index = types_index.transform_values(&:lax) Lax.new(Tuple.new(primitive, **, types_index: lax_types_index, meta: )) end |
#name ⇒ String
89 90 91 |
# File 'lib/dry/types/tuple.rb', line 89 def name "Tuple" end |
#of(*types) ⇒ Dry::Types::Tuple
61 62 63 |
# File 'lib/dry/types/tuple.rb', line 61 def of(*types) with(types_index: self.class.build_index(types)) end |
#rest_type ⇒ Type
82 83 84 |
# File 'lib/dry/types/tuple.rb', line 82 def rest_type [:types_index].default end |
#to_ast(meta: true) ⇒ Array
Returns An AST representation.
140 141 142 143 144 145 |
# File 'lib/dry/types/tuple.rb', line 140 def to_ast(meta: true) structure = [*fixed_types.map { |type| type.to_ast(meta: ) }] structure << [rest_type.to_ast(meta: )] unless rest_type.nil? structure << ? self. : EMPTY_HASH [:tuple, structure] end |
#try(tuple) {|result| ... } ⇒ Result
118 119 120 121 122 123 |
# File 'lib/dry/types/tuple.rb', line 118 def try(tuple) result = coerce(tuple) return result if result.success? || !block_given? yield(result) end |
#types_index ⇒ Hash
68 69 70 |
# File 'lib/dry/types/tuple.rb', line 68 def types_index [:types_index] end |