Class: Reportability::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/reportability/pivot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Table



4
5
6
# File 'lib/reportability/pivot.rb', line 4

def initialize(args)
  @cols, @rows = args[:cols], args[:rows]
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



3
4
5
# File 'lib/reportability/pivot.rb', line 3

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



3
4
5
# File 'lib/reportability/pivot.rb', line 3

def rows
  @rows
end

Instance Method Details

#inspectObject



8
9
10
11
12
13
14
15
# File 'lib/reportability/pivot.rb', line 8

def inspect
  lengths = cols.map { |col| max_column_length(col) }
  rows.map do |row|
    "|".tap do |s|
      row.each_index { |i| s << (" %-#{lengths[i]}s |" % row[i].to_s)}
    end
  end.join("\n")
end

#max_column_length(col) ⇒ Object



17
18
19
20
# File 'lib/reportability/pivot.rb', line 17

def max_column_length(col)
  idx = cols.index(col)
  ([col.to_s] + rows).map { |row| row[idx].to_s.length }.max
end