Class: Protocol::Content::Parameters::Enumeration
- Inherits:
-
Object
- Object
- Protocol::Content::Parameters::Enumeration
- Defined in:
- lib/protocol/content/parameters/enumeration.rb
Overview
Converts an exact set of input values to corresponding output values.
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
The accepted input values and corresponding output values.
Class Method Summary collapse
-
.build(*values, **options) ⇒ Object
Construct an enumeration from accepted values or an input-to-output mapping.
Instance Method Summary collapse
-
#call(value) ⇒ Object
Convert an accepted input value.
-
#initialize(mapping) ⇒ Enumeration
constructor
Initialize an enumeration from an input-to-output mapping.
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
#mapping ⇒ Object (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, **) mapping = values.to_h{|value| [value, value]} mapping.update() return new(mapping) end |
Instance Method Details
#call(value) ⇒ Object
Convert an accepted input value.
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 |