class TTY::Option::AggregateErrors

Public Class Methods

new(errors = []) click to toggle source

Create an intance from the passed error objects

@api public

# File lib/tty/option/aggregate_errors.rb, line 19
def initialize(errors = [])
  @errors = errors
end

Public Instance Methods

add(error) click to toggle source

Add error

@api public

# File lib/tty/option/aggregate_errors.rb, line 26
def add(error)
  @errors << error
  error
end
each(&block) click to toggle source

Enumerate each error

@example

errors = AggregateErrors.new
errors.each do |error|
  # instance of TTY::Option::Error
end

@api public

# File lib/tty/option/aggregate_errors.rb, line 40
def each(&block)
  @errors.each(&block)
end
messages() click to toggle source

All error messages

@example

errors = AggregateErrors.new
errors.add TTY::OptionInvalidArgument.new("invalid argument")
errors.messages
# => ["invalid argument"]

@api public

# File lib/tty/option/aggregate_errors.rb, line 53
def messages
  map(&:message)
end
summary(width: 80, indent: 0) click to toggle source

Format errors for display in terminal

@example

errors = AggregateErrors.new
errors.add TTY::OptionInvalidArgument.new("invalid argument")
errors.summary
# =>
# Error: invalid argument

@param [Integer] :width @param [Integer] :indent

@return [String]

@api public

# File lib/tty/option/aggregate_errors.rb, line 72
def summary(width: 80, indent: 0)
  return "" if count.zero?

  output = []
  space_indent = " " * indent
  if messages.count == 1
    msg = messages.first
    label = "Error: "
    output << "#{space_indent}#{label}" \
              "#{wrap(msg, indent: indent + label.length, width: width)}"
  else
    output << "#{space_indent}Errors:"
    messages.each_with_index do |message, num|
      entry = "  #{num + 1}) "
      output << "#{space_indent}#{entry}" \
                "#{wrap(message.capitalize, indent: indent + entry.length,
                                            width: width)}"
    end
  end
  output.join("\n")
end