Module: Dry::Tuple::StructClassInterface

Extended by:
HookExtendObject
Includes:
ClassDecorator
Defined in:
lib/dry/tuple.rb

Overview

Extracted due to make it possible to use this feature within Struct classes.

Examples:

extending Dry::Struct subclass


class SomeStruct < Dry::Struct
  attribute :some, Types::Integer
  attribute :with, Types::Hash
  extend ::Dry::Tuple::StructClassInterface
  auto_tuple :some, :with
end

Instance Method Summary collapse

Methods included from ClassDecorator

#call_safe, #call_unsafe, #new_from_tuple

Instance Method Details

#auto_tuple(*keys) ⇒ Object



138
139
140
141
142
# File 'lib/dry/tuple.rb', line 138

def auto_tuple(*keys)
  keys_order(keys_order | keys)
  index = schema.keys.map { |t| [t.name, t.type] }.to_h
  tuple Dry::Types::Tuple.build_unsplat(index.values_at(*keys_order))
end

#coerce_tuple(input) ⇒ Object



144
145
146
# File 'lib/dry/tuple.rb', line 144

def coerce_tuple(input)
  keys_order.zip(input).to_h
end

#try(input, &block) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/dry/tuple.rb', line 130

def try(input, &block)
  if input.is_a?(::Array)
    @tuple.try(input, &block)
  else
    super(input, &block)
  end
end