Permalink
Browse files

Make sure config objects survive if config loaded more than once.

  • Loading branch information...
1 parent 40f5280 commit 5b3260edbb1f6848296b59a3937236b1f8c4814b Ovid committed Mar 22, 2012
Showing with 14 additions and 4 deletions.
  1. +12 −3 lib/Dancer/Config/Object.pm
  2. +2 −1 t/01_config/07_strict_config.t
@@ -6,6 +6,7 @@ use warnings;
use base 'Exporter';
use Carp 'croak';
use Dancer::Exception qw(:all);
+use Scalar::Util 'blessed';
register_exception('BadConfigMethod',
message_pattern =>
@@ -21,9 +22,17 @@ our @EXPORT_OK = qw(hashref_to_object);
my $class = __PACKAGE__;
my $target = "${class}::__ANON__$index";
$index++;
- unless ('HASH' eq ref $hashref) {
- # should never happen
- raise 'Core::Config' => "Argument to $class must be a hashref";
+ if ('HASH' ne ref $hashref) {
+ if ( blessed $hashref ) {
+ # we have already converted this to an object. This can happen
+ # in cases where Dancer::Config->load is called more than
+ # once.
+ return $hashref;
+ }
+ else {
+ # should never happen
+ raise 'Core::Config' => "Argument to $class must be a hashref";
+ }
}
my $object = bless $hashref => $target;
_add_methods($object);
@@ -6,7 +6,7 @@ plan skip_all => "YAML needed to run this tests"
unless Dancer::ModuleLoader->load('YAML');
plan skip_all => "File::Temp 0.22 required"
unless Dancer::ModuleLoader->load( 'File::Temp', '0.22' );
-plan tests => 11;
+plan tests => 12;
use Dancer ':syntax';
use File::Spec;
@@ -42,6 +42,7 @@ strict_config: 1
END
write_file($conffile => $conf);
ok(Dancer::Config->load, 'Config load works with a conffile');
+ok(Dancer::Config->load, '... and it should be safe to call more than once');
can_ok config, 'port';
is config->port, '4500', 'basic methods should work with strict configs';

0 comments on commit 5b3260e

Please sign in to comment.