diff --git a/lib/open_food_network/reports/report.rb b/lib/open_food_network/reports/report.rb index ad7c09ae7c..b23f77eeeb 100644 --- a/lib/open_food_network/reports/report.rb +++ b/lib/open_food_network/reports/report.rb @@ -12,6 +12,19 @@ module OpenFoodNetwork::Reports @@columns.to_a end + def rules + # Flatten linked list and return as hashes + rules = [] + + rule = @@rules_head + while rule + rules << rule + rule = rule.next + end + + rules.map &:to_h + end + # -- DSL def self.header(*columns) diff --git a/spec/lib/open_food_network/reports/report_spec.rb b/spec/lib/open_food_network/reports/report_spec.rb index 7bbbccddb0..e56a283332 100644 --- a/spec/lib/open_food_network/reports/report_spec.rb +++ b/spec/lib/open_food_network/reports/report_spec.rb @@ -1,27 +1,32 @@ require 'open_food_network/reports/report' module OpenFoodNetwork::Reports + P1 = Proc.new { |o| o[:one] } + P2 = Proc.new { |o| o[:two] } + P3 = Proc.new { |o| o[:three] } + P4 = Proc.new { |o| o[:four] } + class TestReport < Report header 'One', 'Two', 'Three', 'Four' columns do - column { |o| o[:one] } - column { |o| o[:two] } - column { |o| o[:three] } - column { |o| o[:four] } + column &P1 + column &P2 + column &P3 + column &P4 end organise do - group { |o| o[:one] } - sort { |o| o[:two] } + group &P1 + sort &P2 organise do - group { |o| o[:three] } - sort { |o| o[:four] } + group &P3 + sort &P4 summary_row do - column { |o| o[:one] } - column { |o| o[:four] } + column &P1 + column &P4 end end end @@ -65,5 +70,12 @@ module OpenFoodNetwork::Reports next_summary_columns[1].call(data).should == 4 end end + + describe "outputting rules" do + it "outputs the rules" do + report.rules.should == [{group_by: P1, sort_by: P2}, + {group_by: P3, sort_by: P4, summary_columns: [P1, P4]}] + end + end end end