Permalink
Browse files

table formatting

  • Loading branch information...
1 parent aef4190 commit 58000524770a155170909a3b30798843f8bd4d32 @tsonntag committed Apr 5, 2012
Showing with 30 additions and 17 deletions.
  1. +2 −0 lib/gitter.rb
  2. +28 −17 lib/gitter/table.rb
View
2 lib/gitter.rb
@@ -2,8 +2,10 @@ module Gitter
autoload :Version, 'gitter/version'
autoload :Grid, 'gitter/grid'
+ autoload :PivotGrid, 'gitter/pivot_grid'
autoload :Helper, 'gitter/helper'
autoload :Controller, 'gitter/controller'
+ autoload :Table, 'gitter/table'
class ConfigurationError < StandardError
end
View
45 lib/gitter/table.rb
@@ -3,6 +3,8 @@
module Gitter
class TableCell
+ include ActionView::Helpers::NumberHelper
+
attr_reader :x, :y, :content
@@ -11,12 +13,16 @@ def initialize x, y, content
end
def html opts = {}
- Table.tag :td, content, opts.merge(class: "#{x} #{y}")
+ Table.tag :td, formatted_content, opts.merge(class: "#{x} #{y}")
end
def header?
false
end
+
+ def formatted_content
+ number_with_delimiter content, delimiter: '.'
+ end
end
class TableHeaderCell < TableCell
@@ -27,7 +33,7 @@ def initialize content
end
def html opts = {}
- Table.tag :th, content, opts
+ Table.tag :th, formatted_content, opts
end
def header?
@@ -50,21 +56,26 @@ def self.tag tag, content, opts = {}
# # data: hash from [x_key,y_key] to cell_array
def initialize title, x_axis, y_axis, data, opts = {}
@title, @x_axis, @y_axis, @data, @opts = title, x_axis, y_axis, data, opts
+ @cells = data.dup
if label = opts[:show_sums]
- add_sums_to_cells data
- @x_axis = add_sums_to_axis @x_axis, label
- @y_axis = add_sums_to_axis @y_axis, label
+ add_sums
+ @x_axis = add_sum_label_to_axis @x_axis, label
+ @y_axis = add_sum_label_to_axis @y_axis, label
end
end
+ def empty?
+ data.empty?
+ end
+
def rows
@rows ||= begin
rows = []
rows << x_header if x_header
rows + (y_axis||[nil]).map do |y,y_title|
row = (x_axis||[nil]).map do |x,x_title|
- cell = data[pivot_key(x,y)]
+ cell = @cells[cell_key(x,y)]
cell = yield cell, x, y if block_given?
TableCell.new x, y, cell
end
@@ -97,30 +108,30 @@ def x_header
end
end
- def pivot_key x, y
+ def cell_key x, y
if x.nil? || y.nil?
x.nil? ? y : x
else
[x,y]
end
end
- def add_sums_to_cells cells
- xcells, ycells = {}, {}
+ def add_sums
+ xsums, ysums = {}, {}
sum = 0
- cells.each do |key,value|
+ @cells.each do |key,value|
x, y = *key
- xcells[y] = (xcells[y]||0) + value
- ycells[x] = (ycells[x]||0) + value
+ xsums[y] = (xsums[y]||0) + value
+ ysums[x] = (ysums[x]||0) + value
sum += value
end
- xcells.each{|y,sum| cells[pivot_key(:sum,y)] = sum}
- ycells.each{|x,sum| cells[pivot_key(x,:sum)] = sum}
- cells[[:sum,:sum]] = sum
- cells
+ xsums.each{|y,sum| @cells[cell_key(:sum,y)] = sum}
+ ysums.each{|x,sum| @cells[cell_key(x,:sum)] = sum}
+ @cells[[:sum,:sum]] = sum
+ @cells
end
- def add_sums_to_axis axis, label = nil
+ def add_sum_label_to_axis axis, label = nil
label = 'Sum' unless String === label
case axis
when Array then axis + [[:sum, label]]

0 comments on commit 5800052

Please sign in to comment.