Module: Dry::Types::Composition
- Included in:
- Implication, Intersection, Sum
- Defined in:
- lib/dry/types/composition.rb
Defined Under Namespace
Modules: Constrained
Instance Attribute Summary collapse
- #left ⇒ Type readonly
- #options ⇒ Hash included from Options readonly private
- #right ⇒ Type readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#&(other) ⇒ Intersection, Intersection::Constrained
included
from Builder
private
Compose two types into an Intersection type.
-
#>(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 ⇒ Constrained
included
from Builder
Turn a type into a constrained type.
- #constrained? ⇒ false
- #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 ⇒ Class included from Builder private
-
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default
included
from Builder
Turn a type into a type with a default value.
- #default? ⇒ false
-
#enum(*values) ⇒ Enum
included
from Builder
Define an enum on top of the existing type.
- #failure(input, _error = nil) ⇒ Object private
-
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor
included
from Builder
Use the given value on type mismatch.
- #initialize(left, right, **options) ⇒ Object private
-
#lax ⇒ Lax
included
from Builder
Turn a type into a lax type that will rescue from type-errors and return the original input.
-
#maybe ⇒ Maybe
included
from Builder
Turn a type into a maybe type.
- #meta(data = Undefined) ⇒ Object included from Meta
- #name ⇒ String
-
#optional ⇒ Sum
included
from Builder
Turn a type into an optional type.
- #optional? ⇒ Boolean
- #primitive?(value) ⇒ Boolean private
-
#pristine ⇒ Dry::Types::Type
included
from Meta
Resets meta.
- #success(input) ⇒ Object private
- #to_ast(meta: true) ⇒ Object
-
#to_proc ⇒ Proc
Wrap the type with a proc.
- #to_s ⇒ String (also: #inspect) included from Printable private
- #try(input) ⇒ Object
-
#valid?(input = Undefined) ⇒ Boolean
(also: #===)
included
from Type
private
Whether a value is a valid member of the type.
- #with(**options) ⇒ Type included from Meta
-
#|(other) ⇒ Sum, Sum::Constrained
included
from Builder
private
Compose two types into a Sum type.
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.
#right ⇒ Type (readonly)
21 22 23 |
# File 'lib/dry/types/composition.rb', line 21 def right @right end |
Class Method Details
.included(base) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/dry/types/composition.rb', line 33 def self.included(base) composition_name = Inflector.demodulize(base) ast_type = Inflector.underscore(composition_name).to_sym base.define_singleton_method(:ast_type) { ast_type } base.define_singleton_method(:composition_name) { composition_name } base.const_set("Constrained", Class.new(base) { include Constrained }) 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
#>(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.
94 95 96 |
# File 'lib/dry/types/composition.rb', line 94 def call_safe(input, &block) raise NotImplementedError 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.
85 86 87 |
# File 'lib/dry/types/composition.rb', line 85 def call_unsafe(input) raise NotImplementedError end |
#constrained ⇒ Constrained Originally defined in module Builder
Turn a type into a constrained type
#constrained? ⇒ false
69 70 71 |
# File 'lib/dry/types/composition.rb', line 69 def constrained? false 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 ⇒ 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.
#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder
Turn a type into a type with a default value
#default? ⇒ false
62 63 64 |
# File 'lib/dry/types/composition.rb', line 62 def default? false end |
#enum(*values) ⇒ Enum Originally defined in module Builder
Define an enum on top of the existing type
#failure(input, _error = nil) ⇒ 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.
116 117 118 119 120 121 122 123 |
# File 'lib/dry/types/composition.rb', line 116 def failure(input, _error = nil) result = try(input) if result.failure? result else raise ArgumentError, "Invalid failure value '#{input}' for #{inspect}" end end |
#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder
Use the given value on type mismatch
#initialize(left, right, **options) ⇒ 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 |
# File 'lib/dry/types/composition.rb', line 46 def initialize(left, right, **) super @left, @right = left, right freeze end |
#lax ⇒ Lax Originally defined in module Builder
Turn a type into a lax type that will rescue from type-errors and return the original input
#name ⇒ String
55 56 57 |
# File 'lib/dry/types/composition.rb', line 55 def name [left, right].map(&:name).join(" #{self.class.operator} ") end |
#optional? ⇒ Boolean
76 77 78 |
# File 'lib/dry/types/composition.rb', line 76 def optional? false end |
#primitive?(value) ⇒ Boolean
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.
130 131 132 |
# File 'lib/dry/types/composition.rb', line 130 def primitive?(value) raise NotImplementedError end |
#pristine ⇒ Dry::Types::Type Originally defined in module Meta
Resets meta
#success(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.
106 107 108 109 110 111 112 113 |
# File 'lib/dry/types/composition.rb', line 106 def success(input) result = try(input) if result.success? result else raise ArgumentError, "Invalid success value '#{input}' for #{inspect}" end end |
#to_ast(meta: true) ⇒ Object
137 138 139 140 |
# File 'lib/dry/types/composition.rb', line 137 def to_ast(meta: true) [self.class.ast_type, [left.to_ast(meta: ), right.to_ast(meta: ), ? self. : EMPTY_HASH]] end |
#to_proc ⇒ Proc
Wrap the type with a proc
147 148 149 |
# File 'lib/dry/types/composition.rb', line 147 def to_proc proc { |value| self.(value) } end |
#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) ⇒ Object
101 102 103 |
# File 'lib/dry/types/composition.rb', line 101 def try(input) raise NotImplementedError 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
#|(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