class Enumeration
Converts an exact set of input values to corresponding output values.
Definitions
def self.build(*values, **options)
Construct an enumeration from accepted values or an input-to-output mapping.
Signature
-
parameter
valuesArray(Object) The accepted values.
-
parameter
optionsHash Additional input-to-output mappings.
-
returns
Enumeration The enumeration converter.
Implementation
def self.build(*values, **options)
mapping = values.to_h{|value| [value, value]}
mapping.update(options)
return new(mapping)
end
def initialize(mapping)
Initialize an enumeration from an input-to-output mapping.
Signature
-
parameter
mappingHash The accepted inputs and corresponding outputs.
Implementation
def initialize(mapping)
@mapping = mapping.freeze
end
attr :mapping
The accepted input values and corresponding output values.
def call(value)
Convert an accepted input value.
Signature
-
parameter
valueObject The input value.
-
returns
Object The corresponding output value.
-
raises
ArgumentError If the input value is not accepted.
Implementation
def call(value)
if @mapping.key?(value)
return @mapping[value]
end
raise ArgumentError, "Invalid enumeration value: #{value.inspect}!"
end