[Catalyst-commits] r8348 - in branches/Config-Any/unsupported_error: . lib/Config lib/Config/Any t t/conf t/lib t/lib/Config t/lib/Config/Any

bricas at dev.catalyst.perl.org bricas at dev.catalyst.perl.org
Wed Sep 3 14:55:06 BST 2008


Author: bricas
Date: 2008-09-03 14:55:06 +0100 (Wed, 03 Sep 2008)
New Revision: 8348

Added:
   branches/Config-Any/unsupported_error/lib/Config/Any/Base.pm
   branches/Config-Any/unsupported_error/t/63-unsupported.t
   branches/Config-Any/unsupported_error/t/conf/conf.unsupported
   branches/Config-Any/unsupported_error/t/lib/
   branches/Config-Any/unsupported_error/t/lib/Config/
   branches/Config-Any/unsupported_error/t/lib/Config/Any/
   branches/Config-Any/unsupported_error/t/lib/Config/Any/Unsupported.pm
Modified:
   branches/Config-Any/unsupported_error/Changes
   branches/Config-Any/unsupported_error/lib/Config/Any.pm
   branches/Config-Any/unsupported_error/lib/Config/Any/General.pm
   branches/Config-Any/unsupported_error/lib/Config/Any/INI.pm
   branches/Config-Any/unsupported_error/lib/Config/Any/JSON.pm
   branches/Config-Any/unsupported_error/lib/Config/Any/Perl.pm
   branches/Config-Any/unsupported_error/lib/Config/Any/XML.pm
   branches/Config-Any/unsupported_error/lib/Config/Any/YAML.pm
   branches/Config-Any/unsupported_error/t/50-general.t
   branches/Config-Any/unsupported_error/t/51-ini.t
   branches/Config-Any/unsupported_error/t/52-json.t
   branches/Config-Any/unsupported_error/t/53-perl.t
   branches/Config-Any/unsupported_error/t/54-xml.t
   branches/Config-Any/unsupported_error/t/55-yaml.t
   branches/Config-Any/unsupported_error/t/61-features.t
Log:
when use_ext is true, we will check to see if there are no supported modules for a particular file. instead of the file being skipped, an error will be thrown.
officially support multiple loaders per extension.
add a Config::Any::Base for all loaders to inherit from, plus add a new dependency mechanism: requires_any_of() and requires_all_of().

Modified: branches/Config-Any/unsupported_error/Changes
===================================================================
--- branches/Config-Any/unsupported_error/Changes	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/Changes	2008-09-03 13:55:06 UTC (rev 8348)
@@ -1,5 +1,13 @@
 Revision history for Config-Any
 
+0.15 XXX
+    - when use_ext is true, we will check to see if there are no supported
+      modules for a particular file. instead of the file being skipped, an
+      error will be thrown.
+    - officially support multiple loaders per extension
+    - add a Config::Any::Base for all loaders to inherit from, plus add
+      a new dependency mechanism: requires_any_of() and requires_all_of().
+
 0.14 Wed 06 Aug 2008
     - skip xml failure tests if XML::LibXML < 1.59 is installed, it seems
       to parse anything you throw at it (Matt S. Trout)

Added: branches/Config-Any/unsupported_error/lib/Config/Any/Base.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/Base.pm	                        (rev 0)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/Base.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -0,0 +1,85 @@
+package Config::Any::Base;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+Config::Any::Base - Base class for loaders
+
+=head1 DESCRIPTION
+
+This is a base class for all loaders. It currently handles the specification
+of dependencies in order to ensure the subclass can load the config file
+format.
+
+=head1 METHODS
+
+=head2 is_supported( )
+
+Allows us to determine if the file format can be loaded. The can be done via
+one of two subclass methds:
+
+=over 4
+
+=item * C<requires_all_of()> - returns an array of items that must all be present in order to work
+
+=item * C<requires_any_of()> - returns an array of items in which at least one must be present
+
+=back
+
+You can specify a module version by passing an array reference in the return.
+
+    sub requires_all_of { [ 'My::Module', '1.1' ], 'My::OtherModule' }
+
+Lack of specifying these subs will assume you require no extra modules to function.
+
+=cut
+
+sub is_supported {
+    my ( $class ) = shift;
+    if ( $class->can( 'requires_all_of' ) ) {
+        eval join( '', map { _require_line( $_ ) } $class->requires_all_of );
+        return $@ ? 0 : 1;
+    }
+    if ( $class->can( 'requires_any_of' ) ) {
+        for ( $class->requires_any_of ) {
+            eval _require_line( $_ );
+            return 1 unless $@;
+        }
+        return 0;
+    }
+
+    # requires nothing!
+    return 1;
+}
+
+sub _require_line {
+    my ( $input ) = shift;
+    my ( $module, $version ) = ( ref $input ? @$input : $input );
+    return "require $module;"
+        . ( $version ? "${module}->VERSION('${version}');" : '' );
+}
+
+=head1 AUTHOR
+
+Brian Cassidy E<lt>bricas at cpan.orgE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008 by Brian Cassidy
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself. 
+
+=head1 SEE ALSO
+
+=over 4 
+
+=item * L<Config::Any>
+
+=back
+
+=cut
+
+1;

