default_view / default_model
André Walker [Mon, 11 Jul 2011 22:53:59 +0000 (19:53 -0300)]
lib/Catalyst/IOC/Container.pm
lib/Catalyst/IOC/SubContainer.pm
t/aggregate/unit_core_mvc.t

index c2e75bf..03c850a 100644 (file)
@@ -51,13 +51,11 @@ has sub_container_class => (
 );
 
 sub BUILD {
-    my $self = shift;
+    my ( $self, $params ) = @_;
 
     $self->add_service(
         $self->${\"build_${_}_service"}
     ) for qw/
-        default_view
-        default_model
         substitutions
         file
         driver
@@ -76,14 +74,26 @@ sub BUILD {
     /;
 
     $self->add_sub_container(
-        $self->${ \"build_${_}_subcontainer" }
-    ) for qw/ model view controller /;
+        $self->build_controller_subcontainer
+    );
+
+    $self->add_sub_container(
+        $self->build_view_subcontainer(
+            default_component => $params->{default_view},
+        )
+    );
+
+    $self->add_sub_container(
+        $self->build_model_subcontainer(
+            default_component => $params->{default_model},
+        )
+    );
 }
 
 sub build_model_subcontainer {
     my $self = shift;
 
-    return $self->new_sub_container(
+    return $self->new_sub_container( @_,
         name => 'model',
     );
 }
@@ -91,7 +101,7 @@ sub build_model_subcontainer {
 sub build_view_subcontainer {
     my $self = shift;
 
-    return $self->new_sub_container(
+    return $self->new_sub_container( @_,
         name => 'view',
     );
 }
@@ -104,26 +114,6 @@ sub build_controller_subcontainer {
     );
 }
 
-sub build_default_model_service {
-    Bread::Board::BlockInjection->new(
-        name => 'default_model',
-        block => sub {
-            shift->param('config')->{default_model};
-        },
-        dependencies => [ depends_on('config') ],
-    );
-}
-
-sub build_default_view_service {
-    Bread::Board::BlockInjection->new(
-        name => 'default_view',
-        block => sub {
-            shift->param('config')->{default_view};
-        },
-        dependencies => [ depends_on('config') ],
-    );
-}
-
 sub build_name_service {
     my $self = shift;
 
@@ -408,9 +398,7 @@ sub get_component_from_sub_container {
     my $sub_container = $self->get_sub_container( $sub_container_name );
 
     if (!$name) {
-        my $default_name = 'default_' . $sub_container_name;
-        my $default      = $self->resolve( service => $default_name )
-            if $self->has_service($default_name);
+        my $default = $sub_container->default_component;
 
         return $sub_container->get_component( $default, $c, @args )
             if $default && $sub_container->has_service( $default );
@@ -420,6 +408,8 @@ sub get_component_from_sub_container {
         $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
         $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
         $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
+
+        return;
     }
 
     return $sub_container->get_component_regexp( $name, $c, @args )
index 068f379..2751268 100644 (file)
@@ -5,6 +5,12 @@ use Catalyst::IOC::BlockInjection;
 
 extends 'Bread::Board::Container';
 
+has default_component => (
+    isa => 'Str|Undef',
+    is  => 'ro',
+    required => 0,
+);
+
 sub get_component {
     my ( $self, $name, @args ) = @_;
 
index fa1683c..26ee2d6 100644 (file)
@@ -6,9 +6,9 @@ use Moose::Meta::Class;
 
 use_ok('Catalyst');
 
-our @complist =
-  map { "MyMVCTestApp::$_"; }
-  qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
+our @complist_suffix = qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/;
+
+our @complist = map { "MyMVCTestApp::$_" } @complist_suffix;
 
 foreach my $comp (@complist) {
     Moose::Meta::Class->create(
@@ -88,8 +88,8 @@ is_deeply( [ sort MyMVCTestApp->models ],
 {
     $warnings = 0;
 
-    like (MyMVCTestApp->view , qr/^MyMVCTestApp\::(V|View)\::/ , 'view() with no defaults returns *something*');
-    ok( $warnings, 'view() w/o a default is random, warnings thrown' );
+    is( MyMVCTestApp->view , undef, 'view() w/o a default is undef' );
+    ok( $warnings, 'warnings thrown for view() w/o a default' );
 }
 
 is ( bless ({stash=>{current_view=>'V'}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'current_view ok');
@@ -103,13 +103,8 @@ is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyMVCTestApp:
 {
     $warnings = 0;
 
-    ok( my $model = MyMVCTestApp->model );
-
-    ok( (($model =~ /^MyMVCTestApp\::(M|Model)\::/) ||
-        $model->isa('Some::Test::Object')),
-        'model() with no defaults returns *something*' );
-
-    ok( $warnings, 'model() w/o a default is random, warnings thrown' );
+    is( MyMVCTestApp->model, undef, 'model() w/o a default is undef' );
+    ok( $warnings, 'warnings thrown for model() w/o a default' );
 }
 
 is ( bless ({stash=>{current_model=>'M'}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'current_model ok');
@@ -129,13 +124,59 @@ is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyMVCTestA
     is( get('/foo/test_controller'), 'bar', 'controller() with empty args returns current controller' );
 }
 
-MyMVCTestApp->config->{default_view} = 'V';
-is ( bless ({stash=>{}}, 'MyMVCTestApp')->view , 'MyMVCTestApp::View::V', 'default_view ok');
-is ( MyMVCTestApp->view , 'MyMVCTestApp::View::V', 'default_view in class method ok');
+our @complist_default_view =
+    map { "MyMVCTestAppDefaultView::$_" } @complist_suffix;
+
+{
+    package MyMVCTestAppDefaultView;
+
+    use base qw/Catalyst/;
+
+    sub locate_components {
+        return @::complist_default_view;
+    }
+
+    no warnings 'redefine';
+    *Catalyst::Utils::ensure_class_loaded = sub {
+        my $class = shift;
+        $::loaded++
+            if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultView/
+    };
+
+    __PACKAGE__->config( default_view => 'V' );
+
+    __PACKAGE__->setup;
+}
+
+is( bless ({stash=>{}}, 'MyMVCTestAppDefaultView')->view, 'MyMVCTestAppDefaultView::View::V', 'default_view ok' );
+is( MyMVCTestAppDefaultView->view , 'MyMVCTestAppDefaultView::View::V', 'default_view in class method ok' );
+
+our @complist_default_model =
+    map { "MyMVCTestAppDefaultModel::$_" } @complist_suffix;
+
+{
+    package MyMVCTestAppDefaultModel;
+
+    use base qw/Catalyst/;
+
+    sub locate_components {
+        return @::complist_default_model;
+    }
+
+    no warnings 'redefine';
+    *Catalyst::Utils::ensure_class_loaded = sub {
+        my $class = shift;
+        $::loaded++
+            if Class::MOP::is_class_loaded($class) && $class =~ /^MyMVCTestAppDefaultModel/
+    };
+
+    __PACKAGE__->config( default_model => 'M' );
+
+    __PACKAGE__->setup;
+}
 
-MyMVCTestApp->config->{default_model} = 'M';
-is ( bless ({stash=>{}}, 'MyMVCTestApp')->model , 'MyMVCTestApp::Model::M', 'default_model ok');
-is ( MyMVCTestApp->model , 'MyMVCTestApp::Model::M', 'default_model in class method ok');
+is( bless ({stash=>{}}, 'MyMVCTestAppDefaultModel')->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model ok' );
+is( MyMVCTestAppDefaultModel->model , 'MyMVCTestAppDefaultModel::Model::M', 'default_model in class method ok' );
 
 # regexp behavior tests
 {