Module: Kanji::Types::TypeInterface
- Included in:
- Callable
- Defined in:
- lib/kanji/types/type_interface.rb
Instance Method Summary collapse
- #constrained? ⇒ Boolean
- #failure(obj, error) ⇒ Object
- #optional ⇒ Object
- #success(obj) ⇒ Object
- #try(obj, &block) ⇒ Object
- #valid? ⇒ Boolean
- #|(other) ⇒ Object
Instance Method Details
#constrained? ⇒ Boolean
34 35 36 |
# File 'lib/kanji/types/type_interface.rb', line 34 def constrained? true end |
#failure(obj, error) ⇒ Object
22 23 24 |
# File 'lib/kanji/types/type_interface.rb', line 22 def failure(obj, error) Dry::Types::Result::Failure.new(obj, error) end |
#optional ⇒ Object
30 31 32 |
# File 'lib/kanji/types/type_interface.rb', line 30 def optional Dry::Types::Sum.new(Kanji::Types::Nil, self) end |
#success(obj) ⇒ Object
18 19 20 |
# File 'lib/kanji/types/type_interface.rb', line 18 def success(obj) Dry::Types::Result::Success.new(obj) end |
#try(obj, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/kanji/types/type_interface.rb', line 6 def try(obj, &block) result = valid?(obj) ? success(obj) : failure(obj, constraint_error(obj)) return result if result.success? if block yield(result) else result end end |
#valid? ⇒ Boolean
38 39 40 |
# File 'lib/kanji/types/type_interface.rb', line 38 def valid? raise NotImplementedError, "You must implement the valid? method" end |
#|(other) ⇒ Object
26 27 28 |
# File 'lib/kanji/types/type_interface.rb', line 26 def |(other) Dry::Types::Sum::Constrained.new(self, other) end |