Class: Toys::Acceptor::Base

Inherits:
Object
  • Object
show all
Defined in:
core-docs/toys/acceptor.rb

Overview

Defined in the toys-core gem

A base class for acceptors.

The base acceptor does not do any validation (i.e. it accepts all arguments) or conversion (i.e. it returns the original string). You can subclass this base class and override the #match and #convert methods to implement an acceptor.

Direct Known Subclasses

Enum, Pattern, Simple

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_desc: nil, well_known_spec: nil) ⇒ Base

Create a base acceptor.

Parameters:

  • 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.



52
53
54
# File 'core-docs/toys/acceptor.rb', line 52

def initialize(type_desc: nil, well_known_spec: nil)
  # Source available in the toys-core gem
end

Instance Attribute Details

#type_descString (readonly)

Type description string, shown in help.

Returns:

  • (String)


60
61
62
# File 'core-docs/toys/acceptor.rb', line 60

def type_desc
  @type_desc
end

#well_known_specObject? (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.

Returns:

  • (Object)

    the well-known acceptor

  • (nil)

    if there is no corresponding well-known acceptor



71
72
73
# File 'core-docs/toys/acceptor.rb', line 71

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.

Parameters:

  • str (String, nil)

    Original argument string. May be nil if the value is optional and not provided.

  • extra (Object...)

    Zero or more additional arguments comprising additional elements returned from the match function.

Returns:

  • (Object)

    The converted argument as it should be stored in the context data.



121
122
123
# File 'core-docs/toys/acceptor.rb', line 121

def convert(str, *extra)
  # Source available in the toys-core gem
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.

Parameters:

  • str (String, nil)

    The input argument string. May be nil if the value is optional and not provided.

Returns:

  • (String, Array, nil)


103
104
105
# File 'core-docs/toys/acceptor.rb', line 103

def match(str)
  # Source available in the toys-core gem
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.

Parameters:

  • str (String)

    A string that failed matching.

Returns:

  • (Array<String>)

    A possibly empty array of alternative suggestions that could be displayed with "did you mean..."



138
139
140
# File 'core-docs/toys/acceptor.rb', line 138

def suggestions(str)
  # Source available in the toys-core gem
end

#to_sString

Type description string, shown in help.

Returns:

  • (String)


77
78
79
# File 'core-docs/toys/acceptor.rb', line 77

def to_s
  # Source available in the toys-core gem
end