method names to be delegated, and Moose will install a delegation method
for each one.
+You can also include regular expressions in the arrayref to extend the list
+with the methods (of the class being delegated to) whose names are matched by
+the regular expressions. A method name is included if it is present in the
+arrayref as a string, or if at least one regular expression matches it. See the
+C<REGEX> format below for more details about how the regular expressions are
+interpreted and the limitations they impose.
+
=item C<HASH>
This is the second most common usage for I<handles>. Instead of a list of
Dylan Hardison (doc fixes)
+Norbert (norbi) Buchmuller
+
... and many other #moose folks
=head1 COPYRIGHT AND LICENSE
use warnings;
use Scalar::Util 'blessed', 'weaken';
-use List::MoreUtils 'any';
+use List::MoreUtils 'any', 'uniq', 'part';
use Try::Tiny;
use overload ();
return %{$handles};
}
elsif ($handle_type eq 'ARRAY') {
- return map { $_ => $_ } @{$handles};
+ my ($strings, $regexes) = part { ref $_ eq 'Regexp' } @{$handles};
+
+ my @method_names = @{ $strings || [] };
+ if ($regexes) {
+ ($self->has_type_constraint)
+ || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)", data => $handles);
+ foreach my $regex (@{$regexes}) {
+ push @method_names,
+ grep { /$regex/ } $self->_get_delegate_method_list;
+ }
+ }
+
+ return map { $_ => $_ } uniq @method_names;
}
elsif ($handle_type eq 'Regexp') {
($self->has_type_constraint)
use strict;
use warnings;
-use Test::More tests => 92;
+use Test::More tests => 119;
use Test::Exception;
is($car->stop, 'Engine::stop', '... got the right value from ->stop');
# -------------------------------------------------------------------
+# ARRAY+REGEXP handles
+# -------------------------------------------------------------------
+# the array based format can also contain regexes
+
+{
+ package Thud;
+ use Moose;
+
+ sub foo { 'Thud::foo' }
+ sub bar { 'Thud::bar' }
+ sub baz { 'Thud::baz' }
+ sub quux { 'Thud::quux' }
+
+ package Thud::Proxy1;
+ use Moose;
+
+ has 'thud' => (
+ is => 'ro',
+ isa => 'Thud',
+ default => sub { Thud->new },
+ handles => [
+ qr/^b.*/,
+ ],
+ );
+
+ package Thud::Proxy2;
+ use Moose;
+
+ has 'thud' => (
+ is => 'ro',
+ isa => 'Thud',
+ default => sub { Thud->new },
+ handles => [
+ 'quux',
+ qr/^b.*/,
+ ],
+ );
+
+ package Thud::Proxy3;
+ use Moose;
+
+ has 'thud' => (
+ is => 'ro',
+ isa => 'Thud',
+ default => sub { Thud->new },
+ handles => [
+ qr/.*/,
+ 'quux',
+ ],
+ );
+}
+
+{
+ my $thud_proxy = Thud::Proxy1->new;
+ isa_ok($thud_proxy, 'Thud::Proxy1');
+
+ can_ok($thud_proxy, 'thud');
+ isa_ok($thud_proxy->thud, 'Thud');
+
+ can_ok($thud_proxy, 'bar');
+ can_ok($thud_proxy, 'baz');
+
+ is($thud_proxy->bar, 'Thud::bar', '... got the right proxied return value');
+ is($thud_proxy->baz, 'Thud::baz', '... got the right proxied return value');
+}
+{
+ my $thud_proxy = Thud::Proxy2->new;
+ isa_ok($thud_proxy, 'Thud::Proxy2');
+
+ can_ok($thud_proxy, 'thud');
+ isa_ok($thud_proxy->thud, 'Thud');
+
+ can_ok($thud_proxy, 'bar');
+ can_ok($thud_proxy, 'baz');
+ can_ok($thud_proxy, 'quux');
+
+ is($thud_proxy->bar, 'Thud::bar', '... got the right proxied return value');
+ is($thud_proxy->baz, 'Thud::baz', '... got the right proxied return value');
+ is($thud_proxy->quux, 'Thud::quux', '... got the right proxied return value');
+}
+{
+ my $thud_proxy = Thud::Proxy3->new;
+ isa_ok($thud_proxy, 'Thud::Proxy3');
+
+ can_ok($thud_proxy, 'thud');
+ isa_ok($thud_proxy->thud, 'Thud');
+
+ can_ok($thud_proxy, 'foo');
+ can_ok($thud_proxy, 'bar');
+ can_ok($thud_proxy, 'baz');
+ can_ok($thud_proxy, 'quux');
+
+ is($thud_proxy->foo, 'Thud::foo', '... got the right proxied return value');
+ is($thud_proxy->bar, 'Thud::bar', '... got the right proxied return value');
+ is($thud_proxy->baz, 'Thud::baz', '... got the right proxied return value');
+ is($thud_proxy->quux, 'Thud::quux', '... got the right proxied return value');
+}
+
+# -------------------------------------------------------------------
# REGEXP handles
# -------------------------------------------------------------------
# and we support regexp delegation