Class: TTY::Table::Renderer

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

A class responsible for rendering tabular data

Defined Under Namespace

Classes: ASCII, Basic, Color, Unicode

Constant Summary collapse

RENDERER_MAPPER =
{
  :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:



42
43
44
45
46
47
48
49
50
# File 'lib/tty/table/renderer.rb', line 42

def self.assert_border_class(border_class)
  return unless border_class
  unless border_class <= TTY::Table::Border
    raise TypeError, "#{border_class} should inherit from TTY::Table::Border"
  end
  unless border_class.characters
    raise 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:

  • (renderer)

Returns:

  • (String)


86
87
88
89
90
# File 'lib/tty/table/renderer.rb', line 86

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



67
68
69
70
71
# File 'lib/tty/table/renderer.rb', line 67

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

Parameters:

  • renderer (Symbol)

    the renderer used for displaying table out of [:basic, :ascii, :unicode, :color]

Returns:



28
29
30
# File 'lib/tty/table/renderer.rb', line 28

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