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
- #values ⇒ Array readonly
Attributes included from Decorator
Attributes included from Options
Instance Method Summary collapse
- #call_safe(input) ⇒ Object private
- #call_unsafe(input) ⇒ Object private
- #default ⇒ Object private
-
#each_value ⇒ Array, Enumerator
Iterate over each enum value.
-
#initialize(type, **options) ⇒ Enum
constructor
private
A new instance of Enum.
- #joined_values ⇒ String private
- #name ⇒ String
- #to_ast(meta: true) ⇒ Object
- #to_s ⇒ String (also: #inspect)
- #try(input) ⇒ Object
Methods included from Builder
#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #enum, #fallback, #lax, #maybe, #optional, #|
Methods included from Decorator
#constrained?, #default?, #respond_to_missing?, #to_proc
Methods included from Options
Methods included from 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
#call_safe(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.
44 |
# File 'lib/dry/types/enum.rb', line 44 def call_safe(input, &) = type.call_safe(map_value(input), &) |
#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 |
# File 'lib/dry/types/enum.rb', line 39 def call_unsafe(input) = type.call_unsafe(map_value(input)) |
#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.
52 53 54 55 |
# File 'lib/dry/types/enum.rb', line 52 def default(*) raise ".enum(*values).default(value) is not supported. Call " \ ".default(value).enum(*values) instead" end |
#each_value ⇒ Array, Enumerator
Iterate over each enum value
77 78 79 |
# File 'lib/dry/types/enum.rb', line 77 def each_value(&) values.each(&) end |
#joined_values ⇒ String
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.
91 92 93 94 95 96 97 98 99 |
# File 'lib/dry/types/enum.rb', line 91 def joined_values mapping.keys.map { |value| if value.is_a?(::String) value else value.inspect end }.join("|") end |
#name ⇒ String
86 |
# File 'lib/dry/types/enum.rb', line 86 def name = "#{super}(#{joined_values})" |
#to_ast(meta: true) ⇒ Object
63 64 65 |
# File 'lib/dry/types/enum.rb', line 63 def to_ast(meta: true) [:enum, [type.to_ast(meta: ), mapping]] end |
#to_s ⇒ String Also known as: inspect
70 |
# File 'lib/dry/types/enum.rb', line 70 def to_s = PRINTER.(self) |
#try(input) ⇒ Object
49 |
# File 'lib/dry/types/enum.rb', line 49 def try(input, &) = super(map_value(input)) |