From 1d39fb4438f1923bb97234341a11db5d703cea0b Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 23 Jul 2015 10:35:40 +1000 Subject: [PATCH] Report rules can define a summary row --- lib/open_food_network/reports/rule.rb | 11 ++++++++++- spec/lib/open_food_network/reports/report_spec.rb | 11 +++++++++++ spec/lib/open_food_network/reports/rule_spec.rb | 13 +++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/open_food_network/reports/rule.rb b/lib/open_food_network/reports/rule.rb index 676af48ffc..e2b0b0e255 100644 --- a/lib/open_food_network/reports/rule.rb +++ b/lib/open_food_network/reports/rule.rb @@ -1,3 +1,5 @@ +require 'open_food_network/reports/row' + module OpenFoodNetwork::Reports class Rule attr_reader :next @@ -10,13 +12,20 @@ module OpenFoodNetwork::Reports @sort = block end + def summary_row(&block) + @summary_row = Row.new + @summary_row.instance_eval(&block) + end + def organise(&block) @next = Rule.new @next.instance_eval &block end def to_h - {group_by: @group, sort_by: @sort} + h = {group_by: @group, sort_by: @sort} + h.merge!({summary_columns: @summary_row.to_a}) if @summary_row + h end end end diff --git a/spec/lib/open_food_network/reports/report_spec.rb b/spec/lib/open_food_network/reports/report_spec.rb index 72b64dbc28..7bbbccddb0 100644 --- a/spec/lib/open_food_network/reports/report_spec.rb +++ b/spec/lib/open_food_network/reports/report_spec.rb @@ -18,6 +18,11 @@ module OpenFoodNetwork::Reports organise do group { |o| o[:three] } sort { |o| o[:four] } + + summary_row do + column { |o| o[:one] } + column { |o| o[:four] } + end end end end @@ -43,6 +48,7 @@ module OpenFoodNetwork::Reports let(:sort_by) { rules_head.to_h[:sort_by] } let(:next_group_by) { rules_head.next.to_h[:group_by] } let(:next_sort_by) { rules_head.next.to_h[:sort_by] } + let(:next_summary_columns) { rules_head.next.to_h[:summary_columns] } it "constructs the head of the rules list" do group_by.call(data).should == 1 @@ -53,6 +59,11 @@ module OpenFoodNetwork::Reports next_group_by.call(data).should == 3 next_sort_by.call(data).should == 4 end + + it "constructs summary columns for rules" do + next_summary_columns[0].call(data).should == 1 + next_summary_columns[1].call(data).should == 4 + end end end end diff --git a/spec/lib/open_food_network/reports/rule_spec.rb b/spec/lib/open_food_network/reports/rule_spec.rb index 16a7e49d0d..0e7a1d979b 100644 --- a/spec/lib/open_food_network/reports/rule_spec.rb +++ b/spec/lib/open_food_network/reports/rule_spec.rb @@ -19,5 +19,18 @@ module OpenFoodNetwork::Reports rule.organise &proc rule.next.should be_a Rule end + + it "can define a summary row and return it in a hash" do + rule.summary_row do + column {} + column {} + column {} + end + + rule.to_h[:summary_columns].count.should == 3 + rule.to_h[:summary_columns][0].should be_a Proc + rule.to_h[:summary_columns][1].should be_a Proc + rule.to_h[:summary_columns][2].should be_a Proc + end end end