refactored v0.02
Brian Cassidy [Mon, 30 Jan 2006 01:01:55 +0000 (01:01 +0000)]
Changes
lib/Catalyst/Plugin/ConfigLoader.pm
lib/Catalyst/Plugin/ConfigLoader/INI.pm
lib/Catalyst/Plugin/ConfigLoader/JSON.pm
lib/Catalyst/Plugin/ConfigLoader/Perl.pm
lib/Catalyst/Plugin/ConfigLoader/XML.pm
lib/Catalyst/Plugin/ConfigLoader/YAML.pm

diff --git a/Changes b/Changes
index d8cd4b7..6fe8b17 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,4 +1,7 @@
 Revision history for Perl extension Catalyst::Plugin::ConfigLoader.\r
 \r
+0.02  Sun Jan 29 2006\r
+       - refactoring (suggested by Christian Hansen)\r
+\r
 0.01  Sat Jan 28 2006\r
        - original version
\ No newline at end of file
index ef29444..d4092cb 100644 (file)
@@ -9,7 +9,7 @@ use Module::Pluggable::Fast
     search  => [ __PACKAGE__ ],\r
     require => 1;\r
 \r
-our $VERSION = '0.01';\r
+our $VERSION = '0.02';\r
 \r
 =head1 NAME\r
 \r
@@ -17,9 +17,9 @@ Catalyst::Plugin::ConfigLoader - Load config files of various types
 \r
 =head1 SYNOPSIS\r
 \r
-       package MyApp;\r
-       \r
-       use Catalyst( ConfigLoader );\r
+    package MyApp;\r
+    \r
+    use Catalyst( ConfigLoader );\r
        \r
     # by default myapp.* will be loaded\r
     # you can specify a file if you'd like\r
