Module: TTY::Table::Renderer Private

Defined in:
lib/tty/table/renderer.rb,
lib/tty/table/renderer/ascii.rb,
lib/tty/table/renderer/basic.rb,
lib/tty/table/renderer/unicode.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.

A module responsible for selecting tabule data renderer

Used internally by TTY::Table to render table content out.

Defined Under Namespace

Classes: ASCII, Basic, Unicode

Constant Summary collapse

RENDERER_MAPPER =

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

{
  ascii:   TTY::Table::Renderer::ASCII,
  basic:   TTY::Table::Renderer::Basic,
  unicode: TTY::Table::Renderer::Unicode
}

Class Method Summary collapse

Class Method Details

.assert_border_class(border_class) ⇒ Object

Raises an error if provided border class is of wrong type or has invalid implementation

Raises:


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tty/table/renderer.rb', line 48

def assert_border_class(border_class)
  return unless border_class
  unless border_class <= TTY::Table::Border
    fail TypeError,
         "#{border_class} should inherit from TTY::Table::Border"
  end
  unless border_class.characters
    fail NoImplementationError,
         "#{border_class} should implement def_border"
  end
end

.render(table, options = {}) {|renderer| ... } ⇒ String

Render a given table and return the string representation.

Parameters:

  • table (TTY::Table)

    the table to be rendered

  • options (Hash) (defaults to: {})

    the options to render the table with

Options Hash (options):

  • :renderer (String)

    used to format table output

Yields:

Returns:

  • (String)

74
75
76
77
78
# File 'lib/tty/table/renderer.rb', line 74

def render(table, options = {}, &block)
  renderer = select(options[:renderer]).new(table, options)
  yield renderer if block_given?
  renderer.render
end

.render_with(border_class, table, options = {}, &block) ⇒ Object

Add custom border for the renderer

Parameters:

Raises:

  • (TypeError)

    raised if the klass does not inherit from Table::Border

  • (NoImplemntationError)

    raise if the klass does not implement def_border


96
97
98
99
100
# File 'lib/tty/table/renderer.rb', line 96

def render_with(border_class, table, options = {}, &block)
  assert_border_class(border_class)
  options[:border_class] = border_class if border_class
  render(table, options, &block)
end

.select(type) ⇒ TTY::Table::Renderer

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.

Select renderer class based on string name.

The possible values for type are

[:basic, :ascii, :unicode]

Parameters:

  • type (Symbol)

    the renderer type used for displaying table

Returns:


33
34
35
# File 'lib/tty/table/renderer.rb', line 33

def select(type)
  RENDERER_MAPPER[type || :basic]
end