summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-12-28 18:05:46 -0600
committerJesse Luehrs <doy@tozt.net>2012-12-28 18:05:46 -0600
commit8761aaac596dfd3fe595584aa44720084ccec2eb (patch)
treec32ba8a308e8e1dddcfbd5d1623176880e9e273f
parentacd81a059abac533c0af5ff31b0d672f7f3f2bc1 (diff)
downloadp6-bread-board-8761aaac596dfd3fe595584aa44720084ccec2eb.tar.gz
p6-bread-board-8761aaac596dfd3fe595584aa44720084ccec2eb.zip
implement arrays of strings for dependencies
-rw-r--r--lib/Bread/Board.pm22
-rw-r--r--t/029_sugar_auto_wire_names.t79
2 files changed, 99 insertions, 2 deletions
diff --git a/lib/Bread/Board.pm b/lib/Bread/Board.pm
index 8ee05e7..82b886c 100644
--- a/lib/Bread/Board.pm
+++ b/lib/Bread/Board.pm
@@ -170,7 +170,16 @@ class ConstructorInjection does Service does HasParameters does HasDependencies
if %params.<dependencies> {
if %params.<dependencies> ~~ Array {
%params.<dependencies> = %params.<dependencies>.map(-> $dep {
- $dep.service_path.split('/').[*-1] => $dep
+ my ($name, $obj);
+ if ($dep ~~ Dependency) {
+ $name = $dep.service_path.split('/').[*-1];
+ $obj = $dep;
+ }
+ else {
+ $name = $dep.split('/').[*-1];
+ $obj = Dependency.new(service_path => $dep);
+ }
+ $name => $obj
}).hash;
}
@@ -235,7 +244,16 @@ class BlockInjection does Service does HasParameters does HasDependencies {
if %params.<dependencies> {
if %params.<dependencies> ~~ Array {
%params.<dependencies> = %params.<dependencies>.map(-> $dep {
- $dep.service_path.split('/').[*-1] => $dep
+ my ($name, $obj);
+ if ($dep ~~ Dependency) {
+ $name = $dep.service_path.split('/').[*-1];
+ $obj = $dep;
+ }
+ else {
+ $name = $dep.split('/').[*-1];
+ $obj = Dependency.new(service_path => $dep);
+ }
+ $name => $obj
}).hash;
}
diff --git a/t/029_sugar_auto_wire_names.t b/t/029_sugar_auto_wire_names.t
new file mode 100644
index 0000000..79aa431
--- /dev/null
+++ b/t/029_sugar_auto_wire_names.t
@@ -0,0 +1,79 @@
+use v6;
+use Test;
+
+use Bread::Board;
+
+class FileLogger {
+ has $.log_file;
+}
+
+class DBI {
+ has Str ($.dsn, $.username, $.password);
+
+ method connect (Str $dsn, Str $username, Str $password) {
+ self.new(dsn => $dsn, username => $username, password => $password);
+ }
+}
+
+class MyApplication {
+ has FileLogger $.logger;
+ has DBI $.dbh;
+}
+
+my $c = container 'MyApp', {
+ service 'log_file', 'logfile.log';
+ service 'logger', (
+ class => FileLogger,
+ lifecycle => Singleton,
+ dependencies => ['log_file'],
+ );
+
+ container 'Database', {
+ service 'dsn', 'dbi:sqlite:dbname=my-app.db';
+ service 'username', 'user';
+ service 'password', 'pass';
+
+ service 'dbh', (
+ block => -> $s {
+ DBI.connect(
+ $s.param('dsn'),
+ $s.param('username'),
+ $s.param('password'),
+ ) || die "Could not connect";
+ },
+ dependencies => [qw[dsn username password]],
+ );
+ };
+
+ service 'application', (
+ class => MyApplication,
+ dependencies => ['logger', 'Database/dbh'],
+ );
+};
+
+my $logger = $c.resolve(service => 'logger');
+isa_ok($logger, FileLogger);
+is($logger.log_file, 'logfile.log');
+
+is($c.fetch('logger/log_file').service, $c.fetch('log_file'));
+is($c.fetch('logger/log_file').get, 'logfile.log');
+
+my $dbh = $c.resolve(service => 'Database/dbh');
+isa_ok($dbh, DBI);
+
+is($dbh.dsn, 'dbi:sqlite:dbname=my-app.db');
+is($dbh.username, 'user');
+is($dbh.password, 'pass');
+
+my $app = $c.resolve(service => 'application');
+isa_ok($app, MyApplication);
+
+isa_ok($app.logger, FileLogger);
+is($app.logger, $logger);
+
+isa_ok($app.dbh, DBI);
+isnt($app.dbh, $dbh);
+
+done;
+
+# vim:ft=perl6:foldmethod=manual