Module: Dry::Types::ReferenceHookToMatchTuple Private

Included in:
Dry::Types
Defined in:
lib/dry/types/tuple.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Patch for [] inferrer, that adds support for ‘tuple<type,types…,>` syntax to compose Tuple types.

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Dry::Types::Type

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.

Wraps over the original method.

Returns:

  • (Dry::Types::Type)

See Also:

  • Dry::Types.[]


236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/dry/types/tuple.rb', line 236

def [](name)
  type_map.fetch_or_store(name) do
    if tuple_match = name.match(TUPLE_TYPE_SPEC_REGEX)
      type_id, members = tuple_match[1..2]
      types = members.scan(TUPLE_MEMBERS_SCAN_REGEX).map do |(type, rest)|
        rest.nil? ? self[type] : [self[rest]]
      end

      super(type_id).of(*types)
    else
      super
    end
  end
end