class Dry::Types::Constructor
def self.new(input, **options, &block)
-
block
(#call, nil
) -- -
options
(Hash
) -- -
input
(Builder, Object
) --
def self.new(input, **options, &block) type = input.is_a?(Builder) ? input : Definition.new(input) super(type, **options, &block) end
def call(input)
-
(Object)
-
Parameters:
-
input
(Object
) --
def call(input) type[fn[input]] end
def constrained_type
-
(Class)
-
def constrained_type Constrained::Coercible end
def constructor(new_fn = nil, **options, &block)
-
(Constructor)
-
Parameters:
-
block
(#call, nil
) -- -
options
(Hash
) -- -
new_fn
(#call, nil
) --
def constructor(new_fn = nil, **options, &block) left = new_fn || block right = fn with(options.merge(fn: -> input { left[right[input]] })) end
def default?
def default? type.default? end
def initialize(type, **options, &block)
-
block
(#call, nil
) -- -
options
(Hash
) -- -
type
(Type
) --
def initialize(type, **options, &block) @type = type @fn = options.fetch(:fn, block) raise ArgumentError, 'Missing constructor block' if fn.nil? super(type, **options, fn: fn) end
def method_missing(meth, *args, &block)
-
block
(#call, nil
) -- -
args
(Array
) -- -
meth
(Symbol
) --
def method_missing(meth, *args, &block) if type.respond_to?(meth) response = type.__send__(meth, *args, &block) if response.kind_of?(Builder) self.class.new(response, options) else response end else super end end
def name
-
(String)
-
def name type.name end
def primitive
-
(Class)
-
def primitive type.primitive end
def register_fn(fn)
def register_fn(fn) Dry::Types::FnContainer.register(fn) end
def respond_to_missing?(meth, include_private = false)
-
(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)
- See: Definition#to_ast -
Other tags:
- Api: - public
def to_ast(meta: true) [:constructor, [type.to_ast(meta: meta), register_fn(fn), meta ? self.meta : EMPTY_HASH]] end
def try(input, &block)
-
(Object)
- if block given and try fails -
(Logic::Result, Types::Result)
-
Parameters:
-
block
(#call, nil
) -- -
input
(Object
) --
def try(input, &block) type.try(fn[input], &block) rescue TypeError, ArgumentError => e failure(input, e.message) end
def valid?(value)
-
(Boolean)
-
Parameters:
-
value
(Object
) --
def valid?(value) constructed_value = fn[value] rescue NoMethodError, TypeError, ArgumentError false else type.valid?(constructed_value) end