module TTY::Table::Renderer
A module responsible for selecting tabule data renderer
Used internally by {Table} to render table content out.
@api private
Constants
- RENDERER_MAPPER
Public Class Methods
Raises an error if provided border class is of wrong type or has invalid implementation
@raise [TypeError]
raised when providing wrong class for border
@raise [NoImplementationError]
raised when border class does not implement core methods
@api public
# File lib/tty/table/renderer.rb, line 48 def 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 a given table and return the string representation.
@param [TTY::Table] table
the table to be rendered
@param [Hash] options
the options to render the table with
@option options [String] :renderer
used to format table output
@return [String]
@api public
# 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
Add custom border for the renderer
@param [TTY::Table::Border] border_class
@param [TTY::Table] table
@param [Hash] options
@raise [TypeError]
raised if the klass does not inherit from Table::Border
@raise [NoImplemntationError]
raise if the klass does not implement def_border
@api public
# 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 renderer class based on string name.
The possible values for type are
[:basic, :ascii, :unicode]
@param [Symbol] type
the renderer type used for displaying table
@return [TTY::Table::Renderer]
@api private
# File lib/tty/table/renderer.rb, line 33 def select(type) RENDERER_MAPPER[type || :basic] end
Private Instance Methods
Raises an error if provided border class is of wrong type or has invalid implementation
@raise [TypeError]
raised when providing wrong class for border
@raise [NoImplementationError]
raised when border class does not implement core methods
@api public
# File lib/tty/table/renderer.rb, line 48 def 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 a given table and return the string representation.
@param [TTY::Table] table
the table to be rendered
@param [Hash] options
the options to render the table with
@option options [String] :renderer
used to format table output
@return [String]
@api public
# 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
Add custom border for the renderer
@param [TTY::Table::Border] border_class
@param [TTY::Table] table
@param [Hash] options
@raise [TypeError]
raised if the klass does not inherit from Table::Border
@raise [NoImplemntationError]
raise if the klass does not implement def_border
@api public
# 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 renderer class based on string name.
The possible values for type are
[:basic, :ascii, :unicode]
@param [Symbol] type
the renderer type used for displaying table
@return [TTY::Table::Renderer]
@api private
# File lib/tty/table/renderer.rb, line 33 def select(type) RENDERER_MAPPER[type || :basic] end