Class: Dry::Types::Enum
- Inherits:
-
Object
- Object
- Dry::Types::Enum
- Defined in:
- lib/dry/types/enum.rb
Overview
Enum types can be used to define an enum on top of an existing type
Instance Attribute Summary collapse
- #inverted_mapping ⇒ Hash readonly
- #mapping ⇒ Hash readonly
- #options ⇒ Hash included from Options readonly private
- #type ⇒ Type included from Decorator readonly
- #values ⇒ Array readonly
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? ⇒ Boolean included from Decorator
- #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 ⇒ Object private
- #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) ⇒ Enum
constructor
private
A new instance of Enum.
-
#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.
-
#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)
- #try(input) ⇒ Object
-
#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) ⇒ Enum
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.
Returns a new instance of Enum.
28 29 30 31 32 33 34 |
# File 'lib/dry/types/enum.rb', line 28 def initialize(type, **) super @mapping = .fetch(:mapping).freeze @values = @mapping.keys.freeze @inverted_mapping = @mapping.invert.freeze freeze end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator
Instance Attribute Details
#inverted_mapping ⇒ Hash (readonly)
21 22 23 |
# File 'lib/dry/types/enum.rb', line 21 def inverted_mapping @inverted_mapping end |
#mapping ⇒ Hash (readonly)
18 19 20 |
# File 'lib/dry/types/enum.rb', line 18 def mapping @mapping 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.
46 47 48 |
# File 'lib/dry/types/enum.rb', line 46 def call_safe(input, &block) type.call_safe(map_value(input), &block) 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.
39 40 41 |
# File 'lib/dry/types/enum.rb', line 39 def call_unsafe(input) type.call_unsafe(map_value(input)) end |
#constrained ⇒ Constrained Originally defined in module Builder
Turn a type into a constrained type
#constrained? ⇒ Boolean Originally defined in module Decorator
#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 ⇒ 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.
58 59 60 61 |
# File 'lib/dry/types/enum.rb', line 58 def default(*) raise ".enum(*values).default(value) is not supported. Call "\ ".default(value).enum(*values) instead" end |
#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 Originally defined in module Builder
Turn a type into a lax type that will rescue from type-errors and return the original input
#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator
#to_ast(meta: true) ⇒ Object
69 70 71 |
# File 'lib/dry/types/enum.rb', line 69 def to_ast(meta: true) [:enum, [type.to_ast(meta: ), mapping]] end |
#to_proc ⇒ Proc Originally defined in module Decorator
Wrap the type with a proc
#to_s ⇒ String Also known as: inspect
76 77 78 |
# File 'lib/dry/types/enum.rb', line 76 def to_s PRINTER.(self) end |
#try(input) ⇒ Object
53 54 55 |
# File 'lib/dry/types/enum.rb', line 53 def try(input) super(map_value(input)) 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