Class: Dry::Types::Enum
- Inherits:
-
Object
- Object
- Dry::Types::Enum
- Includes:
- Decorator
- Defined in:
- lib/dry/types/enum.rb
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Attributes included from Decorator
Instance Method Summary collapse
- #call(input) ⇒ Object (also: #[])
-
#initialize(type, options) ⇒ Enum
constructor
A new instance of Enum.
Methods included from Decorator
#constructor, #respond_to_missing?, #valid?, #with
Constructor Details
#initialize(type, options) ⇒ Enum
Returns a new instance of Enum.
10 11 12 13 14 15 |
# File 'lib/dry/types/enum.rb', line 10 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 ⇒ Object (readonly)
Returns the value of attribute mapping.
8 9 10 |
# File 'lib/dry/types/enum.rb', line 8 def mapping @mapping end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
8 9 10 |
# File 'lib/dry/types/enum.rb', line 8 def values @values end |
Instance Method Details
#call(input) ⇒ Object Also known as: []
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dry/types/enum.rb', line 17 def call(input) value = if values.include?(input) input elsif mapping.key?(input) mapping[input] end type[value || input] end |