Class: Terminal::Table::Row
- Inherits:
-
Object
- Object
- Terminal::Table::Row
- Defined in:
- lib/terminal-table/lib/terminal-table/row.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Row cells.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #add_cell(item) ⇒ Object (also: #<<)
- #height ⇒ Object
-
#initialize(table, array = []) ⇒ Row
constructor
Initialize with width and options.
- #render ⇒ Object
- #wrap_cell(index, width) ⇒ Object
Constructor Details
#initialize(table, array = []) ⇒ Row
Initialize with width and options.
16 17 18 19 20 21 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 16 def initialize table, array = [] @cell_index = 0 @table = table @cells = [] array.each { |item| self << item } end |
Instance Attribute Details
#cells ⇒ Object (readonly)
Row cells
9 10 11 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 9 def cells @cells end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
11 12 13 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 11 def table @table end |
Instance Method Details
#[](index) ⇒ Object
31 32 33 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 31 def [] index cells[index] end |
#add_cell(item) ⇒ Object Also known as: <<
23 24 25 26 27 28 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 23 def add_cell item = item.is_a?(Hash) ? item : {:value => item} cell = Cell.new(.merge(:index => @cell_index, :table => @table)) @cell_index += cell.colspan @cells << cell end |
#height ⇒ Object
35 36 37 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 35 def height cells.map { |c| c.lines.count }.max end |
#render ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 44 def render y = @table.style.border_y (0...height).to_a.map do |line| y + cells.map do |cell| cell.render(line) end.join(y) + y end.join("\n") end |
#wrap_cell(index, width) ⇒ Object
39 40 41 42 |
# File 'lib/terminal-table/lib/terminal-table/row.rb', line 39 def wrap_cell(index, width) return if @cells.nil? or @cells[index].nil? @cells[index].wrap(width) end |