Modified: branches/Config-Any/unsupported_error/lib/Config/Any/General.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/General.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/General.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -3,6 +3,8 @@
 use strict;
 use warnings;
 
+use base 'Config::Any::Base';
+
 =head1 NAME
 
 Config::Any::General - Load Config::General files
@@ -68,16 +70,13 @@
     return defined $is_perl_src;
 }
 
-=head2 is_supported( )
+=head2 requires_all_of( )
 
-Returns true if L<Config::General> is available.
+Specifies that this module requires L<Config::General> in order to work.
 
 =cut
 
-sub is_supported {
-    eval { require Config::General; };
-    return $@ ? 0 : 1;
-}
+sub requires_all_of { 'Config::General' }
 
 =head1 AUTHOR
 

Modified: branches/Config-Any/unsupported_error/lib/Config/Any/INI.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/INI.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/INI.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -3,6 +3,8 @@
 use strict;
 use warnings;
 
+use base 'Config::Any::Base';
+
 our $MAP_SECTION_SPACE_TO_NESTED_KEY = 1;
 
 =head1 NAME
@@ -66,16 +68,13 @@
     return $out;
 }
 
-=head2 is_supported( )
+=head2 requires_all_of( )
 
-Returns true if L<Config::Tiny> is available.
+Specifies that this module requires L<Config::Tiny> in order to work.
 
 =cut
 
-sub is_supported {
-    eval { require Config::Tiny; };
-    return $@ ? 0 : 1;
-}
+sub requires_all_of { 'Config::Tiny' }
 
 =head1 PACKAGE VARIABLES
 

Modified: branches/Config-Any/unsupported_error/lib/Config/Any/JSON.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/JSON.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/JSON.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -3,6 +3,8 @@
 use strict;
 use warnings;
 
+use base 'Config::Any::Base';
+
 =head1 NAME
 
 Config::Any::JSON - Load JSON config files
@@ -58,18 +60,14 @@
     }
 }
 
-=head2 is_supported( )
+=head2 requires_any_of( )
 
-Returns true if either L<JSON::Syck> or L<JSON> is available.
+Specifies that this modules requires one of L<JSON::Syck> or L<JSON> in 
+order to work.
 
 =cut
 
-sub is_supported {
-    eval { require JSON::Syck; };
-    return 1 unless $@;
-    eval { require JSON; };
-    return $@ ? 0 : 1;
-}
+sub requires_any_of { 'JSON::Syck', 'JSON' }
 
 =head1 AUTHOR
 

Modified: branches/Config-Any/unsupported_error/lib/Config/Any/Perl.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/Perl.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/Perl.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -3,6 +3,8 @@
 use strict;
 use warnings;
 
+use base 'Config::Any::Base';
+
 my %cache;
 
 =head1 NAME
@@ -54,16 +56,6 @@
     return $content;
 }
 
-=head2 is_supported( )
-
-Returns true.
-
-=cut
-
-sub is_supported {
-    return 1;
-}
-
 =head1 AUTHOR
 
 Brian Cassidy E<lt>bricas at cpan.orgE<gt>

Modified: branches/Config-Any/unsupported_error/lib/Config/Any/XML.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/XML.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/XML.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -3,6 +3,8 @@
 use strict;
 use warnings;
 
+use base 'Config::Any::Base';
+
 =head1 NAME
 
 Config::Any::XML - Load XML config files
@@ -73,16 +75,13 @@
     $out;
 }
 
-=head2 is_supported( )
+=head2 requires_all_of( )
 
-Returns true if L<XML::Simple> is available.
+Specifies that this module requires L<XML::Simple> in order to work.
 
 =cut
 
-sub is_supported {
-    eval { require XML::Simple; };
-    return $@ ? 0 : 1;
-}
+sub requires_all_of { 'XML::Simple' }
 
 =head1 CAVEATS
 

