Skip to content

Commit

Permalink
fix stevan#43: custom constructor name in SetterInjection
Browse files Browse the repository at this point in the history
  • Loading branch information
dakkar committed Mar 31, 2015
1 parent 48a68a6 commit 8f5a3a5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
15 changes: 1 addition & 14 deletions lib/Bread/Board/ConstructorInjection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
43 changes: 43 additions & 0 deletions lib/Bread/Board/Service/WithConstructor.pm
Original file line number Diff line number Diff line change
@@ -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<constructor_name>
=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
5 changes: 3 additions & 2 deletions lib/Bread/Board/SetterInjection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use Moose;

use Bread::Board::Types;

with 'Bread::Board::Service::WithClass',
with 'Bread::Board::Service::WithConstructor',
'Bread::Board::Service::WithParameters',
'Bread::Board::Service::WithDependencies';

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;
}
Expand Down
12 changes: 12 additions & 0 deletions t/002_setter_injection.t
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit 8f5a3a5

Please sign in to comment.