Class: Dry::Types::Constrained
- Inherits:
-
Object
- Object
- Dry::Types::Constrained
- Defined in:
- lib/dry/types/constrained.rb,
lib/dry/types/constrained/coercible.rb
Overview
Constrained types apply rules to the input
Direct Known Subclasses
Defined Under Namespace
Classes: Coercible
Instance Attribute Summary collapse
- #options ⇒ Hash included from Options readonly private
- #rule ⇒ Dry::Logic::Rule readonly
- #type ⇒ Type included from Decorator readonly
Instance Method Summary collapse
-
#&(other) ⇒ Intersection, Intersection::Constrained
included
from Builder
private
Compose two types into an Intersection type.
- #===(value) ⇒ Boolean
-
#>(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, &block) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
- #constrained(options) ⇒ Constrained
- #constrained? ⇒ true
- #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 ⇒ Object private
-
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default
included
from Builder
Turn a type into a type with a default value.
- #default? ⇒ Boolean included from Decorator
-
#enum(*values) ⇒ Enum
included
from Builder
Define an enum on top of the existing type.
-
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor
included
from Builder
Use the given value on type mismatch.
-
#initialize(type, **options) ⇒ Constrained
constructor
A new instance of Constrained.
-
#lax ⇒ Lax
Build lax type.
-
#maybe ⇒ Maybe
included
from Builder
Turn a type into a maybe type.
-
#optional ⇒ Sum
included
from Builder
Turn a type into an optional type.
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean included from Decorator
- #to_ast(meta: true) ⇒ Object
-
#to_proc ⇒ Proc
included
from Decorator
Wrap the type with a proc.
- #to_s ⇒ String (also: #inspect) included from Printable private
-
#try(input, &block) ⇒ Object
Safe coercion attempt.
-
#valid?(input = Undefined) ⇒ Boolean
(also: #===)
included
from Type
private
Whether a value is a valid member of the type.
- #with(**new_options) ⇒ Type included from Options private
-
#|(other) ⇒ Sum, Sum::Constrained
included
from Builder
private
Compose two types into a Sum type.
Constructor Details
#initialize(type, **options) ⇒ Constrained
Returns a new instance of Constrained.
23 24 25 26 |
# File 'lib/dry/types/constrained.rb', line 23 def initialize(type, **) super @rule = .fetch(:rule) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator
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.
#rule ⇒ Dry::Logic::Rule (readonly)
16 17 18 |
# File 'lib/dry/types/constrained.rb', line 16 def rule @rule 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
#===(value) ⇒ Boolean
103 104 105 |
# File 'lib/dry/types/constrained.rb', line 103 def ===(value) valid?(value) end |
#>(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, &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.
44 45 46 47 48 49 50 |
# File 'lib/dry/types/constrained.rb', line 44 def call_safe(input, &block) if rule[input] type.call_safe(input, &block) else yield end 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.
31 32 33 34 35 36 37 38 39 |
# File 'lib/dry/types/constrained.rb', line 31 def call_unsafe(input) result = rule.(input) if result.success? type.call_unsafe(input) else raise ConstraintError.new(result, input) end end |
#constrained(options) ⇒ Constrained
87 88 89 |
# File 'lib/dry/types/constrained.rb', line 87 def constrained() with(rule: rule & Types.Rule()) end |
#constrained? ⇒ true
94 95 96 |
# File 'lib/dry/types/constrained.rb', line 94 def constrained? true 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 ⇒ 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.
122 123 124 |
# File 'lib/dry/types/constrained.rb', line 122 def constructor_type type.constructor_type end |
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder
Turn a type into a type with a default value
#default? ⇒ Boolean Originally defined in module Decorator
#enum(*values) ⇒ Enum Originally defined in module Builder
Define an enum on top of the existing type
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder
Use the given value on type mismatch
#lax ⇒ Lax
Build lax type. Constraints are not applicable to lax types hence unwrapping
111 112 113 |
# File 'lib/dry/types/constrained.rb', line 111 def lax type.lax end |
#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator
#to_ast(meta: true) ⇒ Object
117 118 119 |
# File 'lib/dry/types/constrained.rb', line 117 def to_ast(meta: true) [:constrained, [type.to_ast(meta: ), rule.to_ast]] end |
#to_proc ⇒ Proc Originally defined in module Decorator
Wrap the type with a proc
#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) ⇒ Logic::Result #try(input) {|failure| ... } ⇒ Object
Safe coercion attempt. It is similar to #call with a block given but returns a Result instance with metadata about errors (if any).
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dry/types/constrained.rb', line 67 def try(input, &block) result = rule.(input) if result.success? type.try(input, &block) else failure = failure(input, ConstraintError.new(result, input)) block_given? ? yield(failure) : failure 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
#with(**new_options) ⇒ Type 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.
#|(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