From 8f5a3a52a23b361930dadd68bb56c92db1671480 Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Tue, 31 Mar 2015 12:13:02 +0100 Subject: [PATCH] fix #43: custom constructor name in SetterInjection --- lib/Bread/Board/ConstructorInjection.pm | 15 +------- lib/Bread/Board/Service/WithConstructor.pm | 43 ++++++++++++++++++++++ lib/Bread/Board/SetterInjection.pm | 5 ++- t/002_setter_injection.t | 12 ++++++ 4 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 lib/Bread/Board/Service/WithConstructor.pm diff --git a/lib/Bread/Board/ConstructorInjection.pm b/lib/Bread/Board/ConstructorInjection.pm index 689cfd4..62624fe 100644 --- a/lib/Bread/Board/ConstructorInjection.pm +++ b/lib/Bread/Board/ConstructorInjection.pm @@ -5,25 +5,12 @@ use Try::Tiny; use Bread::Board::Types; -with 'Bread::Board::Service::WithClass', +with 'Bread::Board::Service::WithConstructor', 'Bread::Board::Service::WithParameters', 'Bread::Board::Service::WithDependencies'; -has 'constructor_name' => ( - is => 'rw', - isa => 'Str', - lazy => 1, - builder => '_build_constructor_name', -); - has '+class' => (required => 1); -sub _build_constructor_name { - my $self = shift; - - try { Class::MOP::class_of($self->class)->constructor_name } || 'new'; -} - sub get { my $self = shift; diff --git a/lib/Bread/Board/Service/WithConstructor.pm b/lib/Bread/Board/Service/WithConstructor.pm new file mode 100644 index 0000000..6d86f66 --- /dev/null +++ b/lib/Bread/Board/Service/WithConstructor.pm @@ -0,0 +1,43 @@ +package Bread::Board::Service::WithConstructor; +use Moose::Role; + +use Try::Tiny; + +with 'Bread::Board::Service::WithClass'; + +has 'constructor_name' => ( + is => 'rw', + isa => 'Str', + lazy => 1, + builder => '_build_constructor_name', +); + +sub _build_constructor_name { + my $self = shift; + + try { Class::MOP::class_of($self->class)->constructor_name } || 'new'; +} + +no Moose; 1; + +__END__ + +=pod + +=head1 DESCRIPTION + +=head1 METHODS + +=over 4 + +=item B + +=back + +=head1 BUGS + +All complex software has bugs lurking in it, and this module is no +exception. If you find a bug please either email me, or add the bug +to cpan-RT. + +=cut diff --git a/lib/Bread/Board/SetterInjection.pm b/lib/Bread/Board/SetterInjection.pm index ce6accd..b75325a 100644 --- a/lib/Bread/Board/SetterInjection.pm +++ b/lib/Bread/Board/SetterInjection.pm @@ -3,7 +3,7 @@ use Moose; use Bread::Board::Types; -with 'Bread::Board::Service::WithClass', +with 'Bread::Board::Service::WithConstructor', 'Bread::Board::Service::WithParameters', 'Bread::Board::Service::WithDependencies'; @@ -11,7 +11,8 @@ has '+class' => (required => 1); sub get { my $self = shift; - my $o = $self->class->new; + my $constructor = $self->constructor_name; + my $o = $self->class->$constructor(); $o->$_($self->param($_)) foreach $self->param; return $o; } diff --git a/t/002_setter_injection.t b/t/002_setter_injection.t index fc04a6b..d6cd7a4 100644 --- a/t/002_setter_injection.t +++ b/t/002_setter_injection.t @@ -20,9 +20,12 @@ use Bread::Board::Literal; package Addict; use Moose; + sub shoot_up_good { shift->new(@_, overdose => 1) } + has 'needle' => (is => 'rw'); has 'spoon' => (is => 'rw'); has 'stash' => (is => 'rw'); + has 'overdose' => (is => 'ro', isa => 'Bool', default => 0); } my $s = Bread::Board::SetterInjection->new( @@ -57,6 +60,15 @@ does_ok($s, 'Bread::Board::Service'); } } +$s->constructor_name('shoot_up_good'); + +{ + my $i = $s->get(stash => Mexican::Black::Tar->new); + + isa_ok($i, 'Addict'); + ok $i->overdose, 'Alternate constructor called'; +} + is($s->name, 'William', '... got the right name'); is($s->class, 'Addict', '... got the right class');