Class: Toys::Acceptor::Pattern
Overview
An acceptor that uses a regex to validate input. It also supports a custom conversion function that generates the final value from the match results.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(regex, converter = nil, type_desc: nil, well_known_spec: nil, &block) ⇒ Pattern
constructor
Create a pattern acceptor.
Methods inherited from Base
Constructor Details
#initialize(regex, converter = nil, type_desc: nil, well_known_spec: nil, &block) ⇒ Pattern
Create a pattern acceptor.
You must provide a regular expression (or any object that duck-types
Regexp#match
) as a validator.
You may also optionally provide a converter, either as a proc or a
block. A converter must take as its arguments the values in the
MatchData
returned from a successful regex match. That is, the first
argument is the original input string, and the remaining arguments are
the captures. The converter must return the final converted value.
If no converter is provided, no conversion is done and the input string
is returned.
231 232 233 234 235 |
# File 'lib/toys/acceptor.rb', line 231 def initialize(regex, converter = nil, type_desc: nil, well_known_spec: nil, &block) super(type_desc: type_desc, well_known_spec: well_known_spec) @regex = regex @converter = converter || block end |