Class: KXI::CLI::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/cli/table.rb

Overview

Table renderer

Defined Under Namespace

Classes: Column

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Table

Instantiate the KXI::CLI::Table class

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
# File 'lib/kxi/cli/table.rb', line 8

def initialize
	@cols = KXI::Collections::ArrayCollection.new
	@rows = 0
	yield(self) if block_given?
end

Instance Method Details

#column(name) ⇒ KXI::CLI::Table::Column

Defines a column of table

Parameters:

  • name (string)

    Name of column

Returns:



17
18
19
20
21
# File 'lib/kxi/cli/table.rb', line 17

def column(name)
	col = Column.new(name)
	@cols.add(col)
	return col
end

#renderObject

Renders the table into stdout



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kxi/cli/table.rb', line 32

def render
	@rows.times do |row|
		f = true
		@cols.foreach do |col|
			print(' ') unless f
			f = false
			col.write(row)
		end
		puts
	end
end

#row(e) ⇒ integer

Adds a row to table

Parameters:

  • e (Hash<string, any>)

    Row to add

Returns:

  • (integer)

    Current number of rows



26
27
28
29
# File 'lib/kxi/cli/table.rb', line 26

def row(e)
	e.each_key { |c| @cols.first { |i| i.name == c }&.row(e[c].to_s.strip.chomp) }
	@rows += 1
end