added Config::General support
Brian Cassidy [Mon, 1 May 2006 12:22:31 +0000 (12:22 +0000)]
added nicer syntax for models/views/controllers

Changes
lib/Catalyst/Plugin/ConfigLoader.pm
lib/Catalyst/Plugin/ConfigLoader/General.pm [new file with mode: 0644]
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 b0194a7..4cd05ba 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,10 @@
 Revision history for Perl extension Catalyst::Plugin::ConfigLoader.\r
 \r
+0.07  Mon May 01 2006\r
+    - added Config::General support\r
+    - added nicer syntax for specifying models/views/controllers where\r
+      available\r
+\r
 0.06  Wed Apr 26 2006\r
     - remove "last;" so that _local configs will be processed\r
 \r
index c50c0f5..7ec10fa 100644 (file)
@@ -10,7 +10,7 @@ use Module::Pluggable::Fast
     require => 1;\r
 use Data::Visitor::Callback;\r
 \r
-our $VERSION = '0.06';\r
+our $VERSION = '0.07';\r
 \r
 =head1 NAME\r
 \r
@@ -68,6 +68,9 @@ sub setup {
         for( @files ) {\r
             next unless -f $_;\r
             my $config = $loader->load( $_ );\r
+            \r
+            _fix_syntax( $config );\r
+            \r
             $c->config( $config ) if $config;\r
         }\r
     }\r
@@ -104,6 +107,23 @@ sub finalize_config {
     $v->visit( $c->config );\r
 }\r
 \r
+sub _fix_syntax {\r
+    my $config     = shift;\r
+    my @components = (\r
+        map +{\r
+            prefix => $_ eq 'Component' ? '' : $_ . '::',\r
+            values => delete $config->{ lc $_ } || delete $config->{ $_ }\r
+        }, qw( Component Model View Controller )\r
+    );\r
+\r
+       foreach my $comp ( @components ) {\r
+        my $prefix = $comp->{ prefix };\r
+        foreach my $element ( keys %{ $comp->{ values } } ) {\r
+            $config->{ "$prefix$element" } = $comp->{ values }->{ $element };\r
+        }\r
+    }\r
+}\r
+\r
 =head1 AUTHOR\r
 \r
 =over 4 \r
diff --git a/lib/Catalyst/Plugin/ConfigLoader/General.pm b/lib/Catalyst/Plugin/ConfigLoader/General.pm
new file mode 100644 (file)
index 0000000..f6e8d16
--- /dev/null
@@ -0,0 +1,80 @@
+package Catalyst::Plugin::ConfigLoader::General;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader::General - Load Config::General files\r
+\r
+=head1 DESCRIPTION\r
+\r
+Loads Config::General files. Example:\r
+\r
+    name = TestApp\r
+    <Component Controller::Foo>\r
+        foo bar\r
+    </Component>\r
+    <Model Baz>\r
+        qux xyzzy\r
+    </Model>\r
+\r
+=head1 METHODS\r
+\r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<cnf>, C<conf>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+    return qw( cnf conf );\r
+}\r
+\r
+=head2 load( $file )\r
+\r
+Attempts to load C<$file> via Config::General.\r
+\r
+=cut\r
+\r
+sub load {\r
+    my $class = shift;\r
+    my $file  = shift;\r
+\r
+    require Config::General;\r
+    my $configfile = Config::General->new( $file );\r
+    my $config     = { $configfile->getall };\r
+    \r
+    return $config;\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=item * L<Catalyst::Plugin::ConfigLoader>\r
+\r
+=item * L<Config::General>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
index a5c28f6..f254e86 100644 (file)
@@ -70,6 +70,8 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst::Plugin::ConfigLoader>\r
 \r
+=item * L<Config::Tiny>\r
+\r
 =back\r
 \r
 =cut\r
index 248543b..3a8b539 100644 (file)
@@ -78,6 +78,10 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst::Plugin::ConfigLoader>\r
 \r
+=item * L<JSON>\r
+\r
+=item * L<JSON::Syck>\r
+\r
 =back\r
 \r
 =cut\r
index 205f11c..6cfec00 100644 (file)
@@ -12,7 +12,7 @@ Catalyst::Plugin::ConfigLoader::Perl - Load Perl config files
 Loads Perl files. Example:\r
 \r
     {\r
-        name               => 'TestApp',\r
+        name => 'TestApp',\r
         Controller::Foo => {\r
             foo => 'bar'\r
         }\r
index 9fb2ab4..f44346c 100644 (file)
@@ -16,6 +16,9 @@ Loads XML files. Example:
         <component name="Controller::Foo">\r
             <foo>bar</foo>\r
         </component>\r
+        <model name="Baz">\r
+            <qux>xyzzy</qux>\r
+        </model>\r
     </config>\r
 \r
 =head1 METHODS\r
@@ -42,12 +45,7 @@ sub load {
 \r
     require XML::Simple;\r
     XML::Simple->import;\r
-    my $config = XMLin( $file, ForceArray => [ 'component' ] );\r
-\r
-    my $components = delete $config->{ component };\r
-       foreach my $element ( keys %$components ) {\r
-            $config->{ $element } = $components->{ $element };\r
-    }\r
+    my $config = XMLin( $file, ForceArray => [ qw( component model view controller ) ] );\r
 \r
     return $config;\r
 }\r
index 88683dc..3e521ff 100644 (file)
@@ -74,6 +74,10 @@ it under the same terms as Perl itself.
 \r
 =item * L<Catalyst::Plugin::ConfigLoader>\r
 \r
+=item * L<YAML>\r
+\r
+=item * L<YAML::Syck>\r
+\r
 =back\r
 \r
 =cut\r