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_size(row, rows) click to toggle source

Check if table row is the correct size

@raise [DimensionMismatchError]

if the row is not the correct length

@return [nil]

@api private

# File lib/tty/table/validatable.rb, line 38
def assert_row_size(row, rows)
  return if rows.empty?
  size = rows.last.size
  return if row.size == size
  raise TTY::Table::DimensionMismatchError,
        "row size differs (#{row.size} should be #{size})"
end
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 21
def assert_row_sizes(rows)
  size = (rows[0] || []).size
  rows.each do |row|
    next if row.size == size
    raise 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 53
def assert_table_type(value)
  return value if value.is_a?(TTY::Table)
  raise 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 68
def validate_options!(options)
  header = options[:header]
  rows   = options[:rows]

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

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