Report can define and retrieve columns

This commit is contained in:
Rohan Mitchell
2015-07-23 09:52:59 +10:00
parent 0a5e8fe629
commit c7a1ca29f4
4 changed files with 56 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
require 'open_food_network/reports/row'
module OpenFoodNetwork::Reports
class Report
# -- API
@@ -5,10 +7,19 @@ module OpenFoodNetwork::Reports
@@header
end
def columns
@@columns.to_a
end
# -- DSL
def self.header(*columns)
@@header = columns
end
def self.columns(&block)
@@columns = Row.new
@@columns.instance_eval(&block)
end
end
end

View File

@@ -0,0 +1,16 @@
module OpenFoodNetwork::Reports
class Row
def initialize
@columns = []
end
def column(&block)
@columns << block
end
def to_a
@columns
end
end
end

View File

@@ -3,13 +3,26 @@ require 'open_food_network/reports/report'
module OpenFoodNetwork::Reports
class TestReport < Report
header 'One', 'Two', 'Three'
columns do
column { |o| o[:one] }
column { |o| o[:two] }
column { |o| o[:three] }
end
end
describe Report do
let(:report) { TestReport.new }
let(:data) { {one: 1, two: 2, three: 3} }
it "returns the header" do
report.header.should == %w(One Two Three)
end
it "returns columns as an array of procs" do
report.columns[0].call(data).should == 1
report.columns[1].call(data).should == 2
report.columns[2].call(data).should == 3
end
end
end

View File

@@ -0,0 +1,16 @@
require 'open_food_network/reports/row'
module OpenFoodNetwork::Reports
describe Row do
let(:row) { Row.new }
let(:proc) { Proc.new {} }
it "can define a number of columns and return them as an array" do
row.column &proc
row.column &proc
row.column &proc
row.to_a.should == [proc, proc, proc]
end
end
end