Class: Dry::Types::Constrained
- Inherits:
-
Object
- Object
- Dry::Types::Constrained
- Defined in:
- lib/dry/types/constrained.rb,
lib/dry/types/constrained/coercible.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Coercible
Instance Attribute Summary collapse
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
Attributes included from Decorator
Attributes included from Options
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
- #constrained(options) ⇒ Object
- #constrained? ⇒ Boolean
-
#initialize(type, options) ⇒ Constrained
constructor
A new instance of Constrained.
- #try(input, &block) ⇒ Object
- #valid?(value) ⇒ Boolean
Methods included from Builder
#constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|
Methods included from Decorator
#constructor, #default?, #respond_to_missing?
Methods included from Options
Constructor Details
#initialize(type, options) ⇒ Constrained
Returns a new instance of Constrained.
14 15 16 17 |
# File 'lib/dry/types/constrained.rb', line 14 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 ⇒ Object (readonly)
Returns the value of attribute rule.
12 13 14 |
# File 'lib/dry/types/constrained.rb', line 12 def rule @rule end |
Instance Method Details
#call(input) ⇒ Object Also known as: []
19 20 21 22 23 |
# File 'lib/dry/types/constrained.rb', line 19 def call(input) try(input) do |result| raise ConstraintError.new(result, input) end.input end |
#constrained(options) ⇒ Object
41 42 43 |
# File 'lib/dry/types/constrained.rb', line 41 def constrained() with(rule: rule & Types.Rule()) end |
#constrained? ⇒ Boolean
45 46 47 |
# File 'lib/dry/types/constrained.rb', line 45 def constrained? true end |
#try(input, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dry/types/constrained.rb', line 26 def try(input, &block) result = rule.(input) if result.success? type.try(input, &block) else failure = failure(input, result) block ? yield(failure) : failure end end |
#valid?(value) ⇒ Boolean
37 38 39 |
# File 'lib/dry/types/constrained.rb', line 37 def valid?(value) rule.(value).success? && type.valid?(value) end |