aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/public_body_categories.rb29
-rw-r--r--spec/lib/public_body_categories_spec.rb42
2 files changed, 66 insertions, 5 deletions
diff --git a/lib/public_body_categories.rb b/lib/public_body_categories.rb
index b8058bf9e..b4aa71a40 100644
--- a/lib/public_body_categories.rb
+++ b/lib/public_body_categories.rb
@@ -7,9 +7,15 @@
# $Id: public_body_categories.rb,v 1.1 2009-09-14 14:45:48 francis Exp $
class PublicBodyCategories
-
- attr_reader :with_description, :with_headings, :tags, :by_tag, :singular_by_tag
-
+
+ attr_reader :with_description,
+ :with_headings,
+ :tags,
+ :by_tag,
+ :singular_by_tag,
+ :by_heading,
+ :headings
+
def initialize(categories)
@with_headings = categories
# Arranged in different ways for different sorts of displaying
@@ -17,8 +23,21 @@ class PublicBodyCategories
@tags = @with_description.map() { |a| a[0] }
@by_tag = Hash[*@with_description.map() { |a| a[0..1] }.flatten]
@singular_by_tag = Hash[*@with_description.map() { |a| [a[0],a[2]] }.flatten]
+ @by_heading = {}
+ heading = nil
+ @headings = []
+ @with_headings.each do |row|
+ if ! row.instance_of?(Array)
+ heading = row
+ @headings << row
+ @by_heading[row] = []
+ else
+ @by_heading[heading] << row[0]
+ end
+ end
end
+
def PublicBodyCategories.get
load_categories if @@CATEGORIES.empty?
@@CATEGORIES[I18n.locale.to_s] || @@CATEGORIES[I18n.default_locale.to_s] || PublicBodyCategories.new([])
@@ -28,10 +47,10 @@ class PublicBodyCategories
def PublicBodyCategories.add(locale, categories)
@@CATEGORIES[locale.to_s] = PublicBodyCategories.new(categories)
end
-
+
private
@@CATEGORIES = {}
-
+
def PublicBodyCategories.load_categories()
I18n.available_locales.each do |locale|
begin
diff --git a/spec/lib/public_body_categories_spec.rb b/spec/lib/public_body_categories_spec.rb
new file mode 100644
index 000000000..e53d9a028
--- /dev/null
+++ b/spec/lib/public_body_categories_spec.rb
@@ -0,0 +1,42 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe PublicBodyCategories do
+
+ before do
+ load_test_categories
+ end
+
+ describe 'when asked for categories with headings' do
+
+ it 'should return a list of headings as plain strings, each followed by n tag specifications as
+ lists in the form:
+ ["tag_to_use_as_category", "Sub category title", "Instance description"]' do
+ expected_categories = ["Local and regional", ["local_council",
+ "Local councils",
+ "a local council"],
+ "Miscellaneous", ["other",
+ "Miscellaneous",
+ "miscellaneous"]]
+ PublicBodyCategories::get().with_headings().should == expected_categories
+ end
+
+ end
+
+ describe 'when asked for headings' do
+
+ it 'should return a list of headings' do
+ PublicBodyCategories::get().headings().should == ['Local and regional', 'Miscellaneous']
+ end
+
+ end
+
+ describe 'when asked for tags by headings' do
+
+ it 'should return a hash of tags keyed by heading' do
+ PublicBodyCategories::get().by_heading().should == {'Local and regional' => ['local_council'],
+ 'Miscellaneous' => ['other']}
+ end
+
+ end
+
+end \ No newline at end of file