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 values Array(Object)

The accepted values.

parameter options Hash

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 mapping Hash

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 value Object

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