Class: Alchemrest::Transforms::ToType
- Inherits:
-
Morpher::Transform
- Object
- Morpher::Transform
- Alchemrest::Transforms::ToType
show all
- Defined in:
- lib/alchemrest/transforms/to_type.rb,
lib/alchemrest/transforms/to_type/transforms_selector.rb,
lib/alchemrest/transforms/to_type/from_string_to_time_selector.rb
Overview
A class for taking in an input and transforming it so something else. To initialize this class you must provide a ‘FromType` transform that first validates that the input is actually transformable (via the `from:` param) and an array of transforms that will actualy do the transformation (via the `use:` param) If you want to run additional validations after transformation, you can use the `#where` method.
Defined Under Namespace
Classes: FromStringToTimeSelector, TransformsSelector
Constant Summary
collapse
- DEFAULTS =
{ constraints: [] }.freeze
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args) ⇒ ToType
Returns a new instance of ToType.
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/alchemrest/transforms/to_type.rb', line 17
def initialize(args)
super(**DEFAULTS.merge(args))
raise ArgumentError, ":to must be a Class" unless to.instance_of?(Class)
raise ArgumentError, ":from must be a FromType transform" unless from.is_a?(FromType)
unless use.instance_of?(::Array) && !use.empty? && use.all?(Morpher::Transform)
raise ArgumentError, ":use must be an array of Morpher::Transform instances"
end
end
|
Class Method Details
.using(args, &block) ⇒ Object
13
14
15
|
# File 'lib/alchemrest/transforms/to_type.rb', line 13
def self.using(args, &block)
new(**args, use: [Success.new(block)])
end
|
Instance Method Details
#all_constraints ⇒ Object
40
41
42
|
# File 'lib/alchemrest/transforms/to_type.rb', line 40
def all_constraints
[*from.constraints, *constraints]
end
|
#array ⇒ Object
56
57
58
|
# File 'lib/alchemrest/transforms/to_type.rb', line 56
def array
Typed.new(transform: super(), output_type: output_type.with(sorbet_type: T::Array[(output_type.sorbet_type)]))
end
|
#call(input) ⇒ Object
36
37
38
|
# File 'lib/alchemrest/transforms/to_type.rb', line 36
def call(input)
transform.call(input)
end
|
#constraints_for(type) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/alchemrest/transforms/to_type.rb', line 44
def constraints_for(type)
raise ArgumentError, "Must provide a Class" unless type.instance_of?(Class)
unless [self, from].map { |transform| transform.output_type.sorbet_type }.include?(type)
raise ArgumentError,
"`type` must be either the to type (#{output_type.sorbet_type}) or the " \
"from type (#{from.output_type.sorbet_type}), was #{type}"
end
[self, from].select { |transform| transform.output_type.sorbet_type == type }.flat_map(&:constraints)
end
|
#maybe ⇒ Object
60
61
62
|
# File 'lib/alchemrest/transforms/to_type.rb', line 60
def maybe
Typed.new(transform: super(), output_type: output_type.with(sorbet_type: T.nilable(output_type.sorbet_type)))
end
|
#output_type ⇒ Object
28
29
30
|
# File 'lib/alchemrest/transforms/to_type.rb', line 28
def output_type
OutputType.new(sorbet_type: to, constraints: all_constraints)
end
|
#using(transforms) ⇒ Object
32
33
34
|
# File 'lib/alchemrest/transforms/to_type.rb', line 32
def using(transforms)
with(use: transforms)
end
|