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
- #rule ⇒ Dry::Logic::Rule readonly
Attributes included from Decorator
Attributes included from Options
Instance Method Summary collapse
- #===(value) ⇒ Boolean
- #call_safe(input, &block) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
- #constrained(options) ⇒ Constrained
- #constrained? ⇒ true
- #constructor_type ⇒ Object private
-
#initialize(type, **options) ⇒ Constrained
constructor
A new instance of Constrained.
-
#lax ⇒ Lax
Build lax type.
- #to_ast(meta: true) ⇒ Object
-
#try(input, &block) ⇒ Object
Safe coercion attempt.
Methods included from Printable
Methods included from Builder
#&, #>, #constrained_type, #constructor, #default, #enum, #fallback, #maybe, #optional, #|
Methods included from Decorator
#default?, #respond_to_missing?, #to_proc
Methods included from Options
Methods included from 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
#rule ⇒ Dry::Logic::Rule (readonly)
16 17 18 |
# File 'lib/dry/types/constrained.rb', line 16 def rule @rule end |
Instance Method Details
#===(value) ⇒ Boolean
103 104 105 |
# File 'lib/dry/types/constrained.rb', line 103 def ===(value) valid?(value) end |
#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 |
#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 |
#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 |
#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 |
#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 |