class Dry::Types::Constructor
@api public
a new value. Coercion is a common use case for constructor types.
Constructor types apply a function to the input that is supposed to return
def self.[](type, fn:, **options)
- Api: - public
Parameters:
-
block
(#call, nil
) -- -
options
(Hash
) -- -
input
(Builder, Object
) --
def self.[](type, fn:, **options) function = Function[fn] if function.wrapper? wrapper_type.new(type, fn: function, **options) else new(type, fn: function, **options) end end
def self.new(input, fn: Undefined, **options, &block)
- Api: - public
Parameters:
-
block
(#call, nil
) -- -
options
(Hash
) -- -
input
(Builder, Object
) --
def self.new(input, fn: Undefined, **options, &block) type = input.is_a?(Builder) ? input : Nominal.new(input) super(type, **options, fn: Function[Undefined.default(fn, block)]) end
def self.wrapper_type
- Api: - private
def self.wrapper_type @wrapper_type ||= if self < Wrapper self else const_set(:Wrapping, ::Class.new(self).include(Wrapper)) end end
def call_safe(input)
- Api: - private
Returns:
-
(Object)
-
def call_safe(input) coerced = fn.(input) { |output = input| return yield(output) } type.call_safe(coerced) { |output = coerced| yield(output) } end
def call_unsafe(input) = type.call_unsafe(fn.(input))
- Api: - private
Returns:
-
(Object)
-
def call_unsafe(input) = type.call_unsafe(fn.(input))
def constrained_type = Constrained::Coercible
- Api: - private
Returns:
-
(Class)
-
def constrained_type = Constrained::Coercible
def constructor(new_fn = nil, **options, &block)
- Api: - public
Returns:
-
(Constructor)
-
Parameters:
-
block
(#call, nil
) -- -
options
(Hash
) -- -
new_fn
(#call, nil
) --
def constructor(new_fn = nil, **options, &block) next_fn = Function[new_fn || block] if next_fn.wrapper? self.class.wrapper_type.new(with(**options), fn: next_fn) else with(**options, fn: fn >> next_fn) end end
def initialize(type, fn: nil, **options)
- Api: - private
Parameters:
-
options
(Hash
) -- -
fn
(Function
) -- -
type
(Type
) --
def initialize(type, fn: nil, **options) @type = type @fn = fn super(type, **options, fn: fn) end
def lax = Lax.new(constructor_type[type.lax, **options])
- Api: - public
Returns:
-
(Lax)
-
def lax = Lax.new(constructor_type[type.lax, **options])
def method_missing(method, ...)
- Api: - private
Parameters:
-
block
(#call, nil
) -- -
args
(Array
) -- -
method
(Symbol
) --
def method_missing(method, ...) if type.respond_to?(method) response = type.public_send(method, ...) if response.is_a?(Type) && response.instance_of?(type.class) response.constructor_type[response, **options] else response end else super end end
def prepend(new_fn = nil, **options, &block)
- Api: - public
Returns:
-
(Constructor)
-
Parameters:
-
block
(#call, nil
) -- -
options
(Hash
) -- -
new_fn
(#call, nil
) --
def prepend(new_fn = nil, **options, &block) with(**options, fn: fn << (new_fn || block)) end
def respond_to_missing?(meth, include_private = false)
- Api: - private
Returns:
-
(Boolean)
-
Parameters:
-
include_private
(Boolean
) -- -
meth
(Symbol
) --
def respond_to_missing?(meth, include_private = false) super || type.respond_to?(meth) end
def to_ast(meta: true)
- Api: - public
Other tags:
- See: Nominal#to_ast -
def to_ast(meta: true) [:constructor, [type.to_ast(meta: meta), fn.to_ast]] end
def to_proc = proc { self.(_1) }
- Api: - public
Returns:
-
(Proc)
-
def to_proc = proc { self.(_1) }
def try(input, &)
- Api: - public
Returns:
-
(Object)
- if block given and try fails -
(Logic::Result, Types::Result)
-
Parameters:
-
block
(#call, nil
) -- -
input
(Object
) --
def try(input, &) value = fn.(input) rescue CoercionError => e failure = failure(input, e) block_given? ? yield(failure) : failure else type.try(value, &) end