Class: Escort::Formatter::StringGrid
- Inherits:
-
Object
- Object
- Escort::Formatter::StringGrid
- Defined in:
- lib/escort/formatter/string_grid.rb
Constant Summary collapse
- DEFAULT_WIDTH =
80
Instance Attribute Summary collapse
-
#column_count ⇒ Object
readonly
Returns the value of attribute column_count.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(options = {}, &block) ⇒ StringGrid
constructor
A new instance of StringGrid.
- #row(*column_values) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ StringGrid
Returns a new instance of StringGrid.
9 10 11 12 13 14 |
# File 'lib/escort/formatter/string_grid.rb', line 9 def initialize( = {}, &block) @width = [:width] || DEFAULT_WIDTH @column_count = [:columns] || 3 @rows = [] block.call(self) if block_given? end |
Instance Attribute Details
#column_count ⇒ Object (readonly)
Returns the value of attribute column_count.
6 7 8 |
# File 'lib/escort/formatter/string_grid.rb', line 6 def column_count @column_count end |
#rows ⇒ Object
Returns the value of attribute rows.
7 8 9 |
# File 'lib/escort/formatter/string_grid.rb', line 7 def rows @rows end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
6 7 8 |
# File 'lib/escort/formatter/string_grid.rb', line 6 def width @width end |
Instance Method Details
#row(*column_values) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/escort/formatter/string_grid.rb', line 16 def row(*column_values) while column_values.size < @column_count column_values << '' end rows << column_values.map(&:to_s) end |
#to_s ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/escort/formatter/string_grid.rb', line 23 def to_s buffer = [] rows.each do |cells| virtual_row = normalize_virtual_row(virtual_row_for(cells)) physical_row_count_for(virtual_row).times do |physical_count| physical_row = format_physical_row_values(physical_row_for(virtual_row, physical_count)) buffer << physical_row.join("").chomp end end buffer.join("\n") end |