Use class attributes instead of class variables to avoid leakage between subclasses

This commit is contained in:
Rohan Mitchell
2015-07-23 11:04:36 +10:00
parent 142e1d6d9a
commit cc9d0defca
2 changed files with 11 additions and 9 deletions

View File

@@ -3,20 +3,22 @@ require 'open_food_network/reports/rule'
module OpenFoodNetwork::Reports
class Report
class_attribute :_header, :_columns, :_rules_head
# -- API
def header
@@header
self._header
end
def columns
@@columns.to_a
self._columns.to_a
end
def rules
# Flatten linked list and return as hashes
rules = []
rule = @@rules_head
rule = self._rules_head
while rule
rules << rule
rule = rule.next
@@ -28,17 +30,17 @@ module OpenFoodNetwork::Reports
# -- DSL
def self.header(*columns)
@@header = columns
self._header = columns
end
def self.columns(&block)
@@columns = Row.new
@@columns.instance_eval(&block)
self._columns = Row.new
self._columns.instance_eval(&block)
end
def self.organise(&block)
@@rules_head = Rule.new
@@rules_head.instance_eval(&block)
self._rules_head = Rule.new
self._rules_head.instance_eval(&block)
end
end
end

View File

@@ -34,7 +34,7 @@ module OpenFoodNetwork::Reports
describe Report do
let(:report) { TestReport.new }
let(:rules_head) { TestReport.class_variable_get(:@@rules_head) }
let(:rules_head) { TestReport._rules_head }
let(:data) { {one: 1, two: 2, three: 3, four: 4} }
it "returns the header" do