Class: Dry::Types::Nominal

Inherits:
Object
  • Object
show all
Includes:
Builder, Meta, Options, Printable, Type
Defined in:
lib/dry/types/nominal.rb

Overview

Nominal types define a primitive class and do not apply any constructors or constraints

Use these types for annotations and the base for building more complex types on top of them.

Direct Known Subclasses

AnyClass, Array, Constructor, Hash, Map

Constant Summary collapse

ALWAYS =
proc { true }

Instance Attribute Summary collapse

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #maybe, #optional, #|

Methods included from Meta

#meta, #pristine, #with

Methods included from Options

#with

Methods included from Type

#call, #valid?

Constructor Details

#initialize(primitive, **options) ⇒ Nominal

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Nominal.

Parameters:

  • primitive (Type, Class)
  • options (Hash)


42
43
44
45
46
# File 'lib/dry/types/nominal.rb', line 42

def initialize(primitive, **options)
  super
  @primitive = primitive
  freeze
end

Instance Attribute Details

#primitiveClass (readonly)

Returns:

  • (Class)


19
20
21
# File 'lib/dry/types/nominal.rb', line 19

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • primitive (Class)

Returns:



26
27
28
29
30
31
32
33
34
# File 'lib/dry/types/nominal.rb', line 26

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

Instance Method Details

#call_safe(input) ⇒ BasicObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • input (BasicObject)

Returns:

  • (BasicObject)


80
# File 'lib/dry/types/nominal.rb', line 80

def call_safe(input, &) = input

#call_unsafe(input) ⇒ BasicObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • input (BasicObject)

Returns:

  • (BasicObject)


73
# File 'lib/dry/types/nominal.rb', line 73

def call_unsafe(input) = input

#coerce(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



121
122
123
124
125
126
127
128
129
# File 'lib/dry/types/nominal.rb', line 121

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

#constrained?false

Returns:

  • (false)


61
# File 'lib/dry/types/nominal.rb', line 61

def constrained? = false

#default?false

Returns:

  • (false)


56
# File 'lib/dry/types/nominal.rb', line 56

def default? = false

#failure(input, error) ⇒ Result::Failure

Returns:

Raises:

  • (::ArgumentError)


105
106
107
108
109
# File 'lib/dry/types/nominal.rb', line 105

def failure(input, error)
  raise ::ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError)

  Result::Failure.new(input, error)
end

#laxNominal

Return self. Nominal types are lax by definition

Returns:



163
# File 'lib/dry/types/nominal.rb', line 163

def lax = self

#nameString

Returns:

  • (String)


51
# File 'lib/dry/types/nominal.rb', line 51

def name = primitive.name

#optional?false

Returns:

  • (false)


66
# File 'lib/dry/types/nominal.rb', line 66

def optional? = false

#primitive?(value) ⇒ Boolean

Checks whether value is of a #primitive class

Parameters:

  • value (Object)

Returns:

  • (Boolean)


118
# File 'lib/dry/types/nominal.rb', line 118

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

#success(input) ⇒ Result::Success

Returns:



98
# File 'lib/dry/types/nominal.rb', line 98

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

#to_ast(meta: true) ⇒ Array

Return AST representation of a type nominal

Returns:



154
155
156
# File 'lib/dry/types/nominal.rb', line 154

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

#to_procProc

Wrap the type with a proc

Returns:

  • (Proc)


170
# File 'lib/dry/types/nominal.rb', line 170

def to_proc = ALWAYS

#try(input) {|failure| ... } ⇒ Result, ...

Parameters:

  • input (Object)

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:

  • (Result, Logic::Result)

    when a block is not provided

  • (nil)

    otherwise



91
# File 'lib/dry/types/nominal.rb', line 91

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

#try_coerce(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dry/types/nominal.rb', line 132

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