| 1 | package MooseX::MultiObject::Meta::Method::MultiDelegation; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | $MooseX::MultiObject::Meta::Method::MultiDelegation::VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: method that delegates to a set of objects | ||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use true; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use parent 'Moose::Meta::Method', 'Class::MOP::Method::Generated'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # i hate class mop | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 2 | my %options = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | confess 'You must supply an object_getter method name' | ||||||||||||||||||||||||||||
| 21 | 2 | ▶ | unless exists $options{object_getter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | confess 'You must supply a delegate_to method or coderef' | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | unless exists $options{delegate_to}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | exists $options{curried_arguments} | ||||||||||||||||||||||||||||
| 27 | 2 | ▶ | || ( $options{curried_arguments} = [] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ( $options{curried_arguments} && | ||||||||||||||||||||||||||||
| 30 | 2 | ▶ | ( 'ARRAY' eq ref $options{curried_arguments} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$options{"curried_arguments"} and "ARRAY" eq ref $options{"curried_arguments"}
MC/DC0%0/2
$options{"curried_arguments"} and "ARRAY" eq ref $options{"curried_arguments"}
$options{"curried_arguments"}
"ARRAY" eq ref $options{"curried_arguments"}
Truth table33%1/3
A and B
A= $options{"curried_arguments"}B= "ARRAY" eq ref $options{"curried_arguments"}
| |||||||||||||||||||||||||||||
| 31 | || confess 'You must supply a curried_arguments which is an ARRAY reference'; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | my $self = $class->_new( \%options ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | $self->_initialize_body; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | return $self; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | sub _new { | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 41 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $options = @_ == 1 ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | return bless $options, $class; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | sub object_getter { $_[0]->{object_getter} } | ||||||||||||||||||||||||||
|
Subroutine
object_getter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | sub curried_arguments { $_[0]->{curried_arguments} } | ||||||||||||||||||||||||||
|
Subroutine
curried_arguments: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub delegate_to { $_[0]->{delegate_to} } | ||||||||||||||||||||||||||
|
Subroutine
delegate_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | sub _initialize_body { | ||||||||||||||||||||||||||
|
Subroutine
_initialize_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 2 | my $meta = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | my $object_getter = $meta->object_getter; | |||||||||||||||||||||||||||
| 55 | 2 | my @extra_args = @{$meta->curried_arguments}; | |||||||||||||||||||||||||||
| 56 | 2 | my $delegate_to = $meta->delegate_to; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | $meta->{body} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 2 | unshift @_, @extra_args; | |||||||||||||||||||||||||||
| 61 | 2 | my @objects = $self->$object_getter; | |||||||||||||||||||||||||||
| 62 | 2 | return map { scalar $_->$delegate_to(@_) } @objects; | |||||||||||||||||||||||||||
| 63 | 2 | }; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =pod | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head1 NAME | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | MooseX::MultiObject::Meta::Method::MultiDelegation - method that delegates to a set of objects | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 VERSION | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | version 0.03 | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Given a class that C<has> a set of objects: | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my $meta = Moose::Meta::Class->create( ... ); | ||||||||||||||||||||||||||||
| 83 | $meta->add_attribute ( objects => ( | ||||||||||||||||||||||||||||
| 84 | is => 'ro', isa => 'Set', handles => ['members'], | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Make a method foo to call foo on every element of the set: | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | my $foo_metamethod = MooseX::MultiObject::Meta::Method::MultiDelegation->new( | ||||||||||||||||||||||||||||
| 90 | object_getter => 'members', | ||||||||||||||||||||||||||||
| 91 | delegate_to => 'foo', | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | $meta->add_method( foo => $foo_metamethod ); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Then you can write: | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | my $class = $meta->name->new( objects => [ $a, $b ] ); | ||||||||||||||||||||||||||||
| 99 | my @results = $class->foo; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Which is equivalent to: | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | my $set = set($a, $b); | ||||||||||||||||||||||||||||
| 104 | my @results = map { $_->foo } $set->members; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | This is a C<Moose::Meta::Method> and C<Class::MOP::Method::Generated> | ||||||||||||||||||||||||||||
| 109 | that works like C<Moose::Meta::Method::Delegation>, except it | ||||||||||||||||||||||||||||
| 110 | delegates to a collection of objects instead of just one. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 INITARGS | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 curried_arguments | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 delegate_to | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 object_getter | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | This software is copyright (c) 2011 by Jonathan Rockway. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 129 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||