Report rules can define a summary row

This commit is contained in:
Rohan Mitchell
2015-07-23 10:35:40 +10:00
parent 07eb857a8d
commit 1d39fb4438
3 changed files with 34 additions and 1 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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