Skip Menu |
 

This queue is for tickets about the MooseX-SimpleConfig CPAN distribution.

Report information
The Basics
Id: 48552
Status: resolved
Priority: 0/
Queue: MooseX-SimpleConfig

People
Owner: bobtfish [...] bobtfish.net
Requestors: XSAWYERX [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in:
  • 0.01
  • 0.02
  • 0.03
Fixed in: (no value)



Subject: Allowing multiple configuration files
Download (untitled) / with headers
text/plain 500b
I love MooseX::SimpleConfig. Unfortunately it doesn't support multiple configuration files. I included a patch that I wrote. I might have implemented different than you would want, but I tried to keep it as true to the way you wrote it. This patch should be accompanied with a patch to MooseX::ConfigFromFile, that changes the attribute type to allow either a Path or an ArrayRef of a Path. I am opening another ticket with MooseX::ConfigFromFile and including a patch for that as well. Thank you.
Subject: mx_simpleconfig-allow_multiple_configs.patch
--- /usr/local/share/perl/5.8.8/MooseX/SimpleConfig.pm 2008-01-23 08:06:35.000000000 +0200 +++ lib/MooseX/SimpleConfig.pm 2009-07-20 10:50:42.000000000 +0300 @@ -10,22 +10,27 @@ sub get_config_from_file { my ($class, $file) = @_; + my $files_ref = ref $file eq 'ARRAY' ? + $file : + [ $file ]; + my $raw_cfany = Config::Any->load_files({ - files => [ $file ], + files => $files_ref, use_ext => 1, + 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 = $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}; + } + %raw_config = ( %raw_config, %{ $raw_cfany->{$file_tested} } ); + } - die "configfile must represent a hash structure" - unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH'; + \%raw_config; - $raw_config; } no Moose::Role; 1;
Download (untitled) / with headers
text/plain 382b
Hi. Since I opened this ticket, there was another release of MooseX::SimpleConfig, which didn't include my change. Version 0.04 broke my original patch so I included a new one, which is a bit better than the previous one since it keeps a check that the original one omitted. Also, this time I added a test that checks multiple files can be read successfully. Thank you, Sawyer X.
Subject: mx_simpleconfig-allow_multiple_configs.patch
--- MooseX-SimpleConfig-0.04/lib/MooseX/SimpleConfig.pm 2009-11-04 11:41:36.000000000 +0200 +++ MooseX-SimpleConfig-0.04_patched/lib/MooseX/SimpleConfig.pm 2010-01-19 22:37:19.000000000 +0200 @@ -10,27 +10,34 @@ 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 = $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 $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;
Download (untitled) / with headers
text/plain 216b
On Tue Jan 19 16:29:48 2010, xsawyerx wrote: Show quoted text
> Also, this time I added a test that checks multiple files can be read > successfully.
For some reason it didn't upload the test. Here it is. Show quoted text
> Thank you, > Sawyer X.
Subject: multiple.t
Download multiple.t
text/x-perl 1.2k
#!/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'); }


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.