From 565afc7a56c520e8bf20dd8415d72481e861ed31 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 2 Mar 2011 22:28:34 -0600 Subject: more useful handling of attributes with no services defined --- lib/Bread/Board/Declare.pm | 3 +- lib/Bread/Board/Declare/Meta/Role/Attribute.pm | 25 ++++++--- t/01-basic.t | 4 +- t/35-no-service.t | 72 ++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 t/35-no-service.t diff --git a/lib/Bread/Board/Declare.pm b/lib/Bread/Board/Declare.pm index 5480c77..a0a3789 100644 --- a/lib/Bread/Board/Declare.pm +++ b/lib/Bread/Board/Declare.pm @@ -75,7 +75,8 @@ corresponding to the type constraint. =item -Otherwise, no service is created. +Otherwise, a BlockInjection service is created which throws an exception. This allows services to be created for the sole purpose of being set through the attribute, without requiring a default to be specified. Note that +C<< required => 1 >> is still valid on these attributes. =back diff --git a/lib/Bread/Board/Declare/Meta/Role/Attribute.pm b/lib/Bread/Board/Declare/Meta/Role/Attribute.pm index 19ba8f7..23434e4 100644 --- a/lib/Bread/Board/Declare/Meta/Role/Attribute.pm +++ b/lib/Bread/Board/Declare/Meta/Role/Attribute.pm @@ -127,6 +127,8 @@ after attach_to_class => sub { : ()), ); + my $tc = $self->has_type_constraint ? $self->type_constraint : undef; + my $service; if ($self->has_block) { $service = Bread::Board::Declare::BlockInjection->new( @@ -140,14 +142,21 @@ after attach_to_class => sub { value => $self->literal_value, ); } - elsif ($self->has_type_constraint) { - my $tc = $self->type_constraint; - if ($tc->isa('Moose::Meta::TypeConstraint::Class')) { - $service = Bread::Board::Declare::ConstructorInjection->new( - %params, - class => $tc->class, - ); - } + elsif ($tc && $tc->isa('Moose::Meta::TypeConstraint::Class')) { + $service = Bread::Board::Declare::ConstructorInjection->new( + %params, + class => $tc->class, + ); + } + else { + $service = Bread::Board::Declare::BlockInjection->new( + %params, + block => sub { + die "Attribute " . $self->name . " did not specify a service." + . " It must be given a value through the constructor or" + . " writer method before it can be resolved." + }, + ); } $self->associated_service($service) if $service; diff --git a/t/01-basic.t b/t/01-basic.t index e978101..311583d 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -51,9 +51,9 @@ $i = 0; my $foo = Foo->new; isa_ok($foo, 'Bread::Board::Container'); ok($foo->has_service($_), "has service $_") - for qw(bar baz quux); + for qw(foo bar baz quux); ok(!$foo->has_service($_), "doesn't have service $_") - for qw(foo baz2); + for qw(baz2); isa_ok($foo->get_service('bar'), 'Bread::Board::Declare::Literal'); isa_ok($foo->get_service('baz'), 'Bread::Board::Declare::ConstructorInjection'); isa_ok($foo->get_service('quux'), 'Bread::Board::Declare::BlockInjection'); diff --git a/t/35-no-service.t b/t/35-no-service.t new file mode 100644 index 0000000..c6a9226 --- /dev/null +++ b/t/35-no-service.t @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use Test::Fatal; + +{ + package Bar; + use Moose; +} + +{ + package Foo; + use Moose; + use Bread::Board::Declare; + + has foo => ( + is => 'ro', + isa => 'Str', + ); + + has bar => ( + is => 'ro', + isa => 'Bar', + service => 0, + ); + + has baz => ( + is => 'ro', + isa => 'Str', + block => sub { shift->param('foo') }, + dependencies => ['foo'], + ); + + has quux => ( + is => 'ro', + isa => 'Bar', + block => sub { shift->param('bar') }, + dependencies => ['bar'], + ); +} + +{ + my $foo = Foo->new; + ok($foo->has_service($_), "has service $_") for qw(foo baz); + ok(!$foo->has_service($_), "doesn't have service $_") for qw(bar); +} + +{ + my $foo = Foo->new; + like( + exception { $foo->baz }, + qr/^Attribute foo did not specify a service\. It must be given a value through the constructor or writer method before it can be resolved\./, + "got the right error when foo isn't set" + ); +} + +{ + my $foo = Foo->new(foo => 'bar'); + is($foo->baz, 'bar', "didn't get an error when foo is set"); +} + +{ + my $foo = Foo->new; + like( + exception { $foo->quux }, + qr/^Could not find container or service for bar in Foo/, + "can't depend on attrs with no service" + ); +} + +done_testing; -- cgit v1.2.3-54-g00ecf