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_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:



19
20
21
22
23
24
25
26
# File 'lib/tty/table/validatable.rb', line 19

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) ⇒ 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:



35
36
37
38
39
# File 'lib/tty/table/validatable.rb', line 35

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) ⇒ 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



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tty/table/validatable.rb', line 50

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