class Dry::Types::Nominal

@api public
Use these types for annotations and the base for building more complex types on top of them.
Nominal types define a primitive class and do not apply any constructors or constraints

def self.[](primitive)

Other tags:
    Api: - private

Returns:
  • (Type) -

Parameters:
  • primitive (Class) --
def self.[](primitive)
  if primitive == ::Array
    Types::Array
  elsif primitive == ::Hash
    Types::Hash
  else
    self
  end
end

def call_safe(input, &) = input

Other tags:
    Api: - private

Returns:
  • (BasicObject) -

Parameters:
  • input (BasicObject) --
def call_safe(input, &) = input

def call_unsafe(input) = input

Other tags:
    Api: - private

Returns:
  • (BasicObject) -

Parameters:
  • input (BasicObject) --
def call_unsafe(input) = input

def coerce(input, &)

Other tags:
    Api: - private
def coerce(input, &)
  if primitive?(input)
    input
  elsif block_given?
    yield
  else
    raise CoercionError, "#{input.inspect} must be an instance of #{primitive}"
  end
end

def constrained? = false

Other tags:
    Api: - public

Returns:
  • (false) -
def constrained? = false

def default? = false

Other tags:
    Api: - public

Returns:
  • (false) -
def default? = false

def failure(input, error)

Other tags:
    Api: - public

Returns:
  • (Result::Failure) -

Parameters:
  • () --
def failure(input, error)
  raise ::ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError)
  Result::Failure.new(input, error)
end

def initialize(primitive, **options)

Other tags:
    Api: - private

Parameters:
  • options (Hash) --
  • primitive (Type, Class) --
def initialize(primitive, **options)
  super
  @primitive = primitive
  freeze
end

def lax = self

Other tags:
    Api: - public

Returns:
  • (Nominal) -
def lax = self

def name = primitive.name

Other tags:
    Api: - public

Returns:
  • (String) -
def name = primitive.name

def optional? = false

Other tags:
    Api: - public

Returns:
  • (false) -
def optional? = false

def primitive?(value) = value.is_a?(primitive)

Other tags:
    Api: - public

Returns:
  • (Boolean) -

Parameters:
  • value (Object) --
def primitive?(value) = value.is_a?(primitive)

def success(input) = Result::Success.new(input)

Other tags:
    Api: - public

Returns:
  • (Result::Success) -

Parameters:
  • () --
def success(input) = Result::Success.new(input)

def to_ast(meta: true)

Other tags:
    Api: - public

Returns:
  • (Array) -
def to_ast(meta: true)
  [:nominal, [primitive, meta ? self.meta : EMPTY_HASH]]
end

def to_proc = ALWAYS

Other tags:
    Api: - public

Returns:
  • (Proc) -
def to_proc = ALWAYS

def try(input, &) = success(input)

Other tags:
    Api: - public

Returns:
  • (nil) - otherwise
  • (Result, Logic::Result) - when a block is not provided

Other tags:
    Yieldreturn: -

Other tags:
    Yieldparam: failure -

Parameters:
  • input (Object) --
def try(input, &) = success(input)

def try_coerce(input)

Other tags:
    Api: - private
def try_coerce(input)
  result = success(input)
  coerce(input) do
    result = failure(
      input,
      CoercionError.new("#{input.inspect} must be an instance of #{primitive}")
    )
  end
  if block_given?
    yield(result)
  else
    result
  end
end