From: Brian Cassidy Date: Tue, 5 Aug 2008 15:26:38 +0000 (+0000) Subject: added a flatten_to_hash option to return a simple key-value hashref instead of the... X-Git-Tag: v0.13~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FConfig-Any.git;a=commitdiff_plain;h=aa7bd7c30e544ebbb418c2a0508523800e790836;hp=11501cf7c7fd46e8723c2de165a00e2f8deefc72 added a flatten_to_hash option to return a simple key-value hashref instead of the default "arrayref of hashrefs" (Pedro Figueiredo) --- diff --git a/Changes b/Changes index c522042..3b96ddf 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,8 @@ Revision history for Config-Any - ensure Config::Tiny parse errors are trapped - added tests for each format to ensure they throw parse errors - added a caveat regarding XML::Simple's strict mode (Peter Corlett) + - added a flatten_to_hash option to return a simple key-value hashref + instead of the default "arrayref of hashrefs" (Pedro Figueiredo) 0.12 Mon 07 Apr 2008 - ensure Perl loader dies on a failed require() (RT #32995) diff --git a/lib/Config/Any.pm b/lib/Config/Any.pm index 1b056e1..d774911 100644 --- a/lib/Config/Any.pm +++ b/lib/Config/Any.pm @@ -49,6 +49,7 @@ configuration formats. Config::Any->load_files( { files => \@files } ); Config::Any->load_files( { files => \@files, filter => \&filter } ); Config::Any->load_files( { files => \@files, use_ext => 1 } ); + Config::Any->load_files( { files => \@files, flatten_to_hash => 1 } ); C attempts to load configuration from the list of files passed in the C parameter, if the file exists. @@ -65,6 +66,9 @@ be aware that you will lose flexibility -- for example, a file called C or C would be. +When the C parameter is defined, the loader will return a hash +keyed on the file names, as opposed to the usual list of single-key hashes. + C also supports a 'force_plugins' parameter, whose value should be an arrayref of plugin names like C. Its intended use is to allow the use of a non-standard file extension while forcing it to be offered to a particular parser. @@ -95,6 +99,7 @@ sub load_files { Config::Any->load_stems( { stems => \@stems } ); Config::Any->load_stems( { stems => \@stems, filter => \&filter } ); Config::Any->load_stems( { stems => \@stems, use_ext => 1 } ); + Config::Any->load_stems( { stems => \@stems, flatten_to_hash => 1 } ); C attempts to load configuration from a list of files which it generates by combining the filename stems list passed in the C parameter with the @@ -193,6 +198,11 @@ sub _load { } } + if ( defined $args->{ flatten_to_hash } ) { + my %flattened = map { %$_ } @results; + return \%flattened; + } + return \@results; } diff --git a/t/61-features.t b/t/61-features.t index c1d983d..62bd59e 100644 --- a/t/61-features.t +++ b/t/61-features.t @@ -4,7 +4,7 @@ use warnings; $|++; -use Test::More tests => 10; +use Test::More tests => 14; use Scalar::Util qw(blessed reftype); use Config::Any; @@ -13,42 +13,71 @@ use Config::Any::INI; our $cfg_file = 't/conf/conf.foo'; eval { Config::Any::INI->load( $cfg_file ); }; + SKIP: { - skip "File loading backend for INI not found", 10 if $@; + skip "File loading backend for INI not found", 14 if $@; + + my $struct; # used to make sure parsing works for arrays and hashes - ok( my $c_arr = Config::Any->load_files( + # force_plugins + { + my $result = Config::Any->load_files( { files => [ $cfg_file ], force_plugins => [ qw(Config::Any::INI) ] } - ), - "load file with parser forced" - ); + ); - ok( my $c = $c_arr->[ 0 ], "load_files returns an arrayref" ); + ok( $result, 'load file with parser forced' ); - ok( ref $c, "load_files arrayref contains a ref" ); - my $ref = blessed $c ? reftype $c : ref $c; - is( substr( $ref, 0, 4 ), "HASH", "hashref" ); + ok( my $first = $result->[ 0 ], 'load_files returns an arrayref' ); + ok( ref $first, 'load_files arrayref contains a ref' ); - my ( $name, $cfg ) = each %$c; - is( $name, $cfg_file, "filename matches" ); + my $ref = blessed $first ? reftype $first : ref $first; + is( substr( $ref, 0, 4 ), 'HASH', 'hashref' ); - my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg; - is( substr( $cfgref, 0, 4 ), "HASH", "hashref cfg" ); + $struct = $first; - is( $cfg->{ name }, 'TestApp', "appname parses" ); - is( $cfg->{ Component }{ "Controller::Foo" }->{ foo }, - 'bar', "component->cntrlr->foo = bar" ); - is( $cfg->{ Model }{ "Model::Baz" }->{ qux }, - 'xyzzy', "model->model::baz->qux = xyzzy" ); + my ( $name, $cfg ) = %$first; + is( $name, $cfg_file, 'filenames match' ); - ok( my $c_arr_2 = Config::Any->load_files( - { files => [ $cfg_file ], - force_plugins => [ qw(Config::Any::INI) ], - use_ext => 1 + my $cfgref = blessed $cfg ? reftype $cfg : ref $cfg; + is( substr( $cfgref, 0, 4 ), 'HASH', 'hashref cfg' ); + + is( $cfg->{ name }, 'TestApp', 'appname parses' ); + is( $cfg->{ Component }{ "Controller::Foo" }->{ foo }, + 'bar', 'component->cntrlr->foo = bar' ); + is( $cfg->{ Model }{ "Model::Baz" }->{ qux }, + 'xyzzy', 'model->model::baz->qux = xyzzy' ); + } + + # flatten_to_hash + { + my $result = Config::Any->load_files( + { files => [ $cfg_file ], + force_plugins => [ qw(Config::Any::INI) ], + flatten_to_hash => 1 } - ), - "load file with parser forced" - ); + ); + + ok( $result, 'load file with parser forced, flatten to hash' ); + ok( ref $result, 'load_files hashref contains a ref' ); + + my $ref = blessed $result ? reftype $result : ref $result; + is( substr( $ref, 0, 4 ), 'HASH', 'hashref' ); + + is_deeply( $result, $struct, 'load_files return an hashref (flatten_to_hash)' ); + } + + # use_ext + { + ok( my $result = Config::Any->load_files( + { files => [ $cfg_file ], + force_plugins => [ qw(Config::Any::INI) ], + use_ext => 1 + } + ), + "load file with parser forced" + ); + } }