Module: TTY::Table::Validatable Private

Included in:
TTY::Table, Renderer::Basic
Defined in:
lib/tty/table/validatable.rb

Overview

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

Mixin to provide validation for TTY::Table.

Include this mixin to add validation for options.

Instance Method Summary collapse

Instance Method Details

#assert_row_size(row, rows) ⇒ nil

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.

Check if table row is the correct size

Returns:

  • (nil)

Raises:



38
39
40
41
42
43
44
# 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) ⇒ nil

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.

Check if table rows are the equal size

Returns:

  • (nil)

Raises:



21
22
23
24
25
26
27
28
# 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) ⇒ Table

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.

Check if table type is provided

Returns:

Raises:



53
54
55
56
57
# 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) ⇒ 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.

Check if options are of required type



68
69
70
71
72
73
74
75
76
77
78
79
# 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