Class: Toys::Definition::EnumAcceptor

Inherits:
Acceptor
  • Object
show all
Defined in:
lib/toys/definition/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.

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

Attributes inherited from Acceptor

#name

Instance Method Summary collapse

Constructor Details

#initialize(name, values) ⇒ EnumAcceptor

Create an acceptor.



170
171
172
173
# File 'lib/toys/definition/acceptor.rb', line 170

def initialize(name, values)
  super(name)
  @values = Array(values).map { |v| [v.to_s, v] }
end

Instance Method Details

#convert(_str, elem) ⇒ Object

Overrides Acceptor#convert to return the original element.



186
187
188
# File 'lib/toys/definition/acceptor.rb', line 186

def convert(_str, elem)
  elem
end

#match(str) ⇒ Object

Overrides Acceptor#match to find the value.



178
179
180
# File 'lib/toys/definition/acceptor.rb', line 178

def match(str)
  @values.find { |s, _e| s == str }
end