Class: Dry::Types::Nominal
- Inherits:
-
Object
- Object
- Dry::Types::Nominal
- 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
- #options ⇒ Hash included from Options readonly private
- #primitive ⇒ Class readonly
Class Method Summary collapse
- .[](primitive) ⇒ Type private
Instance Method Summary collapse
-
#&(other) ⇒ Intersection, Intersection::Constrained
included
from Builder
private
Compose two types into an Intersection type.
-
#>(other) ⇒ Implication, Implication::Constrained
included
from Builder
private
Compose two types into an Implication type.
-
#call(input = Undefined, &block) ⇒ Object
(also: #[])
included
from Type
Apply type to a value.
- #call_safe(input) ⇒ BasicObject private
- #call_unsafe(input) ⇒ BasicObject private
- #coerce(input, &_block) ⇒ Object private
-
#constrained ⇒ Constrained
included
from Builder
Turn a type into a constrained type.
- #constrained? ⇒ false
- #constrained_type ⇒ Class included from Builder private
-
#constructor(constructor = nil, **options, &block) ⇒ Constructor
(also: #append, #prepend, #>>, #<<)
included
from Builder
Define a constructor for the type.
- #constructor_type ⇒ Class included from Builder private
-
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default
included
from Builder
Turn a type into a type with a default value.
- #default? ⇒ false
-
#enum(*values) ⇒ Enum
included
from Builder
Define an enum on top of the existing type.
- #failure(input, error) ⇒ Result::Failure
-
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor
included
from Builder
Use the given value on type mismatch.
-
#initialize(primitive, **options) ⇒ Nominal
constructor
private
A new instance of Nominal.
-
#lax ⇒ Nominal
Return self.
-
#maybe ⇒ Maybe
included
from Builder
Turn a type into a maybe type.
- #meta(data = Undefined) ⇒ Object included from Meta
- #name ⇒ String
-
#optional ⇒ Sum
included
from Builder
Turn a type into an optional type.
- #optional? ⇒ false
-
#primitive?(value) ⇒ Boolean
Checks whether value is of a #primitive class.
-
#pristine ⇒ Dry::Types::Type
included
from Meta
Resets meta.
- #success(input) ⇒ Result::Success
-
#to_ast(meta: true) ⇒ Array
Return AST representation of a type nominal.
-
#to_proc ⇒ Proc
Wrap the type with a proc.
- #to_s ⇒ String (also: #inspect) included from Printable private
- #try(input) {|failure| ... } ⇒ Result, ...
- #try_coerce(input) ⇒ Object private
-
#valid?(input = Undefined) ⇒ Boolean
(also: #===)
included
from Type
private
Whether a value is a valid member of the type.
- #with(**options) ⇒ Type included from Meta
-
#|(other) ⇒ Sum, Sum::Constrained
included
from Builder
private
Compose two types into a Sum type.
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.
42 43 44 45 46 |
# File 'lib/dry/types/nominal.rb', line 42 def initialize(primitive, **) super @primitive = primitive freeze end |
Instance Attribute Details
#options ⇒ Hash (readonly) Originally defined in module Options
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.
#primitive ⇒ Class (readonly)
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.
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
#&(other) ⇒ Intersection, Intersection::Constrained Originally defined in module Builder
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.
Compose two types into an Intersection type
#>(other) ⇒ Implication, Implication::Constrained Originally defined in module Builder
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.
Compose two types into an Implication type
#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: [] Originally defined in module Type
Apply type to a value
#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.
90 91 92 |
# File 'lib/dry/types/nominal.rb', line 90 def call_safe(input) input end |
#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.
81 82 83 |
# File 'lib/dry/types/nominal.rb', line 81 def call_unsafe(input) input end |
#coerce(input, &_block) ⇒ 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.
139 140 141 142 143 144 145 146 147 |
# File 'lib/dry/types/nominal.rb', line 139 def coerce(input, &_block) if primitive?(input) input elsif block_given? yield else raise CoercionError, "#{input.inspect} must be an instance of #{primitive}" end end |
#constrained ⇒ Constrained Originally defined in module Builder
Turn a type into a constrained type
#constrained? ⇒ false
65 66 67 |
# File 'lib/dry/types/nominal.rb', line 65 def constrained? false end |
#constrained_type ⇒ Class Originally defined in module Builder
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.
#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder
Define a constructor for the type
#constructor_type ⇒ Class Originally defined in module Builder
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.
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder
Turn a type into a type with a default value
#default? ⇒ false
58 59 60 |
# File 'lib/dry/types/nominal.rb', line 58 def default? false end |
#enum(*values) ⇒ Enum Originally defined in module Builder
Define an enum on top of the existing type
#failure(input, error) ⇒ Result::Failure
121 122 123 124 125 |
# File 'lib/dry/types/nominal.rb', line 121 def failure(input, error) raise ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError) Result::Failure.new(input, error) end |
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder
Use the given value on type mismatch
#lax ⇒ Nominal
Return self. Nominal types are lax by definition
181 182 183 |
# File 'lib/dry/types/nominal.rb', line 181 def lax self end |
#name ⇒ String
51 52 53 |
# File 'lib/dry/types/nominal.rb', line 51 def name primitive.name end |
#optional? ⇒ false
72 73 74 |
# File 'lib/dry/types/nominal.rb', line 72 def optional? false end |
#primitive?(value) ⇒ Boolean
Checks whether value is of a #primitive class
134 135 136 |
# File 'lib/dry/types/nominal.rb', line 134 def primitive?(value) value.is_a?(primitive) end |
#pristine ⇒ Dry::Types::Type Originally defined in module Meta
Resets meta
#success(input) ⇒ Result::Success
112 113 114 |
# File 'lib/dry/types/nominal.rb', line 112 def success(input) Result::Success.new(input) end |
#to_ast(meta: true) ⇒ Array
Return AST representation of a type nominal
172 173 174 |
# File 'lib/dry/types/nominal.rb', line 172 def to_ast(meta: true) [:nominal, [primitive, ? self. : EMPTY_HASH]] end |
#to_proc ⇒ Proc
Wrap the type with a proc
190 191 192 |
# File 'lib/dry/types/nominal.rb', line 190 def to_proc ALWAYS end |
#to_s ⇒ String Also known as: inspect Originally defined in module Printable
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.
#try(input) {|failure| ... } ⇒ Result, ...
103 104 105 |
# File 'lib/dry/types/nominal.rb', line 103 def try(input) success(input) end |
#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.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/dry/types/nominal.rb', line 150 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 |
#valid?(input = Undefined) ⇒ Boolean Also known as: === Originally defined in module 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.
Whether a value is a valid member of the type
#|(other) ⇒ Sum, Sum::Constrained Originally defined in module Builder
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.
Compose two types into a Sum type