Class: Toys::Definition::Acceptor
- Inherits:
-
Object
- Object
- Toys::Definition::Acceptor
- Defined in:
- lib/toys/definition/acceptor.rb
Overview
An Acceptor validates and converts arguments. It is designed to be compatible with the OptionParser accept mechanism.
First, an acceptor validates an argument via the #match method. This method should determine whether the argument is valid, and return information that will help with conversion of the argument.
Second, an acceptor converts the argument from the input string to its final form via the #convert method.
Finally, an acceptor has a name that may appear in help text for flags and arguments that use it.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ String
(also: #to_s)
readonly
Name of the acceptor.
Instance Method Summary collapse
-
#convert(str, *extra) ⇒ Object
Convert the given input.
-
#initialize(name, converter = nil, &block) ⇒ Acceptor
constructor
Create a base acceptor.
-
#match(str) ⇒ String, Array
Validate the given input.
Constructor Details
#initialize(name, converter = nil, &block) ⇒ Acceptor
Create a base acceptor.
The base acceptor does not do any validation (i.e. it accepts all arguments). You may subclass this object and override the #match method to change this behavior.
The base acceptor lets you provide a converter as a proc. The proc should take one or more arguments, the first of which is the entire argument string, and the others of which are any additional values returned from validation. The converter should return the final converted value of the argument.
The converter may be provided either as a proc in the converter
parameter, or as a block. If neither is provided, the base acceptor
performs no conversion and uses the argument string.
70 71 72 73 |
# File 'lib/toys/definition/acceptor.rb', line 70 def initialize(name, converter = nil, &block) @name = name.to_s @converter = converter || block end |
Instance Attribute Details
#name ⇒ String (readonly) Also known as: to_s
Name of the acceptor
79 80 81 |
# File 'lib/toys/definition/acceptor.rb', line 79 def name @name end |
Instance Method Details
#convert(str, *extra) ⇒ Object
Convert the given input. Uses the converter provided to this object's constructor. Subclasses may also override this method.
115 116 117 |
# File 'lib/toys/definition/acceptor.rb', line 115 def convert(str, *extra) @converter ? @converter.call(str, *extra) : str end |
#match(str) ⇒ String, Array
Validate the given input.
You may override this method to specify a validation function. For a valid input, the function must return either the original argument string, or an array of which the first element is the original argument string, and the remaining elements may comprise additional information. All returned information is then passed to the conversion function. Note that a MatchInfo object is a legitimate return value since it duck-types the appropriate array.
For an invalid input, you should return a falsy value.
The default implementation simply returns the original argument string, indicating all inputs are valid.
101 102 103 |
# File 'lib/toys/definition/acceptor.rb', line 101 def match(str) str end |