Class: Protocol::Content::Parameters::Enumeration

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/content/parameters/enumeration.rb

Overview

Converts an exact set of input values to corresponding output values.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ Enumeration

Initialize an enumeration from an input-to-output mapping.



23
24
25
# File 'lib/protocol/content/parameters/enumeration.rb', line 23

def initialize(mapping)
	@mapping = mapping.freeze
end

Instance Attribute Details

#mappingObject (readonly)

The accepted input values and corresponding output values.



28
29
30
# File 'lib/protocol/content/parameters/enumeration.rb', line 28

def mapping
  @mapping
end

Class Method Details

.build(*values, **options) ⇒ Object

Construct an enumeration from accepted values or an input-to-output mapping.



15
16
17
18
19
# File 'lib/protocol/content/parameters/enumeration.rb', line 15

def self.build(*values, **options)
	mapping = values.to_h{|value| [value, value]}
	mapping.update(options)
	return new(mapping)
end

Instance Method Details

#call(value) ⇒ Object

Convert an accepted input value.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/protocol/content/parameters/enumeration.rb', line 34

def call(value)
	if @mapping.key?(value)
		return @mapping[value]
	end
	
	raise ArgumentError, "Invalid enumeration value: #{value.inspect}!"
end