diff options
author | Jesse Luehrs <doy@tozt.net> | 2011-03-02 22:28:34 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2011-03-02 22:35:01 -0600 |
commit | 565afc7a56c520e8bf20dd8415d72481e861ed31 (patch) | |
tree | 3a31738c0f0dbe8bc4f2447102d30430269fc4ca /lib | |
parent | 2b95f03cda1e9f719624e253a8ed24a2aac5a143 (diff) | |
download | bread-board-declare-565afc7a56c520e8bf20dd8415d72481e861ed31.tar.gz bread-board-declare-565afc7a56c520e8bf20dd8415d72481e861ed31.zip |
more useful handling of attributes with no services defined
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Bread/Board/Declare.pm | 3 | ||||
-rw-r--r-- | lib/Bread/Board/Declare/Meta/Role/Attribute.pm | 25 |
2 files changed, 19 insertions, 9 deletions
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; |