@@ -43,14 +43,29 @@ successfully loaded.
 =cut\r
 \r
 sub setup {\r
-    my $c        = shift;\r
-    my $confpath = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r
+    my $c    = shift;\r
+    my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r
+\r
+    my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r
     \r
     for my $loader ( $c->_config_loaders ) {\r
-        my $config = $loader->load( $confpath );\r
-        if( $config ) {\r
-            $c->config( $config );\r
-            last;\r
+        my @files;\r
+        my @extensions = $loader->extensions;\r
+        if( $extension ) {\r
+            next unless grep { $_ eq $extension } @extensions;\r
+            push @files, $path;\r
+        }\r
+        else {\r
+            push @files, "$path.$_" for @extensions;\r
+        }\r
+\r
+        for( @files ) {\r
+            next unless -f $_;\r
+            my $config = $loader->load( $_ );\r
+            if( $config ) {\r
+                $c->config( $config );\r
+                last;\r
+            }\r
         }\r
     }\r
 \r
index df263da..64d04d7 100644 (file)
@@ -13,11 +13,21 @@ Loads INI files. Example:
 \r
     name=TestApp\r
     \r
-    [Controller::Config]\r
+    [Controller::Foo]\r
     foo=bar\r
 \r
 =head1 METHODS\r
 \r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<ini>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+    return qw( ini );\r
+}\r
+\r
 =head2 load( $file )\r
 \r
 Attempts to load C<$file> as an INI file.\r
@@ -25,23 +35,13 @@ Attempts to load C<$file> as an INI file.
 =cut\r
 \r
 sub load {\r
-       my $class    = shift;\r
-       my $confpath = shift;\r
-\r
-       my $file;\r
-    if( $confpath =~ /\.(.{3})$/ ) {\r
-        return unless $1 eq 'ini';\r
-        $file = $confpath;\r
-    }\r
-    else {\r
-        $file = "$confpath.ini";\r
-    }\r
-    \r
-    return unless -f $file;\r
+    my $class = shift;\r
+    my $file  = shift;\r
 \r
     require Config::Tiny;\r
     my $config = Config::Tiny->read( $file );\r
     my $main   = delete $config->{ _ };\r
+    \r
     $config->{ $_ } = $main->{ $_ } for keys %$main;\r
 \r
     return $config;\r
@@ -68,6 +68,8 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst>\r
 \r
+=item * <Catalyst::Plugin::ConfigLoader>\r
+\r
 =back\r
 \r
 =cut\r
index 02cdced..041e55f 100644 (file)
@@ -15,13 +15,23 @@ Loads JSON files. Example:
 \r
     {\r
         "name": "TestApp",\r
-        "Controller::Config": {\r
+        "Controller::Foo": {\r
             "foo": "bar"\r
         }\r
     }\r
 \r
 =head1 METHODS\r
 \r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<json>, C<jsn>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+    return qw( json jsn );\r
+}\r
+\r
 =head2 load( $file )\r
 \r
 Attempts to load C<$file> as a JSON file.\r
@@ -29,32 +39,19 @@ Attempts to load C<$file> as a JSON file.
 =cut\r
 \r
 sub load {\r
-       my $class    = shift;\r
-       my $confpath = shift;\r
+    my $class = shift;\r
+    my $file  = shift;\r
 \r
-       my @files;\r
-    if( $confpath =~ /\.(.{3,4})$/ ) {\r
-        return unless $1 =~ /^jso?n$/;\r
-        @files = $confpath;\r
+    my $content = read_file( $file );\r
+\r
+    eval { require JSON::Syck; };\r
+    if( $@ ) {\r
+        require JSON;\r
+        JSON->import;\r
+        return jsonToObj( $content );\r
     }\r
     else {\r
-        @files = map { "$confpath.$_" } qw( json jsn );\r
-    }\r
-    \r
-    for my $file ( @files ) {\r
-        next unless -f $file;\r
-        \r
-        my $content = read_file( $file );\r
-\r
-        eval { require JSON::Syck; };\r
-        if( $@ ) {\r
-            require JSON;\r
-            JSON->import;\r
-            return jsonToObj( $content );\r
-        }\r
-        else {\r
-            return JSON::Syck::Load( $content );\r
-        }\r
+        return JSON::Syck::Load( $content );\r
     }\r
 }\r
 \r
@@ -79,6 +76,8 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst>\r
 \r
+=item * <Catalyst::Plugin::ConfigLoader>\r
+\r
 =back\r
 \r
 =cut\r
index a7beb2a..23e7142 100644 (file)
@@ -13,13 +13,23 @@ Loads Perl files. Example:
 \r
     {\r
         name               => 'TestApp',\r
-        Controller::Config => {\r
+        Controller::Foo => {\r
             foo => 'bar'\r
         }\r
     }\r
 \r
 =head1 METHODS\r
 \r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<pl>, C<perl>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+    return qw( pl perl );\r
+}\r
+\r
 =head2 load( $file )\r
 \r
 Attempts to load C<$file> as a Perl file.\r
@@ -27,22 +37,10 @@ Attempts to load C<$file> as a Perl file.
 =cut\r
 \r
 sub load {\r
-       my $class    = shift;\r
-       my $confpath = shift;\r
+    my $class = shift;\r
+    my $file  = shift;\r
 \r
-       my @files;\r
-    if( $confpath =~ /\.(.{2,4})$/ ) {\r
-        return unless $1 =~ /^p(er)?l$/;\r
-        @files = $confpath;\r
-    }\r
-    else {\r
-        @files = map { "$confpath.$_" } qw( pl perl );\r
-    }\r
-    \r
-    for my $file ( @files ) {\r
-        next unless -f $file;\r
-        return eval { require $file };\r
-    }\r
+    return eval { require $file };\r
 }\r
 \r
 =head1 AUTHOR\r
@@ -66,6 +64,8 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst>\r
 \r
+=item * <Catalyst::Plugin::ConfigLoader>\r
+\r
 =back\r
 \r
 =cut\r
index e44bcae..e960e6a 100644 (file)
@@ -13,13 +13,23 @@ Loads XML files. Example:
 \r
     <config>\r
         <name>TestApp</name>\r
-        <component name="Controller::Config">\r
+        <component name="Controller::Foo">\r
             <foo>bar</foo>\r
         </component>\r
     </config>\r
 \r
 =head1 METHODS\r
 \r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<xml>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+    return qw( xml );\r
+}\r
+\r
 =head2 load( $file )\r
 \r
 Attempts to load C<$file> as an XML file.\r
@@ -27,23 +37,12 @@ Attempts to load C<$file> as an XML file.
 =cut\r
 \r
 sub load {\r
-       my $class    = shift;\r
-       my $confpath = shift;\r
-\r
-       my $file;\r
-    if( $confpath =~ /\.(.{3})$/ ) {\r
-        return unless $1 eq 'xml';\r
-        $file = $confpath;\r
-    }\r
-    else {\r
-        $file = "$confpath.xml";\r
-    }\r
-    \r
-    return unless -f $file;\r
+    my $class = shift;\r
+    my $file  = shift;\r
 \r
     require XML::Simple;\r
     XML::Simple->import;\r
-    my $config      = XMLin( $file, ForceArray => [ 'component' ] );\r
+    my $config = XMLin( $file, ForceArray => [ 'component' ] );\r
 \r
     my $components = delete $config->{ component };\r
        foreach my $element ( keys %$components ) {\r
@@ -74,6 +73,8 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst>\r
 \r
+=item * <Catalyst::Plugin::ConfigLoader>\r
+\r
 =back\r
 \r
 =cut\r
index ec660bd..3d0d538 100644 (file)
@@ -15,11 +15,21 @@ Loads YAML files. Example:
 \r
     ---\r
     name: TestApp\r
-    Controller::Config:\r
+    Controller::Foo:\r
         foo: bar\r
 \r
 =head1 METHODS\r
 \r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<yml>, C<yaml>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+    return qw( yml yaml );\r
+}\r
+\r
 =head2 load( $file )\r
 \r
 Attempts to load C<$file> as a YAML file.\r
@@ -27,30 +37,17 @@ Attempts to load C<$file> as a YAML file.
 =cut\r
 \r
 sub load {\r
-       my $class    = shift;\r
-       my $confpath = shift;\r
+    my $class = shift;\r
+    my $file  = shift;\r
 \r
-       my @files;\r
-    if( $confpath =~ /\.(.{3,4})$/ ) {\r
-        return unless $1 =~ /^ya?ml$/;\r
-        @files = $confpath;\r
+    eval { require YAML::Syck; };\r
+    if( $@ ) {\r
+        require YAML;\r
+        return YAML::LoadFile( $file );\r
     }\r
     else {\r
-        @files = map { "$confpath.$_" } qw( yml yaml );\r
-    }\r
-    \r
-    for my $file ( @files ) {\r
-        next unless -f $file;\r
-\r
-        eval { require YAML::Syck; };\r
-        if( $@ ) {\r
-            require YAML;\r
-            return YAML::LoadFile( $file );\r
-        }\r
-        else {\r
-            my $content = read_file( $file );\r
-            return YAML::Syck::Load( $content );\r
-        }\r
+        my $content = read_file( $file );\r
+        return YAML::Syck::Load( $content );\r
     }\r
 }\r
 \r
@@ -75,6 +72,8 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst>\r
 \r
+=item * <Catalyst::Plugin::ConfigLoader>\r
+\r
 =back\r
 \r
 =cut\r