Class: Kontena::Cli::TableGenerator
- Inherits:
-
Object
- Object
- Kontena::Cli::TableGenerator
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#header_format_proc ⇒ Object
readonly
Returns the value of attribute header_format_proc.
-
#render_mode ⇒ Object
readonly
Returns the value of attribute render_mode.
-
#render_options ⇒ Object
readonly
Returns the value of attribute render_options.
-
#row_format_proc ⇒ Object
readonly
Returns the value of attribute row_format_proc.
Instance Method Summary collapse
- #create_table(header, rows) ⇒ Object
-
#detect_fields ⇒ Object
Collect all the unique keys from the hashes if the data is an array of hashes.
- #format_header_item(field_name) ⇒ Object
- #format_row(row) ⇒ Object
- #generate_header(fields) ⇒ Object
- #initialize(data, fields = nil, row_format_proc: nil, header_format_proc: nil, render_options: nil) ⇒ TTY::Table constructor
- #parse_fields(fields) ⇒ Object
- #render ⇒ Object
- #rows ⇒ Object
- #table ⇒ Object
Constructor Details
#initialize(data, fields = nil, row_format_proc: nil, header_format_proc: nil, render_options: nil) ⇒ TTY::Table
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_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
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/kontena/cli/table_generator.rb', line 7 def data @data end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
8 9 10 |
# File 'lib/kontena/cli/table_generator.rb', line 8 def fields @fields end |
#header ⇒ Object (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_proc ⇒ Object (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_mode ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute render_options.
10 11 12 |
# File 'lib/kontena/cli/table_generator.rb', line 10 def @render_options end |
#row_format_proc ⇒ Object (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_fields ⇒ Object
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 |
#render ⇒ Object
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, ).gsub(/\s+$/, '') end end |
#rows ⇒ Object
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 |
#table ⇒ Object
66 67 68 |
# File 'lib/kontena/cli/table_generator.rb', line 66 def table create_table(header, rows) end |