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
- #namespace ⇒ String? readonly
- #primitive ⇒ Class readonly
Attributes included from Options
Class Method Summary collapse
- .[](primitive) ⇒ Type private
Instance Method Summary collapse
- #call_safe(input) ⇒ BasicObject private
- #call_unsafe(input) ⇒ BasicObject private
- #coerce(input) ⇒ Object private
- #constrained? ⇒ false
- #default? ⇒ false
- #failure(input, error) ⇒ Result::Failure
-
#initialize(primitive, **options) ⇒ Nominal
constructor
private
A new instance of Nominal.
-
#lax ⇒ Nominal
Return self.
- #name ⇒ String
- #optional? ⇒ false
-
#primitive?(value) ⇒ Boolean
Checks whether value is of a #primitive class.
- #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.
- #try(input) {|failure| ... } ⇒ Result, ...
- #try_coerce(input) ⇒ Object private
Methods included from Printable
Methods included from Builder
#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #maybe, #optional, #|
Methods included from Meta
Methods included from Options
Methods included from 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.
45 46 47 48 49 50 |
# File 'lib/dry/types/nominal.rb', line 45 def initialize(primitive, **) super @primitive = primitive @namespace = [:namespace] freeze end |
Instance Attribute Details
#namespace ⇒ String? (readonly)
22 23 24 |
# File 'lib/dry/types/nominal.rb', line 22 def namespace @namespace end |
#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.
29 30 31 32 33 34 35 36 37 |
# File 'lib/dry/types/nominal.rb', line 29 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.
84 |
# File 'lib/dry/types/nominal.rb', line 84 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.
77 |
# File 'lib/dry/types/nominal.rb', line 77 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.
125 126 127 128 129 130 131 132 133 |
# File 'lib/dry/types/nominal.rb', line 125 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
65 |
# File 'lib/dry/types/nominal.rb', line 65 def constrained? = false |
#default? ⇒ false
60 |
# File 'lib/dry/types/nominal.rb', line 60 def default? = false |
#failure(input, error) ⇒ Result::Failure
109 110 111 112 113 |
# File 'lib/dry/types/nominal.rb', line 109 def failure(input, error) raise ::ArgumentError, "error must be a CoercionError" unless error.is_a?(CoercionError) Result::Failure.new(input, error) end |
#lax ⇒ Nominal
Return self. Nominal types are lax by definition
167 |
# File 'lib/dry/types/nominal.rb', line 167 def lax = self |
#name ⇒ String
55 |
# File 'lib/dry/types/nominal.rb', line 55 def name = primitive.name |
#optional? ⇒ false
70 |
# File 'lib/dry/types/nominal.rb', line 70 def optional? = false |
#primitive?(value) ⇒ Boolean
Checks whether value is of a #primitive class
122 |
# File 'lib/dry/types/nominal.rb', line 122 def primitive?(value) = value.is_a?(primitive) |
#success(input) ⇒ Result::Success
102 |
# File 'lib/dry/types/nominal.rb', line 102 def success(input) = Result::Success.new(input) |
#to_ast(meta: true) ⇒ Array
Return AST representation of a type nominal
158 159 160 |
# File 'lib/dry/types/nominal.rb', line 158 def to_ast(meta: true) [:nominal, [primitive, namespace_ast, ()]] end |
#to_proc ⇒ Proc
Wrap the type with a proc
174 |
# File 'lib/dry/types/nominal.rb', line 174 def to_proc = ALWAYS |
#try(input) {|failure| ... } ⇒ Result, ...
95 |
# File 'lib/dry/types/nominal.rb', line 95 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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dry/types/nominal.rb', line 136 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 |