Modified: branches/Config-Any/unsupported_error/lib/Config/Any/YAML.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any/YAML.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any/YAML.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -3,6 +3,8 @@
 use strict;
 use warnings;
 
+use base 'Config::Any::Base';
+
 =head1 NAME
 
 Config::Any::YAML - Load YAML config files
@@ -54,18 +56,14 @@
     }
 }
 
-=head2 is_supported( )
+=head2 requires_any_of( )
 
-Returns true if either L<YAML::Syck> or L<YAML> is available.
+Specifies that this modules requires one of L<YAML::Syck> (0.70) or L<YAML> in 
+order to work.
 
 =cut
 
-sub is_supported {
-    eval { require YAML::Syck; YAML::Syck->VERSION( '0.70' ) };
-    return 1 unless $@;
-    eval { require YAML; };
-    return $@ ? 0 : 1;
-}
+sub requires_any_of { [ 'YAML::Syck', '0.70' ], 'YAML' }
 
 =head1 AUTHOR
 

Modified: branches/Config-Any/unsupported_error/lib/Config/Any.pm
===================================================================
--- branches/Config-Any/unsupported_error/lib/Config/Any.pm	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/lib/Config/Any.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -6,7 +6,7 @@
 use Carp;
 use Module::Pluggable::Object ();
 
-our $VERSION = '0.14';
+our $VERSION = '0.15';
 
 =head1 NAME
 
@@ -149,7 +149,10 @@
     my $use_ext_lut = !$force && $args->{ use_ext };
     if ( $use_ext_lut ) {
         for my $plugin ( @plugins ) {
-            $extension_lut{ $_ } = $plugin for $plugin->extensions;
+            for ( $plugin->extensions ) {
+                $extension_lut{ $_ } ||= [];
+                push @{ $extension_lut{ $_ } }, $plugin;
+            }
         }
 
         $extension_re = join( '|', keys %extension_lut );
@@ -175,11 +178,15 @@
         if ( $use_ext_lut ) {
             $filename =~ m{\.($extension_re)\z};
             next unless $1;
-            @try_plugins = $extension_lut{ $1 };
+            @try_plugins = @{ $extension_lut{ $1 } };
         }
 
+        # not using use_ext means we try all plugins anyway, so we'll
+        # ignore it for the "unsupported" error
+        my $supported = $use_ext_lut ? 0 : 1;
         for my $loader ( @try_plugins ) {
             next unless $loader->is_supported;
+            $supported = 1;
             my @configs
                 = eval { $loader->load( $filename, $loader_args{ $loader } ); };
 
@@ -196,6 +203,12 @@
                 { $filename => @configs == 1 ? $configs[ 0 ] : \@configs };
             last;
         }
+
+        if ( !$supported ) {
+            croak
+                "Cannot load $filename: required support modules are not available.\nPlease install "
+                . join( " OR ", map { _support_error( $_ ) } @try_plugins );
+        }
     }
 
     if ( defined $args->{ flatten_to_hash } ) {
@@ -206,6 +219,17 @@
     return \@results;
 }
 
+sub _support_error {
+    my $module = shift;
+    if ( $module->can( 'requires_all_of' ) ) {
+        return join( ' and ',
+            map { ref $_ ? join( ' ', @$_ ) : $_ } $module->requires_all_of );
+    }
+    if ( $module->can( 'requires_any_of' ) ) {
+        return 'one of ' . join( ' or ', $module->requires_any_of );
+    }
+}
+
 =head2 finder( )
 
 The C<finder()> classmethod returns the 
@@ -219,6 +243,7 @@
     my $class  = shift;
     my $finder = Module::Pluggable::Object->new(
         search_path => [ __PACKAGE__ ],
+        except      => [ __PACKAGE__ . '::Base' ],
         require     => 1
     );
     return $finder;

Modified: branches/Config-Any/unsupported_error/t/50-general.t
===================================================================
--- branches/Config-Any/unsupported_error/t/50-general.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/50-general.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -30,5 +30,5 @@
     my $config = eval { Config::Any::General->load( $file ) };
 
     ok( !$config, 'config load failed' );
-    ok( $@, "error thrown ($@)" );
+    ok( $@,       "error thrown ($@)" );
 }

Modified: branches/Config-Any/unsupported_error/t/51-ini.t
===================================================================
--- branches/Config-Any/unsupported_error/t/51-ini.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/51-ini.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -53,5 +53,5 @@
     my $config = eval { Config::Any::INI->load( $file ) };
 
     ok( !$config, 'config load failed' );
