Class: DLDInternet::Formatters::Table

Inherits:
Basic
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/dldinternet/formatters/table.rb

Overview

Table formatter :reek:RepeatedConditional false

Instance Attribute Summary

Attributes inherited from Basic

#columns, #format, #is_a_hash, #object, #options, #title, #widths

Instance Method Summary collapse

Methods inherited from Basic

#header_it, #table_widths

Constructor Details

#initialize(obj, format, options) ⇒ Table

Returns a new instance of Table.



14
15
16
17
18
# 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

Instance Method Details

#capture_outputObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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 = {}) ⇒ Object



83
84
85
86
# File 'lib/dldinternet/formatters/table.rb', line 83

def column(text, options = {})
  super
  text
end

#format_it(item = nil) ⇒ Object



101
102
103
104
105
106
107
108
# 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_rowObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# 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_outputObject



39
40
41
42
43
44
45
46
# 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

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dldinternet/formatters/table.rb', line 48

def run
  # suppress_output
  capture_output do
    header(title: @title, align: 'center') if @title #&& [email protected]?

    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_outputObject



35
36
37
# File 'lib/dldinternet/formatters/table.rb', line 35

def suppress_output
  @previous_stdout, $stdout = $stdout, StringIO.new
end