Class: Termtable::Table
- Inherits:
-
Object
- Object
- Termtable::Table
- Defined in:
- lib/termtable/table.rb
Instance Method Summary collapse
- #<<(row) ⇒ Object
- #buffer_jagged_array! ⇒ Object
-
#initialize(*args) ⇒ Table
constructor
A new instance of Table.
- #render(**opts) ⇒ Object
- #row_count ⇒ Object
Constructor Details
#initialize(*args) ⇒ Table
Returns a new instance of Table.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/termtable/table.rb', line 3 def initialize(*args) if args.first.is_a?(Array) && args.depth == 1 raise Termtable::Error::InitializationError, '2d array required for initialization' end if args.empty? @rows = [] yield(self) if block_given? else if args.first.is_a?(Hash) headers = args.first.delete(:headers) @rows = args.first.delete(:rows) elsif args.size == 2 headers, @rows = args else @rows = args.first end buffer_jagged_array! @rows = @rows.map { |row| Row.new(row) } @rows.unshift(Header.new(headers)) unless headers.nil? end end |
Instance Method Details
#<<(row) ⇒ Object
38 39 40 |
# File 'lib/termtable/table.rb', line 38 def <<(row) @rows << Row.new(row) end |
#buffer_jagged_array! ⇒ Object
29 30 31 32 |
# File 'lib/termtable/table.rb', line 29 def buffer_jagged_array! max = @rows.map(&:size).max @rows.map { |row| (max - row.size).times { row << ' ' } } end |
#render(**opts) ⇒ Object
42 43 44 |
# File 'lib/termtable/table.rb', line 42 def render(**opts) Render.new(@rows, opts).exec end |
#row_count ⇒ Object
34 35 36 |
# File 'lib/termtable/table.rb', line 34 def row_count @rows.size end |