mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Report can define and retrieve columns
This commit is contained in:
@@ -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
|
||||
|
||||
16
lib/open_food_network/reports/row.rb
Normal file
16
lib/open_food_network/reports/row.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
16
spec/lib/open_food_network/reports/row_spec.rb
Normal file
16
spec/lib/open_food_network/reports/row_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user