Report can define basic rules

This commit is contained in:
Rohan Mitchell
2015-07-23 10:08:38 +10:00
parent c7a1ca29f4
commit 66f64fc413
4 changed files with 56 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
require 'open_food_network/reports/row'
require 'open_food_network/reports/rule'
module OpenFoodNetwork::Reports
class Report
@@ -21,5 +22,10 @@ module OpenFoodNetwork::Reports
@@columns = Row.new
@@columns.instance_eval(&block)
end
def self.organise(&block)
@@rules_head = Rule.new
@@rules_head.instance_eval(&block)
end
end
end

View File

@@ -0,0 +1,16 @@
module OpenFoodNetwork::Reports
class Rule
def group(&block)
@group = block
end
def sort(&block)
@sort = block
end
def to_h
{group_by: @group, sort_by: @sort}
end
end
end

View File

@@ -9,10 +9,16 @@ module OpenFoodNetwork::Reports
column { |o| o[:two] }
column { |o| o[:three] }
end
organise do
group { |o| o[:one] }
sort { |o| o[:two] }
end
end
describe Report do
let(:report) { TestReport.new }
let(:rules_head) { TestReport.class_variable_get(:@@rules_head) }
let(:data) { {one: 1, two: 2, three: 3} }
it "returns the header" do
@@ -24,5 +30,15 @@ module OpenFoodNetwork::Reports
report.columns[1].call(data).should == 2
report.columns[2].call(data).should == 3
end
describe "rules" do
let(:group_by) { rules_head.to_h[:group_by] }
let(:sort_by) { rules_head.to_h[:sort_by] }
it "constructs a linked list of rules" do
group_by.call(data).should == 1
sort_by.call(data).should == 2
end
end
end
end

View File

@@ -0,0 +1,18 @@
require 'open_food_network/reports/rule'
module OpenFoodNetwork::Reports
describe Rule do
let(:rule) { Rule.new }
let(:proc) { Proc.new {} }
it "can define a group proc and return it in a hash" do
rule.group &proc
rule.to_h.should == {group_by: proc, sort_by: nil}
end
it "can define a sort proc and return it in a hash" do
rule.sort &proc
rule.to_h.should == {group_by: nil, sort_by: proc}
end
end
end