fixed bug where setup wasn't called without plugins
Robert 'phaylon' Sedlacek [Thu, 20 Aug 2009 23:37:06 +0000 (01:37 +0200)]
Changes
lib/CatalystX/Declare/Context/AppSetup.pm [new file with mode: 0644]
lib/CatalystX/Declare/Keyword/Application.pm
t/011_plain_application.t [new file with mode: 0644]
t/lib/PlainTestApp.pm [new file with mode: 0644]
t/lib/PlainTestApp/Controller/Root.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index 77a7b61..ae2d81d 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@
     - action classes are now moosified after loading, this means that
       RenderView should now work with isa. (thanks to all reporters!)
     - documented 'isa' action class option (experimental!)
+    - fixed bug where setup wasn't called without plugins
 
 [0.008] Tue Aug 18 19:59:57 CEST 2009
     - default_inner now ignores arguments
diff --git a/lib/CatalystX/Declare/Context/AppSetup.pm b/lib/CatalystX/Declare/Context/AppSetup.pm
new file mode 100644 (file)
index 0000000..0b0a2b3
--- /dev/null
@@ -0,0 +1,31 @@
+use MooseX::Declare;
+
+role CatalystX::Declare::Context::AppSetup {
+
+    use MooseX::Types::Moose qw( Bool );
+    use Carp                 qw( croak );
+
+
+    has setup_was_called => (
+        is          => 'rw',
+        isa         => Bool,
+    );
+
+    method add_setup_code_parts (Str $package, ArrayRef $plugins = []) {
+
+        if ($self->setup_was_called) {
+            croak 'call to setup already pushed to cleanup code parts';
+        }
+
+        $self->add_cleanup_code_parts(
+            sprintf(
+                '%s->setup(qw( %s ))',
+                $package,
+                join(' ', @$plugins),
+            ),
+            '1;',
+        );
+        $self->setup_was_called(1);
+    }
+}
+
index dd3006e..3756337 100644 (file)
@@ -3,19 +3,16 @@ use MooseX::Declare;
 class CatalystX::Declare::Keyword::Application
     extends MooseX::Declare::Syntax::Keyword::Class {
 
+    use aliased 'CatalystX::Declare::Context::AppSetup';
+
     
     override auto_make_immutable { 0 }
 
+    around context_traits { $self->$orig, AppSetup }
+
     override add_with_option_customizations (Object $ctx, Str $package, ArrayRef $plugins, HashRef $options) {
 
-        $ctx->add_cleanup_code_parts(
-            sprintf(
-                '%s->setup(qw( %s ))',
-                $package,
-                join(' ', @$plugins),
-            ),
-            '1;',
-        );
+        $ctx->add_setup_code_parts($package, $plugins)
     }
 
     before add_namespace_customizations (Object $ctx, Str $package) {
@@ -25,6 +22,12 @@ class CatalystX::Declare::Keyword::Application
             'use parent q{Catalyst}',
         );
     }
+
+    after add_optional_customizations (Object $ctx, Str $package) {
+
+        $ctx->add_setup_code_parts($package)
+            unless $ctx->setup_was_called;
+    }
 }
 
 =head1 NAME
@@ -77,6 +80,41 @@ should not make this class immutable. Currently, making application classes
 immutable isn't supported yet, therefore C<is mutable> is currently a no-op.
 This will likely change as soon as application classes can be made immutable,
 
+=head2 context_traits
+
+    List[ClassName] Object->context_traits ()
+
+This extends the remaining context traits with 
+L<CatalystX::Declare::Context::AppSetup> to manage calls to 
+L<Catalyst/MyApp-E<gt>setup>.
+
+=head2 add_with_option_customizations
+
+    Object->add_with_option_customizations (
+        Object   $ctx, 
+        Str      $package,
+        ArrayRef $plugins,
+        HashRef  $options
+    )
+
+This will prepare L<Catalyst/MyApp-E<gt>setup> to be called with the list of
+plugins that were specified as roles.
+
+=head2 add_namespace_customizations
+
+    Object->add_namespace_customizations (Object $ctx, Str $package)
+
+This will prepare L<Catalyst> as a parent and import L<CLASS> into the
+application's namespace before the other customizations are run.
+
+=head2 add_optional_customizations
+
+    Object->add_optional_customizations (Object $ctx, Str $package)
+
+After all customizations have been done, this modifier will push a call to
+L<Catalyst/MyApp-E<gt>setup> if this wasn't already done by the plugin
+specifications.
+
 =head1 SEE ALSO
 
 =over
diff --git a/t/011_plain_application.t b/t/011_plain_application.t
new file mode 100644 (file)
index 0000000..f8359bb
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More; 
+use Catalyst::Test 'PlainTestApp';
+
+is get('/foo'), 'foo', 'view was reached via RenderView';
+
+done_testing;
+
diff --git a/t/lib/PlainTestApp.pm b/t/lib/PlainTestApp.pm
new file mode 100644 (file)
index 0000000..a86f452
--- /dev/null
@@ -0,0 +1,3 @@
+use CatalystX::Declare;
+
+application PlainTestApp { }
diff --git a/t/lib/PlainTestApp/Controller/Root.pm b/t/lib/PlainTestApp/Controller/Root.pm
new file mode 100644 (file)
index 0000000..ead663c
--- /dev/null
@@ -0,0 +1,6 @@
+use CatalystX::Declare;
+
+controller PlainTestApp::Controller::Root {
+
+    final action foo under '/' { $ctx->response->body('foo') }
+}