Class: Toys::Definition::PatternAcceptor

Inherits:
Acceptor
  • Object
show all
Defined in:
lib/toys/definition/acceptor.rb

Overview

An acceptor that uses a regex to validate input.

Instance Attribute Summary

Attributes inherited from Acceptor

#name

Instance Method Summary collapse

Methods inherited from Acceptor

#convert

Constructor Details

#initialize(name, regex, converter = nil, &block) ⇒ PatternAcceptor

Create a pattern acceptor.

You must provide a regular expression as a validator. You may also provide a converter proc. See Acceptor for details on the converter.

Parameters:

  • name (String)

    A visible name for the acceptor, shown in help.

  • regex (Regexp)

    Regular expression defining value values.

  • converter (Proc) (defaults to: nil)

    A converter function. May also be given as a block. Note that the converter will be passed all elements of the MatchInfo.



137
138
139
140
# File 'lib/toys/definition/acceptor.rb', line 137

def initialize(name, regex, converter = nil, &block)
  super(name, converter, &block)
  @regex = regex
end

Instance Method Details

#match(str) ⇒ Object

Overrides Acceptor#match to use the given regex.



145
146
147
# File 'lib/toys/definition/acceptor.rb', line 145

def match(str)
  @regex.match(str)
end