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

#format_itObject



67
68
69
# File 'lib/dldinternet/formatters/table.rb', line 67

def format_it
  run
end

#header_rowObject



40
41
42
43
44
45
46
47
48
# File 'lib/dldinternet/formatters/table.rb', line 40

def header_row
  if @is_a_hash
    row color: 'light_yellow', bold: true, encoding: :ascii do
      @object[0].each do |key, _|
        column key.to_s, width: widths[key] if (@columns.nil? || @columns.keys.include?(key))
      end
    end
  end
end

#obj_row(idx, obj) ⇒ Object



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

def obj_row(idx, obj)
  row color: 'white', bold: false do
    if @is_a_hash
      obj.each do |key, val|
        if @columns.nil? || (@columns.keys.include?(key) && @columns[key].nil?)
          column val.to_s
        else
          subcolumn(key, val)
        end
      end
    else
      column obj.to_s, width: widths[idx]
      idx += 1
    end
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dldinternet/formatters/table.rb', line 20

def run
  suppress_output

  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

  capture_output
rescue => exe
  restore_output
  raise exe
end