Class: Escort::Formatter::StringGrid

Inherits:
Object
  • Object
show all
Defined in:
lib/escort/formatter/string_grid.rb

Constant Summary collapse

DEFAULT_WIDTH =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {}, &block)
  @width = options[:width] || DEFAULT_WIDTH
  @column_count = options[:columns] || 3
  @rows = []
  block.call(self) if block_given?
end

Instance Attribute Details

#column_countObject (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

#rowsObject

Returns the value of attribute rows.



7
8
9
# File 'lib/escort/formatter/string_grid.rb', line 7

def rows
  @rows
end

#widthObject (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_sObject



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