[Catalyst-commits] r7125 - in trunk/Config-Any: . lib/Config lib/Config/Any t t/multi

bricas at dev.catalyst.perl.org bricas at dev.catalyst.perl.org
Tue Nov 13 15:21:57 GMT 2007


Author: bricas
Date: 2007-11-13 15:21:57 +0000 (Tue, 13 Nov 2007)
New Revision: 7125

Added:
   trunk/Config-Any/t/62-multi.t
   trunk/Config-Any/t/multi/
   trunk/Config-Any/t/multi/conf.yml
Modified:
   trunk/Config-Any/Changes
   trunk/Config-Any/lib/Config/Any.pm
   trunk/Config-Any/lib/Config/Any/General.pm
   trunk/Config-Any/lib/Config/Any/INI.pm
   trunk/Config-Any/lib/Config/Any/JSON.pm
   trunk/Config-Any/lib/Config/Any/Perl.pm
   trunk/Config-Any/lib/Config/Any/XML.pm
   trunk/Config-Any/lib/Config/Any/YAML.pm
Log:
refactor _load(). fix an issue with use_ext => 0. use_ext now on by default. throw errors when use_ext => 1. allow multi-stream configs. pod updates

Modified: trunk/Config-Any/Changes
===================================================================
--- trunk/Config-Any/Changes	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/Changes	2007-11-13 15:21:57 UTC (rev 7125)
@@ -1,34 +1,38 @@
 Revision history for Config-Any
 
-0.09 Thu Nov 08 2007
-    code and pod cleanups
+0.09_01 Tue Nov 13 2007
+    - code and pod cleanups
+    - use_ext is now on by default
+    - when use_ext is on, if a parser throws an error, we throw an error
+    - fix case where use_ext is defined and false, but was behaving like
+      use_ext => 1
+    - allow loaders to return multiple documents as an array.
 
 0.08 Thu Aug 23 2007
