Skip to content
Browse files

support for literals

  • Loading branch information...
1 parent 92d76a8 commit ad635dbeb91e9de72ec5c16357784f69bdffb36c @jasonmay jasonmay committed
Showing with 30 additions and 6 deletions.
  1. +11 −2 lib/IoC.pm6
  2. +14 −1 t/01-basic.t
  3. +5 −3 t/02-sugar.t
View
13 lib/IoC.pm6
@@ -3,6 +3,7 @@ module IoC;
use IoC::Container;
use IoC::ConstructorInjection;
use IoC::BlockInjection;
+use IoC::Literal;
my %containers;
my $container-name;
@@ -26,8 +27,13 @@ sub container($pair) is export {
sub contains(Block $sub) is export { return $sub }
sub service($pair) is export {
- my %params = ('name' => $pair.key, $pair.value.pairs);
-
+ my %params = ('name' => $pair.key);
+ if $pair.value.^isa('Str') {
+ %params<value> = $pair.value;
+ }
+ else {
+ %params = (%params, $pair.value.pairs);
+ }
my $service-class;
if %params<block> {
@@ -36,6 +42,9 @@ sub service($pair) is export {
elsif %params<class> {
$service-class = 'IoC::ConstructorInjection';
}
+ elsif %params<value> {
+ $service-class = 'IoC::Literal';
+ }
else {
warn "Service {$pair.key} needs more parameters";
return;
View
15 t/01-basic.t
@@ -3,9 +3,10 @@ BEGIN { @*INC.push('lib') };
use IoC::Container;
use IoC::ConstructorInjection;
use IoC::BlockInjection;
+use IoC::Literal;
use Test;
-plan 4;
+plan 5;
my $c = IoC::Container.new();
@@ -32,8 +33,20 @@ $c.add-service(
)
);
+$c.add-service(
+ 'baz', IoC::Literal.new(
+ :lifecycle('Singleton'),
+ :value('My name is Jason'),
+ )
+);
+
+
+
ok($c.fetch('foo').get);
ok($c.fetch('bar').get);
ok($c.fetch('foo').get.bar);
+
is($c.fetch('foo').get.bar, $c.fetch('bar').get);
+
+is($c.fetch('baz').get, 'My name is Jason');
View
8 t/02-sugar.t
@@ -2,7 +2,7 @@ BEGIN { @*INC.push('lib') };
use IoC;
use Test;
-plan(5);
+plan 5;
class Bar {};
class Foo { has Bar $.bar; };
@@ -19,12 +19,14 @@ my $c = container 'mycont' => contains {
return Bar.new();
},
};
+
+ service 'baz' => 'My name is Jason';
};
ok($c.fetch('foo').get);
ok($c.fetch('bar').get);
-ok($c.fetch('bar').get);
ok($c.fetch('foo').get.bar);
-
is($c.fetch('foo').get.bar, $c.fetch('bar').get);
+
+is($c.fetch('baz').get, 'My name is Jason');

0 comments on commit ad635db

Please sign in to comment.
Something went wrong with that request. Please try again.