Class: Dry::Types::Tuple
- Inherits:
-
Nominal
- Object
- Nominal
- Dry::Types::Tuple
- Defined in:
- lib/dry/types/tuple.rb
Overview
Class Method Summary collapse
-
.self.build(*fixed_types, rest_type) ⇒ Dry::Types::Tuple
Build a tuple type.
-
.build_index(types) ⇒ Hash { Integer => Type }
Prepares types index for the Tuple.
- .coerce(types, **opts) ⇒ Object private
-
.extract_rest(types) ⇒ Undefined, ...
Extracts the rest type or types.
Instance Method Summary collapse
- #build_unsplat ⇒ Object
- #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, **opts) ⇒ self constructor
-
#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, **opts) ⇒ self
72 73 74 |
# File 'lib/dry/types/tuple.rb', line 72 def initialize(primitive, types_index: EMPTY_HASH, meta: EMPTY_HASH, **opts) super(primitive, **opts, types_index:, meta:) end |
Class Method Details
.self.build(*fixed_types, rest_type) ⇒ Dry::Types::Tuple
Build a tuple type.
24 25 26 |
# File 'lib/dry/types/tuple.rb', line 24 def self.build(*types, **opts) coerce(types, **opts) end |
.build_index(types) ⇒ Hash { Integer => Type }
Prepares types index for the Tuple
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dry/types/tuple.rb', line 32 def self.build_index(types) types_index = {} Undefined.map(extract_rest(types)) do |rest_type| types_index.default = rest_type end types.each_with_index do |type, index| types_index[index] = type end types_index end |
.coerce(types, **opts) ⇒ 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.
63 64 65 66 |
# File 'lib/dry/types/tuple.rb', line 63 def self.coerce(types, **opts) types_index = build_index(types) new(::Array, **opts, types_index:) end |
.extract_rest(types) ⇒ Undefined, ...
Note:
Destructive on input arrays.
Extracts the rest type or types. More than one types in list will be composed to Sum.
52 53 54 55 56 57 58 59 60 |
# File 'lib/dry/types/tuple.rb', line 52 def self.extract_rest(types) case types in *head, ::Array => rest if rest.size > 0 types.replace(head) rest.reduce(:|) else Undefined end end |
Instance Method Details
#build_unsplat ⇒ Object
68 |
# File 'lib/dry/types/tuple.rb', line 68 singleton_class.alias_method :build_unsplat, :coerce |
#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.
136 137 138 |
# File 'lib/dry/types/tuple.rb', line 136 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.
129 130 131 |
# File 'lib/dry/types/tuple.rb', line 129 def call_unsafe(tuple) try(tuple) { raise MapError, _1.error. }.input end |
#constrained? ⇒ Boolean
122 123 124 |
# File 'lib/dry/types/tuple.rb', line 122 def constrained? rest_type&.constrained? || types_index.each_value.any?(&:constrained?) end |
#fixed_types ⇒ Array<Type>
88 |
# File 'lib/dry/types/tuple.rb', line 88 def fixed_types = types_index.values |
#lax ⇒ Lax
Build a lax type
106 107 108 109 110 |
# File 'lib/dry/types/tuple.rb', line 106 def lax types_index = types_index().transform_values(&:lax) types_index.default = rest_type.lax if rest_type Lax.new(Tuple.new(primitive, **, types_index:, meta:)) end |
#name ⇒ String
94 |
# File 'lib/dry/types/tuple.rb', line 94 def name = 'Tuple' |
#of(*types) ⇒ Dry::Types::Tuple
80 81 82 |
# File 'lib/dry/types/tuple.rb', line 80 def of(*types) with(types_index: self.class.build_index(types)) end |
#rest_type ⇒ Type
91 |
# File 'lib/dry/types/tuple.rb', line 91 def rest_type = types_index.default |
#to_ast(meta: true) ⇒ Array
114 115 116 117 118 119 |
# File 'lib/dry/types/tuple.rb', line 114 def to_ast(meta: true) structure = [*fixed_types.map { _1.to_ast(meta:) }] structure << [rest_type.to_ast(meta:)] unless rest_type.nil? structure << ? () : EMPTY_HASH [:tuple, structure] end |
#try(tuple) {|result| ... } ⇒ Result
98 99 100 101 102 |
# File 'lib/dry/types/tuple.rb', line 98 def try(tuple) result = coerce(tuple) return result if result.success? || !block_given? yield(result) end |
#types_index ⇒ Hash
85 |
# File 'lib/dry/types/tuple.rb', line 85 def types_index = [:types_index] |