-    pass config options to each parser
-    fix for loading the same perl config twice (RT #28812)
+    - pass config options to each parser
+    - fix for loading the same perl config twice (RT #28812)
 
 0.07 Mon Feb 26 2007
-    promote 0.06_01 to non-dev.
+    - promote 0.06_01 to non-dev.
 
 0.06_01 Sun Feb 25 19:23:00 2007
-    fixed bug [rt.cpan.org #25143] make tests fails
-        - t/61_features.t had 1 more test added than was set to skip if the INI parser
-          was not installed. Fixed by s/9/10/ on the skip() line.
+    - fixed bug [rt.cpan.org #25143] make tests fails
+      + t/61_features.t had 1 more test added than was set to skip if the INI
+        parser was not installed. Fixed by s/9/10/ on the skip() line.
 
 0.06 Thu Feb 22 21:05:00 2007
-    removed reference to Test::Exception, bumped version number
+    - removed reference to Test::Exception, bumped version number
 
 0.05  Wed Feb 21 22:00:00 2007
-    added support for:
-        - 'force_plugins => [ qw(Config::Any::Foo Config::Any::Blah) ]' parameter
-	  to load_(files|stems)
-        - $Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY - boolean, defaulting
-          to on, controlling whether to map spaces in INI section headings 
-          to nested hashrefs 
-        both as requested by Evan Kaufman	
+    - added support for (requested by Evan Kaufman):
+      + 'force_plugins => [ qw(Config::Any::Foo Config::Any::Blah) ]' parameter
+	    to load_(files|stems)
+      + $Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY - boolean, defaulting
+        to on, controlling whether to map spaces in INI section headings 
+        to nested hashrefs
 
 0.04  Mon Aug  7 15:15:15 2006
-    Initial CPAN-worthy release with proper test suite
+    - Initial CPAN-worthy release with proper test suite
 
 0.01 - 0.03 dev releases
 

Modified: trunk/Config-Any/lib/Config/Any/General.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any/General.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any/General.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -70,23 +70,15 @@
 
 =head1 AUTHOR
 
-=over 4 
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
 
-=item * Brian Cassidy E<lt>bricas at cpan.orgE<gt>
-
-=back
-
 =head1 CONTRIBUTORS
 
-=over 4
+Joel Bernstein C<< <rataxis at cpan.org> >>
 
-=item * Joel Bernstein C<< <rataxis at cpan.org> >>
-
-=back
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Brian Cassidy
+Copyright 2007 by Brian Cassidy
 
 Portions Copyright 2006 Portugal Telecom
 

Modified: trunk/Config-Any/lib/Config/Any/INI.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any/INI.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any/INI.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -84,19 +84,15 @@
 
 =back
 
-=head1 AUTHOR
+=head1 AUTHORS
 
-=over 4 
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
 
-=item * Brian Cassidy E<lt>bricas at cpan.orgE<gt>
+Joel Bernstein E<lt>rataxis at cpan.orgE<gt>
 
-=item * Joel Bernstein E<lt>rataxis at cpan.orgE<gt>
-
-=back
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Brian Cassidy, portions copyright 2006, 2007 by Joel Bernstein
+Copyright 2007 by Brian Cassidy, portions copyright 2006, 2007 by Joel Bernstein
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 

Modified: trunk/Config-Any/lib/Config/Any/JSON.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any/JSON.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any/JSON.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -59,15 +59,11 @@
 
 =head1 AUTHOR
 
-=over 4 
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
 
-=item * Brian Cassidy E<lt>bricas at cpan.orgE<gt>
-
-=back
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Brian Cassidy
+Copyright 2007 by Brian Cassidy
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 

Modified: trunk/Config-Any/lib/Config/Any/Perl.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any/Perl.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any/Perl.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -56,15 +56,11 @@
 
 =head1 AUTHOR
 
-=over 4 
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
 
-=item * Brian Cassidy E<lt>bricas at cpan.orgE<gt>
-
-=back
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Brian Cassidy
+Copyright 2007 by Brian Cassidy
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 

Modified: trunk/Config-Any/lib/Config/Any/XML.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any/XML.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any/XML.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -73,19 +73,15 @@
     $out;
 }
 
-=head1 AUTHOR
+=head1 AUTHORS
 
-=over 4 
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
 
-=item * Brian Cassidy E<lt>bricas at cpan.orgE<gt>
+Joel Bernstein E<lt>rataxis at cpan.orgE<gt>
 
-=item * Joel Bernstein E<lt>rataxis at cpan.orgE<gt>
-
-=back
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Brian Cassidy
+Copyright 2007 by Brian Cassidy
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 

Modified: trunk/Config-Any/lib/Config/Any/YAML.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any/YAML.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any/YAML.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -56,15 +56,11 @@
 
 =head1 AUTHOR
 
-=over 4 
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
 
-=item * Brian Cassidy E<lt>bricas at cpan.orgE<gt>
-
-=back
-
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Brian Cassidy
+Copyright 2007 by Brian Cassidy
 
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 

Modified: trunk/Config-Any/lib/Config/Any.pm
===================================================================
--- trunk/Config-Any/lib/Config/Any.pm	2007-11-11 16:14:53 UTC (rev 7124)
+++ trunk/Config-Any/lib/Config/Any.pm	2007-11-13 15:21:57 UTC (rev 7125)
@@ -6,7 +6,7 @@
 use Carp;
 use Module::Pluggable::Object ();
 
-our $VERSION = '0.09';
+our $VERSION = '0.09_01';
 
 =head1 NAME
 
@@ -129,64 +129,70 @@
     return $class->_load( $args );
 }
 
-# this is where we do the real work
-# it's a private class-method because users should use the interface described
-# in the POD.
 sub _load {
     my ( $class, $args ) = @_;
-    my ( $files_ref, $filter_cb, $use_ext, $force_plugins_ref )
-        = @{ $args }{ qw(files filter use_ext force_plugins) };
-    croak "_load requires a arrayref of file paths" unless defined $files_ref;
+    croak "_load requires a arrayref of file paths" unless $args->{ files };
 
-    my %files         = map { $_ => 1 } @$files_ref;
-    my %force_plugins = map { $_ => 1 } @$force_plugins_ref;
-    my $enforcing = keys %force_plugins ? 1 : 0;
+    if( !defined $args->{ use_ext } ) {
+        warn "use_ext argument was not explicitly set, as of 0.09, this is true by default";
+        $args->{ use_ext } = 1;
+    }
 
-    my $final_configs     = [];
-    my $originally_loaded = {};
+    # figure out what plugins we're using
+    my $force   = defined $args->{ force_plugins };
+    my @plugins = $force ? @{ $args->{ force_plugins } } : $class->plugins;
 
-    # perform a separate file loop for each loader
-    for my $loader ( $class->plugins ) {
-        next if $enforcing && not defined $force_plugins{ $loader };
-        last unless keys %files;
-        my %ext = map { $_ => 1 } $loader->extensions;
+    # map extensions if we have to
+    my( %extension_lut, $extension_re );
+    my $use_ext_lut = !$force && $args->{ use_ext };
+    if( $use_ext_lut ) {
+        for my $plugin ( @plugins ) {
+            $extension_lut{ $_ } = $plugin for $plugin->extensions;
+        }
 
-        my ( $loader_class ) = $loader =~ /::([^:]+)$/;
-        my $driver_args = $args->{ driver_args }{ $loader_class } || {};
+        $extension_re = join( '|', keys %extension_lut );
+    }
 
-    FILE:
-        for my $filename ( keys %files ) {
-            next unless -f $filename;
+    # map args to plugins
+    my $base_class = __PACKAGE__;
+    my %loader_args;
+    for my $plugin ( @plugins ) {
+        $plugin =~ m{^$base_class\::(.+)};
+        $loader_args{ $plugin } = $args->{ driver_args }->{ $1 } || {};
+    }
 
-       # use file extension to decide whether this loader should try this file
-       # use_ext => 1 hits this block
-            if ( defined $use_ext && !$enforcing ) {
-                my $matched_ext = 0;
-            EXT:
-                for my $e ( keys %ext ) {
-                    next EXT unless $filename =~ m{ \. $e \z }xms;
-                    next FILE unless exists $ext{ $e };
-                    $matched_ext = 1;
-                }
+    my @results;
 
-                next FILE unless $matched_ext;
-            }
+    for my $filename ( @{ $args->{ files } } ) {
+        # don't even bother if it's not there
+        next unless -f $filename;
 
-            my $config;
-            eval { $config = $loader->load( $filename, $driver_args ); };
+        my @try_plugins = @plugins;
 
-            next if $@;         # if it croaked or warned, we can't use it
-            next if !$config;
-            delete $files{ $filename };
+        if( $use_ext_lut ) {
+            $filename =~ m{\.($extension_re)\z};
+            next unless $1;
+            @try_plugins = $extension_lut{ $1 };
+        }
 
-            # post-process config with a filter callback, if we got one
-            $filter_cb->( $config ) if defined $filter_cb;
+        for my $loader ( @try_plugins ) {
+            my @configs = eval { $loader->load( $filename, $loader_args{ $loader } ); };
 
-            push @$final_configs, { $filename => $config };
+            # fatal error if we used extension matching
+            croak "Error parsing file: $filename" if $@ and $use_ext_lut;
+            next if $@ or !@configs;
+
+            # post-process config with a filter callback
+            if ( $args->{ filter } ) {
+                $args->{ filter }->( $_ ) for @configs;
+            }
+
+            push @results, { $filename => @configs == 1 ? $configs[ 0 ] : \@configs };
+            last;
         }
     }
 
-    return $final_configs;
+    return \@results;
 }
 
 =head2 finder( )

Added: trunk/Config-Any/t/62-multi.t
===================================================================
--- trunk/Config-Any/t/62-multi.t	                        (rev 0)
+++ trunk/Config-Any/t/62-multi.t	2007-11-13 15:21:57 UTC (rev 7125)
@@ -0,0 +1,28 @@
+use Test::More tests => 3;
+
+use strict;
+use warnings;
+
+use Config::Any;
+use Config::Any::YAML;
+
+my $file   = 't/multi/conf.yml';
+my @expect = (
+    {   name  => 'TestApp',
+        Model => { 'Model::Baz' => { qux => 'xyzzy' } }
+    },
+    {   name2  => 'TestApp2',
+        Model2 => { 'Model::Baz2' => { qux2 => 'xyzzy2' } }
+    },
+);
+
+my @results = eval { Config::Any::YAML->load( $file ); };
+
+SKIP: {
+    skip "Can't load YAML files", 3 if $@;
+    is( @results, 2, '2 documents' );
+    is_deeply( \@results, \@expect, 'structures ok' );
+
+    my $return = Config::Any->load_files( { use_ext => 1, files => [ $file ] } );
+    is_deeply( $return, [ { $file => \@expect } ], 'config-any structures ok' );
+}

Added: trunk/Config-Any/t/multi/conf.yml
===================================================================
--- trunk/Config-Any/t/multi/conf.yml	                        (rev 0)
+++ trunk/Config-Any/t/multi/conf.yml	2007-11-13 15:21:57 UTC (rev 7125)
@@ -0,0 +1,11 @@
+---
+name: TestApp
+Model:
+    Model::Baz:
+        qux: xyzzy
+---
+name2: TestApp2
+Model2:
+    Model::Baz2:
+        qux2: xyzzy2
+




More information about the Catalyst-commits mailing list