Skip to content
Browse files

Cardinality info is shown in the bottom of a report.

  • Loading branch information...
1 parent e265e1c commit ef54f18d4bc410a60fb51227896dd5c8c2883143 @MitinPavel MitinPavel committed Jan 11, 2012
Showing with 79 additions and 21 deletions.
  1. +31 −9 lib/tabloid/data.rb
  2. +4 −0 lib/tabloid/group.rb
  3. +37 −12 spec/lib/tabloid/data_spec.rb
  4. +7 −0 spec/lib/tabloid/group_spec.rb
View
40 lib/tabloid/data.rb
@@ -104,19 +104,33 @@ def summarize(key, block)
end
def summary_rows
- data_summary = report_columns.map do |col|
- if summarizer = @summary_options[col.key]
- summarize(col.key, self.send(summarizer)) unless col.hidden?
+ [Tabloid::HeaderRow.new(summary_label, :column_count => visible_column_count)].tap do |result|
+ if total_present?
+ result.push Tabloid::Row.new(:columns => @report_columns, :data => data_summary)
end
-
end
- [
- Tabloid::HeaderRow.new("Totals", :column_count => visible_column_count),
- Tabloid::Row.new(:columns => @report_columns,
- :data => data_summary)
+ end
- ]
+ def summary_label
+ ['Totals', cardinality_label].compact.join ' '
+ end
+ def cardinality_label
+ return nil unless cardinality_present?
+
+ cardinality = rows.map(&:cardinality).inject(&:+) || 0
+ units = cardinality > 1 ? "#{@grouping_options[:cardinality]}s" :
+ @cardinality_label
+
+ "(#{cardinality} #{units})"
+ end
+
+ def data_summary
+ report_columns.map do |col|
+ if summarizer = @summary_options[col.key]
+ summarize(col.key, self.send(summarizer)) unless col.hidden?
+ end
+ end
end
def visible_column_count
@@ -128,6 +142,14 @@ def sum
end
def summary_present?
+ total_present? || cardinality_present?
+ end
+
+ def cardinality_present?
+ !!@grouping_options[:cardinality]
+ end
+
+ def total_present?
!@summary_options.empty?
end
end
View
4 lib/tabloid/group.rb
@@ -19,6 +19,10 @@ def rows
@rows.dup + total_rows
end
+ def cardinality
+ @rows.size
+ end
+
def summarize(key, &block)
@rows[1..-1].inject(@rows[0].send(key)){|summary, row| block.call(summary, row.send(key)) }
end
View
49 spec/lib/tabloid/data_spec.rb
@@ -26,18 +26,19 @@
end
describe "summary" do
- let(:data){ Tabloid::Data.new(:report_columns => columns, :rows => rows, :summary => { :col2 => :sum } )}
- it "adds a totals row to the csv output" do
- csv_rows = FasterCSV.parse(data.to_csv)
- csv_rows.should include(["Totals", nil])
- csv_rows.should include([nil, "6"])
- end
- it "adds a totals row to the html output" do
- doc = Nokogiri::HTML(data.to_html)
- (doc/"tr.summary").should_not be_empty
- (doc/"tr.summary td.col2").text.should == "6"
+ context "[summary options is presented]" do
+ let(:data){ Tabloid::Data.new(:report_columns => columns, :rows => rows, :summary => { :col2 => :sum } )}
+ it "adds a totals row to the csv output" do
+ csv_rows = FasterCSV.parse(data.to_csv)
+ csv_rows.should include(["Totals", nil])
+ csv_rows.should include([nil, "6"])
+ end
+ it "adds a totals row to the html output" do
+ doc = Nokogiri::HTML(data.to_html)
+ (doc/"tr.summary").should_not be_empty
+ (doc/"tr.summary td.col2").text.should == "6"
+ end
end
-
context "[summary options is not presented]" do
let(:data) { Tabloid::Data.new(:report_columns => columns, :rows => rows ) }
@@ -51,7 +52,31 @@
(doc/"tr.summary").should be_empty
end
end
-
+ context "[summary and cardinality options is presented]" do
+ let(:data) do
+ Tabloid::Data.new(:report_columns => columns,
+ :rows => rows,
+ :summary => { :col2 => :sum },
+ :grouping_key => :col1,
+ :grouping_options => { :cardinality => 'foo' })
+ end
+ it "adds a totals row with cardinality info to the csv output" do
+ csv_rows = FasterCSV.parse(data.to_csv)
+ csv_rows.should include(["Totals (2 foos)", nil])
+ end
+ end
+ context "[cardinality options is presented]" do
+ let(:data) do
+ Tabloid::Data.new(:report_columns => columns,
+ :rows => rows,
+ :grouping_key => :col1,
+ :grouping_options => { :cardinality => 'foo' })
+ end
+ it "adds cardinality to the total label of the csv output" do
+ csv_rows = FasterCSV.parse(data.to_csv)
+ csv_rows.should include(["Totals (2 foos)", nil])
+ end
+ end
context "[empty report]" do
before do
data = Tabloid::Data.new(:report_columns => columns, :rows => [], :summary => { :col2 => :sum } )
View
7 spec/lib/tabloid/group_spec.rb
@@ -68,6 +68,13 @@
end
end
end
+ describe "#cardinality" do
+ it "counts rows" do
+ Tabloid::Group.new(:rows => [], :columns => columns).cardinality.should == 0
+ Tabloid::Group.new(:rows => [row1], :columns => columns).cardinality.should == 1
+ Tabloid::Group.new(:rows => [row1, row2], :columns => columns).cardinality.should == 2
+ end
+ end
context "cardinality" do
let(:columns) { [Tabloid::ReportColumn.new(:col1, "Column 1"), Tabloid::ReportColumn.new(:col2, "Column 2")] }
let(:row1) { Tabloid::Row.new(:columns => columns, :data => [1, 2]) }

0 comments on commit ef54f18

Please sign in to comment.
Something went wrong with that request. Please try again.