From cbb4a91e2fcbafa6d64889717763c6a8a9033412 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 2 Apr 2011 23:25:22 -0500 Subject: set the 'class' attribute on block services if possible --- lib/Bread/Board/Declare/Meta/Role/Attribute.pm | 3 ++ t/12-circular-dependency.t | 59 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 t/12-circular-dependency.t diff --git a/lib/Bread/Board/Declare/Meta/Role/Attribute.pm b/lib/Bread/Board/Declare/Meta/Role/Attribute.pm index d4d59b9..f6584ae 100644 --- a/lib/Bread/Board/Declare/Meta/Role/Attribute.pm +++ b/lib/Bread/Board/Declare/Meta/Role/Attribute.pm @@ -131,6 +131,9 @@ after attach_to_class => sub { my $service; if ($self->has_block) { + if ($tc && $tc->isa('Moose::Meta::TypeConstraint::Class')) { + %params = (%params, class => $tc->class); + } $service = Bread::Board::Declare::BlockInjection->new( %params, block => $self->block, diff --git a/t/12-circular-dependency.t b/t/12-circular-dependency.t new file mode 100644 index 0000000..28431c9 --- /dev/null +++ b/t/12-circular-dependency.t @@ -0,0 +1,59 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use Test::Fatal; + +{ + package Foo; + use Moose; + + has bar => ( + is => 'rw', + isa => 'Bar', + ); +} + +{ + package Bar; + use Moose; + + has foo => ( + is => 'rw', + isa => 'Foo', + weak_ref => 1, + ); +} + +{ + package MyApp; + use Moose; + use Bread::Board::Declare; + + has foo => ( + is => 'ro', + isa => 'Foo', + block => sub { + my ($s, $self) = @_; + Foo->new(bar => $s->param('bar')); + }, + lifecycle => 'Singleton', + dependencies => ['bar'], + ); + has bar => ( + is => 'ro', + isa => 'Bar', + block => sub { + my ($s, $self) = @_; + Bar->new(foo => $s->param('foo')); + }, + lifecycle => 'Singleton', + dependencies => ['foo'], + ); +} + + +is exception { MyApp->new->foo->bar }, undef, + 'circular block-injection deps should survive'; + +done_testing(); -- cgit v1.2.3