Class: Dry::Types::Tuple

Inherits:
Nominal
  • Object
show all
Defined in:
lib/dry/types/tuple.rb

Overview

Examples:

Types::ServiceArgs = Types.Tuple(
  Types::Params::Symbol,                                # --- positional types
  [Types::Params::Integer | Types::Coercible::String]   # --- [type for the rest items]
)
Types::ServiceArgs[['thumb', '300', '300', 'sample']]
# => [:thumb, 300, 300, "sample"]

Class Method Summary collapse

Instance Method Summary collapse

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.

See Also:

  • selfself.build_index


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

See Also:

  • selfself.extract_rest


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_unsplatObject



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.message }.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_typesArray<Type>



88
# File 'lib/dry/types/tuple.rb', line 88

def fixed_types = types_index.values

#laxLax

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, **options, types_index:, meta:))
end

#nameString



94
# File 'lib/dry/types/tuple.rb', line 94

def name = 'Tuple'

#of(*types) ⇒ Dry::Types::Tuple

See Also:



80
81
82
# File 'lib/dry/types/tuple.rb', line 80

def of(*types)
  with(types_index: self.class.build_index(types))
end

#rest_typeType



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 << meta ? meta() : EMPTY_HASH
  [:tuple, structure]
end

#try(tuple) {|result| ... } ⇒ Result

Yields:

  • (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_indexHash



85
# File 'lib/dry/types/tuple.rb', line 85

def types_index = options[:types_index]