Class: Toys::Acceptor::Enum
Overview
An acceptor that recognizes a fixed set of values.
You provide a list of valid values. The input argument string will be matched against the string forms of these valid values. If it matches, the converter will return the actual value from the valid list.
For example, you could pass [:one, :two, 3]
as the set of values. If
an argument of "two"
is passed in, the converter will yield a final
value of the symbol :two
. If an argument of "3" is passed in, the
converter will yield the integer 3
. If an argument of "three" is
passed in, the match will fail.
Instance Attribute Summary collapse
-
#values ⇒ Array<Object>
readonly
The array of enum values.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(values, type_desc: nil, well_known_spec: nil) ⇒ Enum
constructor
Create an acceptor.
Methods inherited from Base
Constructor Details
#initialize(values, type_desc: nil, well_known_spec: nil) ⇒ Enum
Create an acceptor.
277 278 279 280 |
# File 'lib/toys/acceptor.rb', line 277 def initialize(values, type_desc: nil, well_known_spec: nil) super(type_desc: type_desc, well_known_spec: well_known_spec) @values = Array(values).map { |v| [v.to_s, v] } end |
Instance Attribute Details
#values ⇒ Array<Object> (readonly)
The array of enum values.
286 287 288 |
# File 'lib/toys/acceptor.rb', line 286 def values @values end |