Get sugar working
Tomas Doran [Wed, 10 Aug 2011 23:25:58 +0000 (00:25 +0100)]
lib/Catalyst/IOC.pm
t/aggregate/live_container_custom_container_sugar.t
t/lib/TestAppCustomContainer/SugarContainer.pm

index a204295..b04f32f 100644 (file)
@@ -2,21 +2,17 @@ package Catalyst::IOC;
 use strict;
 use warnings;
 use Bread::Board;
+use Catalyst::IOC::ConstructorInjection;
 
 # FIXME - neither of these work:
-#use Sub::Exporter -setup => [
-#    qw(
-#        as
-#        container
-#        depends_on
-#        service
-#        alias
-#        wire_names
-#        include
-#        typemap
-#        infer
-#    )
-#];
+use Sub::Exporter -setup => {
+    exports => [qw/
+        component
+    /],
+    groups  => { default => [qw/
+        component
+    /]},
+};
 #use Sub::Exporter -setup => [
 #    qw(
 #        Bread::Board::as
@@ -32,10 +28,27 @@ use Bread::Board;
 #];
 # I'm probably doing it wrong.
 # Anyway, I'll just use Moose::Exporter. Do I really have to use Sub::Exporter?
-use Moose::Exporter;
-Moose::Exporter->setup_import_methods(
-    also => ['Bread::Board'],
-);
+#use Moose::Exporter;
+#Moose::Exporter->setup_import_methods(
+#    also => ['Bread::Board'],
+#);
+
+sub component {
+    my ($name, %args) = @_;
+    $args{dependencies} ||= {};
+    $args{dependencies}{application_name} = depends_on( '/application_name' );
+
+    # FIXME - check $args{type} here!
+
+    Catalyst::IOC::ConstructorInjection->new(
+        %args,
+        name             => $name,
+        lifecycle        => 'Singleton',
+        # XX FIXME - needs to become possible to intuit catalyst_component_name
+        #            from dependencies (like config)
+        catalyst_component_name => 'TestAppCustomContainer::Model::Bar',
+    )
+}
 
 1;
 
index f57a6b0..0a6ee67 100644 (file)
@@ -3,6 +3,5 @@ use strict;
 use FindBin '$Bin';
 use lib "$Bin/../lib";
 use TestCustomContainer;
-use Test::More skip_all => 'Sugar not implemented';
 
 TestCustomContainer->new(sugar => 1);
index 7fc1a92..80c0fff 100644 (file)
@@ -2,34 +2,20 @@ package TestAppCustomContainer::SugarContainer;
 use Moose;
 use namespace::autoclean;
 use Catalyst::IOC;
-
+use Bread::Board;
 extends 'Catalyst::IOC::Container';
 
 sub BUILD {
     my $self = shift;
 
-    container $self => as {
-        container model => as {
-            component Foo => ();
-            component Bar => ( dependencies => [ depends_on('/model/Foo') ] );
-            component Baz => (
-                lifecycle    => '+Catalyst::IOC::LifeCycle::Request',
-                dependencies => [
-                    depends_on( '/application_name' ),
-                    depends_on( '/config' ),
-                    depends_on( '/model/Foo' ),
-                ],
-            );
-            component Quux => ( lifecycle => 'Singleton' );
-            component Fnar => (
-                lifecycle => 'Singleton',
-                class     => 'My::External::Class',
-                dependencies => [ depends_on('config') ],
-            #   ^^ FIXME - gets whole config, not Model::Foo
-            #   There should be a 'nice' way to get the 'standard' config
-            );
-        };
-    };
+    warn("Add Bar to model");
+    $self->get_sub_container('model')->add_service(
+        component(
+            'Bar' =>
+                class        => 'TestAppCustomContainer::Model::Bar',
+                dependencies => { foo => depends_on('/model/DefaultSetup') },
+        )
+    );
 }
 
 __PACKAGE__->meta->make_immutable;