Class: Toys::Acceptor::Base
- Inherits:
-
Object
- Object
- Toys::Acceptor::Base
- Defined in:
- lib/toys/acceptor.rb
Overview
Instance Attribute Summary collapse
-
#type_desc ⇒ String
readonly
Type description string, shown in help.
-
#well_known_spec ⇒ Object?
readonly
The well-known acceptor spec associated with this acceptor, if any.
Instance Method Summary collapse
-
#convert(str, *extra) ⇒ Object
Convert the given input.
-
#initialize(type_desc: nil, well_known_spec: nil) ⇒ Base
constructor
Create a base acceptor.
-
#match(str) ⇒ String, ...
Validate the given input.
-
#suggestions(str) ⇒ Array<String>
Return suggestions for a given non-matching string.
-
#to_s ⇒ String
Type description string, shown in help.
Constructor Details
#initialize(type_desc: nil, well_known_spec: nil) ⇒ Base
Create a base acceptor.
50 51 52 53 |
# File 'lib/toys/acceptor.rb', line 50 def initialize(type_desc: nil, well_known_spec: nil) @type_desc = type_desc || DEFAULT_TYPE_DESC @well_known_spec = well_known_spec end |
Instance Attribute Details
#type_desc ⇒ String (readonly)
Type description string, shown in help.
59 60 61 |
# File 'lib/toys/acceptor.rb', line 59 def type_desc @type_desc end |
#well_known_spec ⇒ Object? (readonly)
The well-known acceptor spec associated with this acceptor, if any.
This generally identifies an OptionParser-compatible acceptor spec. For
example, the acceptor object that corresponds to Integer
will return
Integer
from this attribute.
70 71 72 |
# File 'lib/toys/acceptor.rb', line 70 def well_known_spec @well_known_spec end |
Instance Method Details
#convert(str, *extra) ⇒ Object
Convert the given input.
This method is passed the results of a successful match, including the original input string and any other values returned from #match. It must return the final converted value to use.
120 121 122 |
# File 'lib/toys/acceptor.rb', line 120 def convert(str, *extra) # rubocop:disable Lint/UnusedMethodArgument str.nil? ? true : str end |
#match(str) ⇒ String, ...
Validate the given input.
When given a valid input, return an array in which the first element is the original input string, and the remaining elements (which may be empty) comprise any additional information that may be useful during conversion. If there is no additional information, you may return the original input string by itself without wrapping in an array.
When given an invalid input, return a falsy value such as nil
.
Note that a MatchData
object is a legitimate return value because it
duck-types the appropriate array.
This default implementation simply returns the original input string, as the only array element, indicating all inputs are valid. You can override this method to provide a different validation function.
102 103 104 |
# File 'lib/toys/acceptor.rb', line 102 def match(str) [str] end |
#suggestions(str) ⇒ Array<String>
Return suggestions for a given non-matching string.
This method may be called when a match fails. It should return a (possibly empty) array of suggestions that could be displayed to the user as "did you mean..."
The default implementation returns the empty list.
137 138 139 |
# File 'lib/toys/acceptor.rb', line 137 def suggestions(str) # rubocop:disable Lint/UnusedMethodArgument [] end |
#to_s ⇒ String
Type description string, shown in help.
76 77 78 |
# File 'lib/toys/acceptor.rb', line 76 def to_s type_desc.to_s end |