module TTY::Table::Validatable

Mixin to provide validation for {Table}.

Include this mixin to add validation for options.

@api private

Public Instance Methods

assert_row_sizes(rows) click to toggle source

Check if table rows are the equal size

@raise [DimensionMismatchError]

if the rows are not equal length

@return [nil]

@api private

# File lib/tty/table/validatable.rb, line 20
def assert_row_sizes(rows)
  size = (rows[0] || []).size
  rows.each do |row|
    next if row.size == size
    fail TTY::Table::DimensionMismatchError,
         "row size differs (#{row.size} should be #{size})"
  end
end
assert_table_type(value) click to toggle source

Check if table type is provided

@raise [ArgumentRequired]

@return [Table]

@api private

# File lib/tty/table/validatable.rb, line 36
def assert_table_type(value)
  return value if value.is_a?(TTY::Table)
  fail ArgumentRequired,
       "Expected TTY::Table instance, got #{value.inspect}"
end
validate_options!(options) click to toggle source

Check if options are of required type

@api private

# File lib/tty/table/validatable.rb, line 51
def validate_options!(options)
  header = options[:header]
  rows   = options[:rows]

  if header && (!header.is_a?(Array) || header.empty?)
    fail InvalidArgument, ':header must be a non-empty array'
  end

  if rows && !(rows.is_a?(Array) || rows.is_a?(Hash))
    fail InvalidArgument, ':rows must be a non-empty array or hash'
  end
end