summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Bread/Board/Declare.pm29
-rw-r--r--t/inline-services.t33
2 files changed, 62 insertions, 0 deletions
diff --git a/lib/Bread/Board/Declare.pm b/lib/Bread/Board/Declare.pm
index 404a427..3f5dff5 100644
--- a/lib/Bread/Board/Declare.pm
+++ b/lib/Bread/Board/Declare.pm
@@ -117,6 +117,7 @@ become services, as in:
=cut
my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
+ as_is => ['dep'],
install => ['import', 'unimport'],
class_metaroles => {
attribute => ['Bread::Board::Declare::Meta::Role::Attribute'],
@@ -145,6 +146,34 @@ sub init_meta {
$package->$init_meta(%options);
}
+sub dep {
+ if (@_ > 1) {
+ my %opts = (
+ name => '__ANON__',
+ @_,
+ );
+
+ if (exists $opts{dependencies}) {
+ confess("Dependencies are not supported for inline services");
+ }
+
+ if (exists $opts{value}) {
+ return Bread::Board::Literal->new(%opts);
+ }
+ elsif (exists $opts{block}) {
+ return Bread::Board::BlockInjection->new(%opts);
+ }
+ elsif (exists $opts{class}) {
+ return Bread::Board::ConstructorInjection->new(%opts);
+ }
+ else {
+ }
+ }
+ else {
+ return Bread::Board::Dependency->new(service_path => $_[0]);
+ }
+}
+
=head1 BUGS
No known bugs.
diff --git a/t/inline-services.t b/t/inline-services.t
new file mode 100644
index 0000000..fc047c8
--- /dev/null
+++ b/t/inline-services.t
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+{
+ package Foo;
+ use Moose;
+ use Bread::Board::Declare;
+
+ has foo => (
+ is => 'ro',
+ isa => 'Str',
+ block => sub {
+ my ($s) = @_;
+ return 'foo' . $s->param('bar') . $s->param('baz');
+ },
+ dependencies => {
+ bar => dep('bar'),
+ baz => dep(value => 'zab'),
+ },
+ );
+
+ has bar => (
+ is => 'ro',
+ isa => 'Str',
+ value => 'BAR',
+ );
+}
+
+is(Foo->new->foo, 'fooBARzab', "inline dependencies resolved properly");
+
+done_testing;