Class: Toys::Acceptor::Enum

Inherits:
Base
  • Object
show all
Defined in:
lib/toys/acceptor.rb

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

Attributes inherited from Base

#type_desc, #well_known_spec

Instance Method Summary collapse

Methods inherited from Base

#to_s

Constructor Details

#initialize(values, type_desc: nil, well_known_spec: nil) ⇒ Enum

Create an acceptor.

Parameters:

  • values (Array<Object>)

    Valid values.

  • type_desc (String) (defaults to: nil)

    Type description string, shown in help. Defaults to DEFAULT_TYPE_DESC.

  • well_known_spec (Object) (defaults to: nil)

    The well-known acceptor spec associated with this acceptor, or nil for none.



275
276
277
278
# File 'lib/toys/acceptor.rb', line 275

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

#valuesArray<Object> (readonly)

The array of enum values.

Returns:

  • (Array<Object>)


284
285
286
# File 'lib/toys/acceptor.rb', line 284

def values
  @values
end