Class: Dry::Types::Enum
- Inherits:
-
Object
- Object
- Dry::Types::Enum
- Includes:
- Decorator
- Defined in:
- lib/dry/types/enum.rb
Instance Attribute Summary collapse
- #mapping ⇒ Hash readonly
- #values ⇒ Array readonly
Attributes included from Decorator
Attributes included from Options
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
-
#initialize(type, options) ⇒ Enum
constructor
A new instance of Enum.
Methods included from Decorator
#constrained?, #constructor, #decorate?, #default?, #method_missing, #respond_to_missing?, #try, #valid?
Methods included from Options
Constructor Details
#initialize(type, options) ⇒ Enum
Returns a new instance of Enum.
18 19 20 21 22 23 |
# File 'lib/dry/types/enum.rb', line 18 def initialize(type, ) super @values = .fetch(:values).freeze @values.each(&:freeze) @mapping = values.each_with_object({}) { |v, h| h[values.index(v)] = v }.freeze end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator
Instance Attribute Details
#mapping ⇒ Hash (readonly)
13 14 15 |
# File 'lib/dry/types/enum.rb', line 13 def mapping @mapping end |
Instance Method Details
#call(input) ⇒ Object Also known as: []
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dry/types/enum.rb', line 27 def call(input) value = if values.include?(input) input elsif mapping.key?(input) mapping[input] end type[value || input] end |