Class: Dry::Types::Constructor
- Defined in:
- lib/dry/types/constructor.rb,
lib/dry/types/constructor/wrapper.rb,
lib/dry/types/constructor/function.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Wrapper Classes: Function
Instance Attribute Summary collapse
- #fn ⇒ #call readonly
- #options ⇒ Hash included from Options readonly private
- #type ⇒ Type readonly
Class Method Summary collapse
- .[](type, fn:, **options) ⇒ Object
- .new(input, fn: Undefined, **options, &block) ⇒ Object
- .wrapper_type ⇒ Object private
Instance Method Summary collapse
- #call_safe(input) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
- #constrained_type ⇒ Class private
-
#constructor(new_fn = nil, **options, &block) ⇒ Constructor
(also: #append, #>>)
Build a new constructor by appending a block to the coercion function.
-
#initialize(type, fn: nil, **options) ⇒ Constructor
constructor
private
Instantiate a new constructor type instance.
-
#lax ⇒ Lax
Build a lax type.
-
#prepend(new_fn = nil, **options, &block) ⇒ Constructor
(also: #<<)
Build a new constructor by prepending a block to the coercion function.
- #to_ast(meta: true) ⇒ Object
-
#to_proc ⇒ Proc
Wrap the type with a proc.
- #try(input, &block) ⇒ Logic::Result, ...
Constructor Details
#initialize(type, fn: nil, **options) ⇒ Constructor
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.
Instantiate a new constructor type instance
62 63 64 65 66 67 |
# File 'lib/dry/types/constructor.rb', line 62 def initialize(type, fn: nil, **) @type = type @fn = fn super(type, **, fn: fn) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
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.
Delegates missing methods to #type
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/dry/types/constructor.rb', line 184 def method_missing(method, *args, &block) if type.respond_to?(method) response = type.public_send(method, *args, &block) if response.is_a?(Type) && response.instance_of?(type.class) response.constructor_type[response, **] else response end else super end end |
Instance Attribute Details
Class Method Details
.[](type, fn:, **options) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/dry/types/constructor.rb', line 35 def self.[](type, fn:, **) function = Function[fn] if function.wrapper? wrapper_type.new(type, fn: function, **) else new(type, fn: function, **) end end |
.new(input, fn: Undefined, **options, &block) ⇒ Object
25 26 27 28 |
# File 'lib/dry/types/constructor.rb', line 25 def self.new(input, fn: Undefined, **, &block) type = input.is_a?(Builder) ? input : Nominal.new(input) super(type, **, fn: Function[Undefined.default(fn, block)]) end |
.wrapper_type ⇒ 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.
46 47 48 49 50 51 52 53 |
# File 'lib/dry/types/constructor.rb', line 46 def self.wrapper_type @wrapper_type ||= if self < Wrapper self else const_set(:Wrapping, ::Class.new(self).include(Wrapper)) end end |
Instance Method Details
#call_safe(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.
72 73 74 75 |
# File 'lib/dry/types/constructor.rb', line 72 def call_safe(input) coerced = fn.(input) { |output = input| return yield(output) } type.call_safe(coerced) { |output = coerced| yield(output) } end |
#call_unsafe(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.
80 81 82 |
# File 'lib/dry/types/constructor.rb', line 80 def call_unsafe(input) type.call_unsafe(fn.(input)) end |
#constrained_type ⇒ Class
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.
124 125 126 |
# File 'lib/dry/types/constructor.rb', line 124 def constrained_type Constrained::Coercible end |
#constructor(new_fn = nil, **options, &block) ⇒ Constructor Also known as: append, >>
Build a new constructor by appending a block to the coercion function
109 110 111 112 113 114 115 116 117 |
# File 'lib/dry/types/constructor.rb', line 109 def constructor(new_fn = nil, **, &block) next_fn = Function[new_fn || block] if next_fn.wrapper? self.class.wrapper_type.new(with(**), fn: next_fn) else with(**, fn: fn >> next_fn) end end |
#lax ⇒ Lax
Build a lax type
153 154 155 |
# File 'lib/dry/types/constructor.rb', line 153 def lax Lax.new(constructor_type[type.lax, **]) end |
#prepend(new_fn = nil, **options, &block) ⇒ Constructor Also known as: <<
Build a new constructor by prepending a block to the coercion function
144 145 146 |
# File 'lib/dry/types/constructor.rb', line 144 def prepend(new_fn = nil, **, &block) with(**, fn: fn << (new_fn || block)) end |
#to_ast(meta: true) ⇒ Object
131 132 133 |
# File 'lib/dry/types/constructor.rb', line 131 def to_ast(meta: true) [:constructor, [type.to_ast(meta: ), fn.to_ast]] end |
#to_proc ⇒ Proc
Wrap the type with a proc
162 163 164 |
# File 'lib/dry/types/constructor.rb', line 162 def to_proc proc { |value| self.(value) } end |
#try(input, &block) ⇒ Logic::Result, ...
91 92 93 94 95 96 97 98 |
# File 'lib/dry/types/constructor.rb', line 91 def try(input, &block) value = fn.(input) rescue CoercionError => e failure = failure(input, e) block_given? ? yield(failure) : failure else type.try(value, &block) end |