Class: TTY::Table::Renderer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/table/renderer.rb,
lib/tty/table/renderer/ascii.rb,
lib/tty/table/renderer/basic.rb,
lib/tty/table/renderer/color.rb,
lib/tty/table/renderer/unicode.rb

Overview

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

A class responsible for rendering tabular data

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

Defined Under Namespace

Classes: ASCII, Basic, Color, 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,
  color:   TTY::Table::Renderer::Color,
  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 self.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)


94
95
96
97
98
# File 'lib/tty/table/renderer.rb', line 94

def self.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



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

def self.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 renderer are

:basic, :ascii, :unicode, :color

Parameters:

  • renderer (Symbol)

    the renderer used for displaying table

Returns:



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

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