Class: Kontena::Cli::TableGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/cli/table_generator.rb

Defined Under Namespace

Modules: Helper

Constant Summary collapse

DEFAULT_HEADER_FORMAT_PROC =
lambda { |header| header.to_s.capitalize }
DEFAULT_RENDER_OPTS =
{padding: [0,2,0,0]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, fields = nil, row_format_proc: nil, header_format_proc: nil, render_options: nil) ⇒ TTY::Table

Parameters:

  • data (Array<Hash>, Array<Array>)

    an array of hashes or arrays

  • fields (Array) (defaults to: nil)

    an array of field names found in the data hashes.

  • fields (Hash) (defaults to: nil)

    a hash of field_title => field_name_in_the_data_hash, for example ‘Users’ => ‘user_count’

  • fields (NilClass) (defaults to: nil)

    try to auto detect fields (all fields!) from the data hashes



49
50
51
52
53
54
55
56
57
# File 'lib/kontena/cli/table_generator.rb', line 49

def initialize(data, fields = nil, row_format_proc: nil, header_format_proc: nil, render_options: nil)
  @data = data
  @render_options = render_options || { }
  @render_mode = @render_options.delete(:mode) || :basic
  @row_format_proc = row_format_proc
  @header_format_proc = header_format_proc || DEFAULT_HEADER_FORMAT_PROC
  @fields = parse_fields(fields)
  @header = generate_header(fields || @fields)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/kontena/cli/table_generator.rb', line 7

def data
  @data
end

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/kontena/cli/table_generator.rb', line 8

def fields
  @fields
end

#headerObject (readonly)

Returns the value of attribute header.



9
10
11
# File 'lib/kontena/cli/table_generator.rb', line 9

def header
  @header
end

#header_format_procObject (readonly)

Returns the value of attribute header_format_proc.



10
11
12
# File 'lib/kontena/cli/table_generator.rb', line 10

def header_format_proc
  @header_format_proc
end

#render_modeObject (readonly)

Returns the value of attribute render_mode.



10
11
12
# File 'lib/kontena/cli/table_generator.rb', line 10

def render_mode
  @render_mode
end

#render_optionsObject (readonly)

Returns the value of attribute render_options.



10
11
12
# File 'lib/kontena/cli/table_generator.rb', line 10

def render_options
  @render_options
end

#row_format_procObject (readonly)

Returns the value of attribute row_format_proc.



10
11
12
# File 'lib/kontena/cli/table_generator.rb', line 10

def row_format_proc
  @row_format_proc
end

Instance Method Details

#create_table(header, rows) ⇒ Object



59
60
61
62
63
64
# File 'lib/kontena/cli/table_generator.rb', line 59

def create_table(header, rows)
  TTY::Table.new(
    header: header,
    rows: rows
  )
end

#detect_fieldsObject

Collect all the unique keys from the hashes if the data is an array of hashes.



95
96
97
98
99
100
101
# File 'lib/kontena/cli/table_generator.rb', line 95

def detect_fields
  if data.first.respond_to?(:keys)
    data.flat_map(&:keys).uniq
  else
    []
  end
end

#format_header_item(field_name) ⇒ Object



85
86
87
# File 'lib/kontena/cli/table_generator.rb', line 85

def format_header_item(field_name)
  header_format_proc.call(field_name)
end

#format_row(row) ⇒ Object



78
79
80
81
82
83
# File 'lib/kontena/cli/table_generator.rb', line 78

def format_row(row)
  return row if row_format_proc.nil?
  row_clone = row.dup
  row_format_proc.call(row_clone)
  row_clone
end

#generate_header(fields) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kontena/cli/table_generator.rb', line 113

def generate_header(fields)
  if fields.kind_of?(Hash)
    header = fields.keys
  else
    header = Array(fields)
  end

  if header.size < 2
    nil
  else
    header.map { |head| format_header_item(head) }
  end
end

#parse_fields(fields) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/kontena/cli/table_generator.rb', line 103

def parse_fields(fields)
  if fields.nil? || fields.empty?
    detect_fields
  elsif fields.kind_of?(Hash)
    fields.values
  else
    fields
  end
end

#renderObject



70
71
72
73
74
75
76
# File 'lib/kontena/cli/table_generator.rb', line 70

def render
  if data.empty?
    fields.size > 1 ? fields.map(&method(:format_header_item)).join('  ') : ''
  else
    table.render(render_mode, render_options).gsub(/\s+$/, '')
  end
end

#rowsObject



89
90
91
# File 'lib/kontena/cli/table_generator.rb', line 89

def rows
  fields.empty? ? data.map { |row| format_row(row).map(&:values) } : data.map { |row| format_row(row).values_at(*fields) }
end

#tableObject



66
67
68
# File 'lib/kontena/cli/table_generator.rb', line 66

def table
  create_table(header, rows)
end