class Toys::Acceptor::Enum
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.
Attributes
The array of enum values. @return [Array<Object>]
Public Class Methods
Create an acceptor.
@param values [Array<Object>] Valid values. @param type_desc [String] Type description string, shown in help.
Defaults to {Toys::Acceptor::DEFAULT_TYPE_DESC}.
@param well_known_spec [Object] The well-known acceptor spec associated
with this acceptor, or `nil` for none.
Toys::Acceptor::Base::new
# 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
Public Instance Methods
Overrides {Toys::Acceptor::Base#convert} to return the actual enum element. @private
# File lib/toys/acceptor.rb, line 299 def convert(_str, elem) elem end
Overrides {Toys::Acceptor::Base#match} to find the value. @private
# File lib/toys/acceptor.rb, line 290 def match(str) str.nil? ? [nil, nil] : @values.find { |s, _e| s == str } end
Overrides {Toys::Acceptor::Base#suggestions} to return close matches from the enum. @private
# File lib/toys/acceptor.rb, line 308 def suggestions(str) Compat.suggestions(str, @values.map(&:first)) end