-    ok( $@, "error thrown ($@)" );
+    ok( $@,       "error thrown ($@)" );
 }

Modified: branches/Config-Any/unsupported_error/t/52-json.t
===================================================================
--- branches/Config-Any/unsupported_error/t/52-json.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/52-json.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -23,5 +23,5 @@
     my $config = eval { Config::Any::JSON->load( $file ) };
 
     ok( !$config, 'config load failed' );
-    ok( $@, "error thrown ($@)" );
+    ok( $@,       "error thrown ($@)" );
 }

Modified: branches/Config-Any/unsupported_error/t/53-perl.t
===================================================================
--- branches/Config-Any/unsupported_error/t/53-perl.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/53-perl.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -22,5 +22,5 @@
     my $config = eval { Config::Any::Perl->load( $file ) };
 
     ok( !$config, 'config load failed' );
-    ok( $@, "error thrown ($@)" );
+    ok( $@,       "error thrown ($@)" );
 }

Modified: branches/Config-Any/unsupported_error/t/54-xml.t
===================================================================
--- branches/Config-Any/unsupported_error/t/54-xml.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/54-xml.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -19,13 +19,14 @@
 
 # test invalid config
 SKIP: {
-    my $broken_libxml = eval { require XML::LibXML; XML::LibXML->VERSION lt '1.59'; };
+    my $broken_libxml
+        = eval { require XML::LibXML; XML::LibXML->VERSION lt '1.59'; };
     skip 'XML::LibXML < 1.58 has issues', 2 if $broken_libxml;
 
-    local $SIG{__WARN__} = sub {}; # squash warnings from XML::Simple
+    local $SIG{ __WARN__ } = sub { };    # squash warnings from XML::Simple
     my $file = 't/invalid/conf.xml';
     my $config = eval { Config::Any::XML->load( $file ) };
 
     ok( !$config, 'config load failed' );
-    ok( $@, "error thrown ($@)" );
+    ok( $@,       "error thrown ($@)" );
 }

Modified: branches/Config-Any/unsupported_error/t/55-yaml.t
===================================================================
--- branches/Config-Any/unsupported_error/t/55-yaml.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/55-yaml.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -23,5 +23,5 @@
     my $config = eval { Config::Any::YAML->load( $file ) };
 
     ok( !$config, 'config load failed' );
-    ok( $@, "error thrown ($@)" );
+    ok( $@,       "error thrown ($@)" );
 }

Modified: branches/Config-Any/unsupported_error/t/61-features.t
===================================================================
--- branches/Config-Any/unsupported_error/t/61-features.t	2008-09-03 13:53:01 UTC (rev 8347)
+++ branches/Config-Any/unsupported_error/t/61-features.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -59,7 +59,7 @@
             }
         );
 
-        ok( $result, 'load file with parser forced, flatten to hash' );
+        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;

Added: branches/Config-Any/unsupported_error/t/63-unsupported.t
===================================================================
--- branches/Config-Any/unsupported_error/t/63-unsupported.t	                        (rev 0)
+++ branches/Config-Any/unsupported_error/t/63-unsupported.t	2008-09-03 13:55:06 UTC (rev 8348)
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+use lib 't/lib';
+use Config::Any;
+
+{
+    my $result = eval {
+        Config::Any->load_files(
+            { files => [ 't/conf/conf.unsupported' ], use_ext => 1 } );
+    };
+
+    ok( !defined $result, 'empty result' );
+    ok( $@,               'error thrown' );
+    like(
+        $@,
+        qr/required support modules are not available/,
+        'error message'
+    );
+}

Added: branches/Config-Any/unsupported_error/t/conf/conf.unsupported
===================================================================

Added: branches/Config-Any/unsupported_error/t/lib/Config/Any/Unsupported.pm
===================================================================
--- branches/Config-Any/unsupported_error/t/lib/Config/Any/Unsupported.pm	                        (rev 0)
+++ branches/Config-Any/unsupported_error/t/lib/Config/Any/Unsupported.pm	2008-09-03 13:55:06 UTC (rev 8348)
@@ -0,0 +1,17 @@
+package Config::Any::Unsupported;
+
+use strict;
+use warnings;
+
+use base 'Config::Any::Base';
+
+sub extensions {
+    return qw( unsupported );
+}
+
+sub load {
+}
+
+sub requires_all_of { 'My::Module::DoesNotExist' }
+
+1;




More information about the Catalyst-commits mailing list