From: Tomas Doran Date: Fri, 22 Jan 2010 21:09:18 +0000 (+0000) Subject: Allow multiple config files, from RT#48552 X-Git-Tag: 0.05~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c67f89a723665ec73e4096b304eccf01362d8603;p=gitmo%2FMooseX-SimpleConfig.git Allow multiple config files, from RT#48552 --- diff --git a/lib/MooseX/SimpleConfig.pm b/lib/MooseX/SimpleConfig.pm index b64d97d..b9ea981 100644 --- a/lib/MooseX/SimpleConfig.pm +++ b/lib/MooseX/SimpleConfig.pm @@ -10,27 +10,34 @@ use Config::Any (); sub get_config_from_file { my ($class, $file) = @_; + my $files_ref = ref $file eq 'ARRAY' ? $file : [$file]; + my $can_config_any_args = $class->can('config_any_args'); my $extra_args = $can_config_any_args ? $can_config_any_args->($class, $file) : {}; ; my $raw_cfany = Config::Any->load_files({ - use_ext => 1, %$extra_args, - files => [ $file ] + use_ext => 1, + files => $files_ref, + flatten_to_hash => 1, } ); - die q{Specified configfile '} . $file - . q{' does not exist, is empty, or is not readable} - unless $raw_cfany->[0] - && exists $raw_cfany->[0]->{$file}; + my %raw_config; + foreach my $file_tested ( @{$files_ref} ) { + if ( ! exists $raw_cfany->{$file_tested} ) { + die qq{Specified configfile '$file_tested' does not exist, } . + q{is empty, or is not readable}; + } - my $raw_config = $raw_cfany->[0]->{$file}; + my $cfany_hash = $raw_cfany->{$file_tested}; + die "configfile must represent a hash structure in file: $file_tested" + unless $cfany_hash && ref $cfany_hash && ref $cfany_hash eq 'HASH'; - die "configfile must represent a hash structure" - unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH'; + %raw_config = ( %raw_config, %{$cfany_hash} ); + } - $raw_config; + \%raw_config; } no Moose::Role; 1; diff --git a/t/multiple.t b/t/multiple.t new file mode 100644 index 0000000..ea69904 --- /dev/null +++ b/t/multiple.t @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use lib 't/lib'; +use lib '../t/lib'; + +BEGIN { + use Test::More; + + eval "use YAML::Syck ()"; + if($@) { + eval "use YAML ()"; + if($@) { + plan skip_all => "YAML or YAML::Syck required for this test"; + } + } + + plan tests => 5; + + use_ok('MXSimpleConfigTest'); +} + +# Can it load a multiple YAML files with options +{ + my $test_yaml; # generic filehandle + open $test_yaml, '>', 'test.yaml' or die "Cannot create test.yaml: $!"; + print {$test_yaml} "direct_attr: 123\ninherited_ro_attr: asdf\n"; + close $test_yaml or die "Cannot close test.yaml: $!"; + + open $test_yaml, '>', 'test2.yaml' or die "Cannot create test2.yaml: $!"; + print {$test_yaml} "req_attr: foo\n"; + close $test_yaml or die "Cannot close test.yaml: $!"; + + my $foo = eval { + MXSimpleConfigTest->new_with_config( + configfile => [ 'test.yaml', 'test2.yaml' ] + ); + }; + ok(!$@, 'Did not die with two YAML config files') + or diag $@; + + is($foo->req_attr, 'foo', 'req_attr works'); + is($foo->direct_attr, 123, 'direct_attr works'); + is($foo->inherited_ro_attr, 'asdf', 'inherited_ro_attr works'); +} + +END { + unlink('test.yaml'); + unlink('test2.yaml'); +}