Class: Dry::Data::Enum

Inherits:
Object
  • Object
show all
Includes:
Decorator
Defined in:
lib/dry/data/enum.rb

Instance Attribute Summary collapse

Attributes included from Decorator

#options, #type

Instance Method Summary collapse

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
# File 'lib/dry/data/enum.rb', line 10

def initialize(type, options)
  super
  @values = options.fetch(:values).freeze
  @values.each(&:freeze)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Data::Decorator

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



8
9
10
# File 'lib/dry/data/enum.rb', line 8

def values
  @values
end

Instance Method Details

#call(input) ⇒ Object Also known as: []



16
17
18
19
20
# File 'lib/dry/data/enum.rb', line 16

def call(input)
  case input
  when Fixnum then type[values[input]]
  else type[input] end
end