Class: Tefil::ColumnFormer

Inherits:
TextFilterBase show all
Defined in:
lib/tefil/columnformer.rb

Instance Method Summary collapse

Methods inherited from TextFilterBase

#filter

Constructor Details

#initialize(options = {}) ⇒ ColumnFormer

Returns a new instance of ColumnFormer.



29
30
31
32
33
34
# File 'lib/tefil/columnformer.rb', line 29

def initialize(options = {})
  @just = options[:just] || :left
  @separator = options[:separator] || ' '
  @transpose = options[:transpose]
  super(options)
end

Instance Method Details

#form(matrix, io = $stdout, indent = 0) ⇒ Object

def form(matrix, io = $stdout, separator = “ ”, left = false)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tefil/columnformer.rb', line 37

def form(matrix, io = $stdout, indent = 0)
  #Obtain max length for each column.

  matrix = matrix.transpose if @transpose

  max_lengths = []
  matrix.each do |row|
    row.each_with_index do |item, index|
      item = item.to_s
      max_lengths[index] ||= 0
      size = print_size(item)
      max_lengths[index] = size if max_lengths[index] < size
    end
  end

  #Output
  matrix.each do |row|
    new_items = []
    row.each_with_index do |item, index|
      item = item.to_s
      method = (@just.to_s + "_just").to_sym
      new_items[index] = item.send(method, max_lengths[index])
    end
    io.print(" " * indent)
    io.puts new_items.join(@separator).sub(/ +$/, "")
  end
end