lib/tty/table/renderer.rb in tty-0.0.9 vs lib/tty/table/renderer.rb in tty-0.0.10
- old
+ new
@@ -1,107 +1,94 @@
# -*- encoding: utf-8 -*-
module TTY
class Table
- # Determine renderer based on terminal capabilities
- #
- # @return [TTY::Table::Renderer]
- #
- # @api public
- def self.renderer
- @renderer ||= if TTY.terminal.color?
- TTY::Table::Renderer::Color
- else
- TTY::Table::Renderer::Basic
- end
- end
-
- # @api public
- def self.renderer=(klass)
- @renderer = klass
- end
-
- # A mixin to allow common rendering methods
- #
- # @return [self]
- #
- # @api public
- module Renderer
- extend TTY::Delegatable
-
+ # A class responsible for rendering tabular data
+ class Renderer
autoload :ASCII, 'tty/table/renderer/ascii'
autoload :Basic, 'tty/table/renderer/basic'
autoload :Color, 'tty/table/renderer/color'
autoload :Unicode, 'tty/table/renderer/unicode'
- RENDERER_DELEGATED_METHODS = [ :render, :total_width]
-
RENDERER_MAPPER = {
:ascii => TTY::Table::Renderer::ASCII,
:basic => TTY::Table::Renderer::Basic,
:color => TTY::Table::Renderer::Color,
:unicode => TTY::Table::Renderer::Unicode
}
- # Initialize a Renderer
+ # Select renderer class based on string name
#
- # @api private
- def initialize(options={})
- super()
- end
-
- # Determine renderer class based on string name
- #
# @param [Symbol] renderer
- # the renderer used for displaying table out of [:basic, :color, :unicode]
+ # the renderer used for displaying table out of [:basic, :ascii, :unicode, :color]
#
# @return [TTY::Table::Renderer]
#
# @api private
- def pick_renderer(type=nil)
- self.renderer= (type ? RENDERER_MAPPER[type].new : self.renderer)
+ def self.select(type)
+ RENDERER_MAPPER[type || :basic]
end
- # Return the default renderer
+ # Raises an error if provided border class is of wrong type or has invalid
+ # implementation
#
- # @return [TTY::Table::Renderer]
+ # @raise [TypeError]
+ # raised when providing wrong class for border
#
+ # @raise [NoImplementationError]
+ # raised when border class does not implement core methods
+ #
# @api public
- def renderer
- @renderer ||= TTY::Table.renderer.new
+ 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
- # Set the renderer
+ # Add custom border for the renderer
#
- # @return [TTY::Table::Renderer]
+ # @param [TTY::Table::Border] border_class
#
- # @api private
- def renderer=(renderer)
- @renderer = renderer
- end
-
- # Add custom border for the renderer
+ # @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
- def renders_with(klass)
- unless klass <= TTY::Table::Border
- raise TypeError, "#{klass} should inherit from TTY::Table::Border"
- end
- unless klass.characters
- raise NoImplementationError, "#{klass} should implement def_border"
- end
- @border_class = klass
+ 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
- delegatable_method :renderer, *RENDERER_DELEGATED_METHODS
-
+ # 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
+ def self.render(table, options={}, &block)
+ renderer = select(options[:renderer]).new(table, options)
+ yield renderer if block_given?
+ renderer.render
+ end
end # Renderer
end # Table
end # TTY