class DLDInternet::Formatters::Table

Table formatter :reek: RepeatedConditional {enabled: false}

Public Class Methods

new(obj, format, options) click to toggle source
Calls superclass method DLDInternet::Formatters::Basic::new
# File lib/dldinternet/formatters/table.rb, line 14
def initialize(obj, format, options)
  super
  # @object = [@object] unless @object.is_a?(Array)
  @values  = Hashie::Mash.new
end

Public Instance Methods

capture_output() { || ... } click to toggle source
# File lib/dldinternet/formatters/table.rb, line 20
def capture_output
  @previous_stdout, $stdout = $stdout, StringIO.new
  begin
    yield
  rescue Exception => e
    @previous_stdout.write $stdout.string
    raise e
  end

  $stdout.string
ensure
  # Restore the previous value of stdout (typically equal to stdout).
  $stdout = @previous_stdout
end
column(text, options = {}) click to toggle source
Calls superclass method DLDInternet::Formatters::Basic#column
# File lib/dldinternet/formatters/table.rb, line 83
def column(text, options = {})
  super
  text
end
format_it(item=nil) click to toggle source
# File lib/dldinternet/formatters/table.rb, line 101
def format_it(item=nil)
  if item
    @object = columnize_item(item)
    @is_a_hash = @object.is_a?(Hash)
    @widths = nil
  end
  run
end
header_row() click to toggle source
# File lib/dldinternet/formatters/table.rb, line 69
def header_row
  # list = if @columns.nil?
  #          @object.is_a?(Array) ? @object[0].keys : @object.keys
  #        else
  #          @columns.keys.map{ |k| subkeys(k) }
  #        end
  list = @object.is_a?(Array) ? @object[0].keys : @object.keys
  row color: 'light_yellow', bold: true, encoding: :ascii do
    list.each do |key, _|
      column subkeys(key).to_s, width: widths[key]
    end
  end
end
obj_row(idx, obj) click to toggle source
# File lib/dldinternet/formatters/table.rb, line 88
def obj_row(idx, obj)
  row color: 'white', bold: false do
    if obj.is_a? Hash
      obj.each do |key, val|
        subcolumn(key, val)
      end
    else
      column obj.to_s, width: widths[idx]
      idx += 1
    end
  end
end
restore_output() click to toggle source
# File lib/dldinternet/formatters/table.rb, line 39
def restore_output
  string = $stdout.string
  string
rescue => e
  # noop
ensure
  $stdout = @previous_stdout if @previous_stdout
end
run() click to toggle source
# File lib/dldinternet/formatters/table.rb, line 48
def run
  # suppress_output
  capture_output do
    header(title: @title, align: 'center') if @title #&& !@title.empty?

    table border: true, encoding: :ascii do
      header_row
      idx = 0
      list = @object.is_a?(Array) ? @object : [@object]
      list.each do |obj|
        obj_row(idx, obj)
      end
    end
  end

  # string = restore_output
rescue => exe
  restore_output
  raise exe
end
suppress_output() click to toggle source
# File lib/dldinternet/formatters/table.rb, line 35
def suppress_output
  @previous_stdout, $stdout = $stdout, StringIO.new
end