| 1 | package AnyEvent::Subprocess::Role::WithDelegates; | ||||||||||||||||||||||||||||
| 2 | 49 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 49 | $AnyEvent::Subprocess::Role::WithDelegates::VERSION = '1.102912'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: paramaterized role consumed by classes that have delegates | ||||||||||||||||||||||||||||
| 6 | 49 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 49 | ▶ | use MooseX::Types::Moose qw(HashRef ArrayRef Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 49 | ▶ | use AnyEvent::Subprocess::Role::WithDelegates::Manager qw(build_delegate); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 41 | parameter type => ( | |||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 143 | ▶ | role { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 143 | with 'MooseX::Clone'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 143 | my $p = shift; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has 'delegate_list' => ( | ||||||||||||||||||||||||||||
| 23 | init_arg => 'delegates', | ||||||||||||||||||||||||||||
| 24 | reader => '_delegate_list', | ||||||||||||||||||||||||||||
| 25 | isa => ArrayRef[$p->type | Str | ArrayRef | HashRef], | ||||||||||||||||||||||||||||
| 26 | 7 | ▶ | default => sub { +[] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 143 | auto_deref => 1, | |||||||||||||||||||||||||||
| 28 | required => 1, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 143 | has 'delegate_ordering' => ( | |||||||||||||||||||||||||||
| 32 | traits => ['NoClone'], | ||||||||||||||||||||||||||||
| 33 | init_arg => undef, | ||||||||||||||||||||||||||||
| 34 | reader => '_delegates', | ||||||||||||||||||||||||||||
| 35 | isa => ArrayRef[$p->type], | ||||||||||||||||||||||||||||
| 36 | auto_deref => 1, | ||||||||||||||||||||||||||||
| 37 | lazy => 1, | ||||||||||||||||||||||||||||
| 38 | builder => '_build_delegate_ordering', | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 143 | has 'delegates_table' => ( | |||||||||||||||||||||||||||
| 42 | traits => ['Hash', 'NoClone'], | ||||||||||||||||||||||||||||
| 43 | init_arg => undef, | ||||||||||||||||||||||||||||
| 44 | isa => HashRef[$p->type], | ||||||||||||||||||||||||||||
| 45 | auto_deref => 1, | ||||||||||||||||||||||||||||
| 46 | lazy => 1, | ||||||||||||||||||||||||||||
| 47 | builder => '_build_delegates_table', | ||||||||||||||||||||||||||||
| 48 | handles => { | ||||||||||||||||||||||||||||
| 49 | delegate => 'get', | ||||||||||||||||||||||||||||
| 50 | '_delegate_exists' => 'exists', | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 78 | ▶ | around clone => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 55 | 78 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | my @cloned_delegates = map { | ||||||||||||||||||||||||||||
| 58 | 78 | ▶ | blessed $_ && $_->can('clone') ? $_->clone : $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($_) and $_->can("clone")
MC/DC0%0/2
builtin::blessed($_) and $_->can("clone")
builtin::blessed($_)
$_->can("clone")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->can("clone")
| |||||||||||||||||||||||||||||
| 59 | } $self->_delegate_list; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 78 | return $self->$orig( | |||||||||||||||||||||||||||
| 62 | delegate_list => \@cloned_delegates, | ||||||||||||||||||||||||||||
| 63 | @args, | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | 143 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 130 | ▶ | before 'delegate' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 68 | 130 | my ($self, $delegate) = @_; | |||||||||||||||||||||||||||
| 69 | 130 | ▶ | confess "No delegate named '$delegate'" if !$self->_delegate_exists($delegate); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 143 | }; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 219 | ▶ | method '_build_delegate_ordering' => sub { | ||||||||||||||||||||||||||
|
Subroutine
_build_delegate_ordering: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 73 | 219 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | 219 | my @delegates_list = $self->_delegate_list; | |||||||||||||||||||||||||||
| 75 | 219 | my @result; | |||||||||||||||||||||||||||
| 76 | 219 | for my $d (@delegates_list){ | |||||||||||||||||||||||||||
| 77 | 495 | ▶ | if( blessed $d ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 78 | 252 | push @result, $d; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 243 | push @result, build_delegate($d); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 219 | return \@result; | |||||||||||||||||||||||||||
| 85 | 143 | }; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 25 | ▶ | method '_build_delegates_table' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_delegates_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||
| 89 | return { | ||||||||||||||||||||||||||||
| 90 | 25 | map { $_->name => $_ } $self->_delegates, | |||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||
| 92 | 143 | }; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 78 | ▶ | after 'clone' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 78 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 78 | $self->_delegates; # vivify noclones after cloning | |||||||||||||||||||||||||||
| 97 | 143 | }; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 83 | ▶ | method 'BUILD' => sub { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 83 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 83 | $self->_delegates; | |||||||||||||||||||||||||||
| 102 | 143 | }; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 382 | ▶ | method '_invoke_delegates' => sub { | ||||||||||||||||||||||||||
|
Subroutine
_invoke_delegates: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 382 | my ($self, $method, @args) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | return map { | ||||||||||||||||||||||||||||
| 108 | 382 | my $d = $_; | |||||||||||||||||||||||||||
| 109 | 1045 | $d->$method($self, @args); | |||||||||||||||||||||||||||
| 110 | } $self->_delegates; | ||||||||||||||||||||||||||||
| 111 | 143 | }; | |||||||||||||||||||||||||||
| 112 | 41 | }; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 41 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =pod | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 NAME | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | AnyEvent::Subprocess::Role::WithDelegates - paramaterized role consumed by classes that have delegates | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 VERSION | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | version 1.102912 | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This role gives its consuming class the ability to have typed | ||||||||||||||||||||||||||||
| 131 | delegates. The type of the delegate is provide at application time | ||||||||||||||||||||||||||||
| 132 | via the C<type> parameter. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Once applied, you get: | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 INITARGS | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 delegates | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | A list (arrayref) of delegates. A delegate can be an instance of a | ||||||||||||||||||||||||||||
| 141 | C<AnyEvent::Subprocess::Delegate> class, a string (which will be | ||||||||||||||||||||||||||||
| 142 | resolved via | ||||||||||||||||||||||||||||
| 143 | L<AnyEvent::Subprocess::Role::WithDelegates::Manager|AnyEvent::Subprocess::Role::WithDelegates::Manager>, | ||||||||||||||||||||||||||||
| 144 | or a two-element hashref or arrayref of the delegate name and a | ||||||||||||||||||||||||||||
| 145 | hashref of delegate args. C<< [ Name => { args } ] >> or C<< { Name => | ||||||||||||||||||||||||||||
| 146 | { args } } >>. C<Name> is treated like a string above, and the args | ||||||||||||||||||||||||||||
| 147 | are handled by the delegate's constructor or by the method supplied at | ||||||||||||||||||||||||||||
| 148 | delegate registration time. See | ||||||||||||||||||||||||||||
| 149 | L<AnyEvent::Subprocess::Role::WithDelegates::Manager> for details. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 METHODS | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 delegate($name) | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Return the delegate named C<$name>. Dies if there is no delegate by | ||||||||||||||||||||||||||||
| 156 | that name. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | (This method is called by users of C<AnyEvent::Subprocess>.) | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 _invoke_delegates($method, @args) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Invokes C<< $delegate->$method($self, @args) >> on each delegate (in the | ||||||||||||||||||||||||||||
| 163 | order they were passed to the constructor). Returns a list of the | ||||||||||||||||||||||||||||
| 164 | return values of each delegate. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | (This method is usually called internally by C<AnyEvent::Subprocess>, | ||||||||||||||||||||||||||||
| 167 | not by end-users.) | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | L<AnyEvent::Subprocess> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Delegate users: | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | L<AnyEvent::Subprocess::Job> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | L<AnyEvent::Subprocess::Running> | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | L<AnyEvent::Subprocess::Done> | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | This software is copyright (c) 2011 by Jonathan Rockway. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 190 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||