Class: Alchemrest::Transforms::FromType
- Inherits:
-
Morpher::Transform
- Object
- Morpher::Transform
- Alchemrest::Transforms::FromType
show all
- Includes:
- Adamantium::Mutable
- Defined in:
- lib/alchemrest/transforms/from_type.rb,
lib/alchemrest/transforms/from_type/empty_to_type_transform_registry.rb
Overview
Base class for transforms where we want to validate that the input is a particular type before operating on it. Additional validations can be chained off by use of the #where method. On success this transform will return the input, unchanged. For a transform that will actually change the input into something else, you can call #to and pass a block which will return a ‘ToType` transform that will run this transform, and then your block.
Defined Under Namespace
Classes: EmptyToTypeTransformRegistry
Constant Summary
collapse
- DEFAULTS =
{ constraints: [] }.freeze
Instance Method Summary
collapse
Constructor Details
#initialize(args) ⇒ FromType
Returns a new instance of FromType.
15
16
17
|
# File 'lib/alchemrest/transforms/from_type.rb', line 15
def initialize(args)
super(**DEFAULTS.merge(args))
end
|
Instance Method Details
#array ⇒ Object
42
43
44
|
# File 'lib/alchemrest/transforms/from_type.rb', line 42
def array
Typed.new(transform: super(), output_type: output_type.with(sorbet_type: T::Array[output_type.sorbet_type]))
end
|
#call(input) ⇒ Object
27
28
29
|
# File 'lib/alchemrest/transforms/from_type.rb', line 27
def call(input)
transform.call(input)
end
|
#maybe ⇒ Object
46
47
48
|
# File 'lib/alchemrest/transforms/from_type.rb', line 46
def maybe
Typed.new(transform: super(), output_type: output_type.with(sorbet_type: T.nilable(output_type.sorbet_type)))
end
|
#output_type ⇒ Object
19
20
21
|
# File 'lib/alchemrest/transforms/from_type.rb', line 19
def output_type
OutputType.new(sorbet_type: type, constraints:)
end
|
#output_type_name ⇒ Object
23
24
25
|
# File 'lib/alchemrest/transforms/from_type.rb', line 23
def output_type_name
type.name
end
|
#to(type, &block) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/alchemrest/transforms/from_type.rb', line 31
def to(type, &block)
if block.nil?
to_type_transform_registry.resolve(type)
else
ToType.using(to: type, from: self, &block)
end
rescue Alchemrest::NoRegisteredTransformError
raise ArgumentError,
"No transform registered to transform #{output_type.sorbet_type} to #{type}. Perhaps you should use the block form?"
end
|