aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdmund von der Burg <evdb@mysociety.org>2011-02-23 18:47:07 +0000
committerEdmund von der Burg <evdb@mysociety.org>2011-02-23 18:47:07 +0000
commitb7df296229dcca8dd35f2138826e5415e0edede9 (patch)
tree5b5d793837caed3edb6c40102984e3d24ad324c0
parentfe18ee66aa7d97a070990731316152b74b132e65 (diff)
Allow access to the config through FixMyStreet
-rw-r--r--Makefile.PL2
-rw-r--r--perllib/FixMyStreet.pm32
-rw-r--r--t/fixmystreet.t23
3 files changed, 53 insertions, 4 deletions
diff --git a/Makefile.PL b/Makefile.PL
index b16129166..44e35d4a6 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -16,6 +16,8 @@ requires 'Catalyst::Action::RenderView';
requires 'Moose';
requires 'namespace::autoclean';
requires 'Config::General';
+requires 'Path::Class';
+requires 'Readonly';
test_requires 'Test::More' => '0.88';
test_requires 'Test::WWW::Mechanize::Catalyst';
diff --git a/perllib/FixMyStreet.pm b/perllib/FixMyStreet.pm
index 25c4599d3..8d50c86e0 100644
--- a/perllib/FixMyStreet.pm
+++ b/perllib/FixMyStreet.pm
@@ -4,6 +4,15 @@ use strict;
use warnings;
use Path::Class;
+my $ROOT_DIR = file(__FILE__)->parent->parent->absolute;
+
+use Readonly;
+
+use mySociety::Config;
+
+# load the config file and store the contents in a readonly hash
+mySociety::Config::set_file( __PACKAGE__->path_to("conf/general") );
+Readonly::Hash my %CONFIG, %{ mySociety::Config::get_list() };
=head1 NAME
@@ -27,13 +36,30 @@ the FixMyStreet directory.
=cut
-my $ROOT_DIR = file(__FILE__)->parent->parent->absolute;
-
sub path_to {
- my $self = shift;
+ my $class = shift;
return $ROOT_DIR->file(@_);
}
+=head2 config
+
+ my $config_hash_ref = FixMyStreet->config();
+ my $config_value = FixMyStreet->config($key);
+Returns a hashref to the config values. This is readonly so any attempt to
+change it will fail.
+
+Or you can pass it a key and it will return the value for that key, or undef if
+it can't find it.
+
+=cut
+
+sub config {
+ my $class = shift;
+ return \%CONFIG unless scalar @_;
+
+ my $key = shift;
+ return exists $CONFIG{$key} ? $CONFIG{$key} : undef;
+}
1;
diff --git a/t/fixmystreet.t b/t/fixmystreet.t
index c1b3f0075..d7f00b047 100644
--- a/t/fixmystreet.t
+++ b/t/fixmystreet.t
@@ -2,7 +2,8 @@ use strict;
use warnings;
use Path::Class;
-use Test::More tests => 4;
+use Test::More;
+use Test::Exception;
use_ok 'FixMyStreet';
@@ -14,3 +15,23 @@ isa_ok $path_to_path, 'Path::Class::File';
ok $path_to_path->is_absolute, "path is absolute";
is "$path_to_path", $file_path, "got $file_path";
+# check that the config gets loaded and is immutable
+my $config = FixMyStreet->config;
+isa_ok $config, 'HASH';
+is $config->{GAZE_URL}, 'http://gaze.mysociety.org/gaze',
+ "got GAZE_URL correctly";
+throws_ok(
+ sub { $config->{GAZE_URL} = 'some other value'; },
+ qr/Modification of a read-only value attempted/,
+ 'attempt to change config caught'
+);
+is $config->{GAZE_URL}, 'http://gaze.mysociety.org/gaze', "GAZE_URL unchanged";
+
+# check that we can get the value by key as well
+is FixMyStreet->config('GAZE_URL'), 'http://gaze.mysociety.org/gaze',
+ "GAZE_URL correct when got by key";
+is FixMyStreet->config('BAD_KEY_DOES_NOT_EXIST'), undef, "config miss is undef";
+
+# all done
+done_testing();
+