Class: Dry::Types::Tuple

Inherits:
Array
  • 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) ⇒ 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: meta)
end

Class Method Details

.Tuple.build(*fixed_types, rest_type) ⇒ Dry::Types::Tuple

Build a tuple type.

Parameters:

  • fixed_types (Array<Dry::Types::Type>)
  • rest_type (Array(Dry::Types::Type))

Returns:

See Also:



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

Parameters:

  • types (Array<Dry::Types::Type>)

Returns:

  • (Dry::Core::Constants::Undefined, Dry::Types::Type)

See Also:



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

Parameters:

  • types (Array<Dry::Types::Type>)

Returns:

  • (Dry::Core::Constants::Undefined, Dry::Types::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.

Parameters:

  • tuple (Array)

Returns:

  • (Array)


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.

Parameters:

  • tuple (Array)

Returns:

  • (Array)


98
99
100
101
102
# File 'lib/dry/types/tuple.rb', line 98

def call_unsafe(tuple)
  try(tuple) { |failure|
    raise MapError, failure.error.message
  }.input
end

#constrained?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/dry/types/tuple.rb', line 150

def constrained?
  rest_type&.constrained? || options[:types_index].each_value.any?(&:constrained?)
end

#fixed_typesArray<Type>

Returns:

  • (Array<Type>)


75
76
77
# File 'lib/dry/types/tuple.rb', line 75

def fixed_types
  options[:types_index].values
end

#laxLax

Build a lax type

Returns:

  • (Lax)


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

#nameString

Returns:

  • (String)


89
90
91
# File 'lib/dry/types/tuple.rb', line 89

def name
  "Tuple"
end

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

Returns:

See Also:



61
62
63
# File 'lib/dry/types/tuple.rb', line 61

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

#rest_typeType

Returns:

  • (Type)


82
83
84
# File 'lib/dry/types/tuple.rb', line 82

def rest_type
  options[:types_index].default
end

#to_ast(meta: true) ⇒ Array

Returns An AST representation.

Parameters:

  • meta (Boolean) (defaults to: true)

    Whether to dump the meta to the AST

Returns:

  • (Array)

    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: meta) }]
  structure << [rest_type.to_ast(meta: meta)] unless rest_type.nil?
  structure << meta ? self.meta : EMPTY_HASH
  [:tuple, structure]
end

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

Parameters:

  • tuple (Array)

Yields:

  • (result)

Returns:

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

Returns:

  • (Hash)


68
69
70
# File 'lib/dry/types/tuple.rb', line 68

def types_index
  options[:types_index]
end