Class: TTY::Option::InvalidArity

Inherits:
ParseError show all
Defined in:
lib/tty/option/errors.rb

Overview

Raised when number of parameter arguments doesn't match

Constant Summary collapse

MESSAGE =
"%<type>s '%<name>s' should appear %<expect>s but appeared %<actual>s"

Instance Attribute Summary

Attributes inherited from ParseError

#param

Instance Method Summary collapse

Constructor Details

#initialize(param_or_message, arity = nil) ⇒ InvalidArity

Returns a new instance of InvalidArity.


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tty/option/errors.rb', line 57

def initialize(param_or_message, arity = nil)
  if param_or_message.is_a?(Parameter)
    @param = param_or_message
    prefix = param.arity < 0 ? "at least " : ""
    expected_arity = param.arity < 0 ? param.arity.abs - 1 : param.arity

    message = format(MESSAGE,
                     type: param.to_sym,
                     name: param.name,
                     expect: prefix + pluralize("time", expected_arity),
                     actual: pluralize("time", arity))
  else
    message = param_or_message
  end

  super(message)
end

Instance Method Details

#pluralize(noun, count = 1) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pluralize a noun


78
79
80
# File 'lib/tty/option/errors.rb', line 78

def pluralize(noun, count = 1)
  "#{count} #{noun}#{'s' unless count == 1}"
end