Class: Toys::Acceptor::Simple

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

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

Range

Instance Attribute Summary

Attributes inherited from Base

#type_desc, #well_known_spec

Instance Method Summary collapse

Methods inherited from Base

#suggestions, #to_s

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.

Parameters:

  • function (Proc) (defaults to: nil)

    The acceptor function

  • type_desc (String) (defaults to: nil)

    Type description string, shown in help. Defaults to DEFAULT_TYPE_DESC.

  • well_known_spec (Object) (defaults to: nil)

    The well-known acceptor spec associated with this acceptor, or nil for none.

  • block (Proc)

    The acceptor function, if not provided as a normal parameter.



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