Class: Toys::Acceptor::Simple
Overview
An acceptor that uses a simple function to validate and convert input. The function must take the input string as its argument, and either return the converted object to indicate success, or raise an exception or return the sentinel REJECT to indicate invalid input.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(function = nil, type_desc: nil, well_known_spec: nil, &block) ⇒ Simple
constructor
Create a simple acceptor.
Methods inherited from Base
Constructor Details
#initialize(function = nil, type_desc: nil, well_known_spec: nil, &block) ⇒ Simple
Create a simple acceptor.
You should provide an acceptor function, either as a proc in the
function
argument, or as a block. The function must take as its one
argument the input string. If the string is valid, the function must
return the value to store in the tool's data. If the string is invalid,
the function may either raise an exception (which must descend from
StandardError
) or return REJECT.
174 175 176 177 |
# File 'lib/toys/acceptor.rb', line 174 def initialize(function = nil, type_desc: nil, well_known_spec: nil, &block) super(type_desc: type_desc, well_known_spec: well_known_spec) @function = function || block || proc { |s| s } end |