| 1 | package AnyEvent::Subprocess::Role::WithDelegates::Manager; | ||||||||||||||||||||||||||||
| 2 | 49 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 49 | $AnyEvent::Subprocess::Role::WithDelegates::Manager::VERSION = '1.102912'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: manage delegate shortcuts | ||||||||||||||||||||||||||||
| 6 | 49 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 49 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 49 | ▶ | use Carp qw(cluck confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 49 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | exports => ['register_delegate', 'build_delegate'], | ||||||||||||||||||||||||||||
| 13 | 49 | }; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 48 | my %builder_map; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 480 | ▶ | sub register_delegate { | ||||||||||||||||||||||||||
|
Subroutine
register_delegate: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 480 | my ($name, $builder) = @_; | |||||||||||||||||||||||||||
| 19 | cluck "Overwriting existing delegate '$name' with '$builder'" | ||||||||||||||||||||||||||||
| 20 | 480 | ▶ | if exists $builder_map{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 480 | $builder_map{$name} = $builder; | |||||||||||||||||||||||||||
| 23 | 480 | return; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 278 | ▶ | sub lookup_delegate { | ||||||||||||||||||||||||||
|
Subroutine
lookup_delegate: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 278 | my ($name) = @_; | |||||||||||||||||||||||||||
| 28 | confess "No delegate named '$name' registered" | ||||||||||||||||||||||||||||
| 29 | 278 | ▶ | unless exists $builder_map{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 278 | return $builder_map{$name}; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 278 | ▶ | sub build_delegate { | ||||||||||||||||||||||||||
|
Subroutine
build_delegate: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 278 | my ($spec) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 278 | my $builder; | |||||||||||||||||||||||||||
| 38 | my @args; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 278 | ▶ | if(ref $spec){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 108 | my ($k, $v); | |||||||||||||||||||||||||||
| 42 | # perl sucks | ||||||||||||||||||||||||||||
| 43 | 108 | eval { ($k, $v) = %$spec } || | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | eval { ($k, $v) = @$spec } || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
eval {
do {
() = %$spec
}
} or eval {
do {
() = @$spec
}
}
MC/DC0%0/2
eval {
do {
() = %$spec
}
} or eval {
do {
() = @$spec
}
}
eval {
do {
() = %$spec
}
}
eval {
do {
() = @$spec
}
}
Truth table33%1/3
A or B
A= eval {
do {
() = %$spec
}
}B= eval {
do {
() = @$spec
}
}
| |||||||||||||||||||||||||||||
| 45 | confess 'Invalid data!'; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 108 | $builder = lookup_delegate($k); | |||||||||||||||||||||||||||
| 48 | 108 | @args = $v; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||
| 51 | 170 | $builder = lookup_delegate($spec); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 278 | ▶ | if( ref $builder && ref $builder eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $builder and ref $builder eq "CODE"
MC/DC50%1/2
ref $builder and ref $builder eq "CODE"
ref $builder
ref $builder eq "CODE"
Truth table66%2/3
A and B
A= ref $builderB= ref $builder eq "CODE"
| |||||||||||||||||||||||||||||
| 55 | 234 | return $builder->(@args); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 44 | return $builder->new(@args); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 48 | 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =pod | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 NAME | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | AnyEvent::Subprocess::Role::WithDelegates::Manager - manage delegate shortcuts | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head1 VERSION | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | version 1.102912 | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Creating an instance of a delegate to pass to | ||||||||||||||||||||||||||||
| 77 | C<AnyEvent::Subprocess>'s constructor is tedious. This module maps | ||||||||||||||||||||||||||||
| 78 | sugary names to builders of delegate objects, so that the user can say | ||||||||||||||||||||||||||||
| 79 | C<'Foo'> instead of C<< AnyEvent::Subprocess::Job::Delegate::Foo->new >>. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | If you are writing a delegate for C<AnyEvent::Subprocess>, simply call | ||||||||||||||||||||||||||||
| 82 | C<register_delegate> in your module. When the users C<use>s your | ||||||||||||||||||||||||||||
| 83 | module, the sugary name will become available. And yeah, it's global, | ||||||||||||||||||||||||||||
| 84 | so be careful. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | None by default, but you can request C<register_delegate> and | ||||||||||||||||||||||||||||
| 89 | C<build_delegate>. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 register_delegate( $name, &$builder ) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Register C<$builder> to build delegates named C<$name>. C<$builder> | ||||||||||||||||||||||||||||
| 96 | is a coderef that is eventually called with the key/value pairs | ||||||||||||||||||||||||||||
| 97 | supplied by the user. (The docs say this has to be a hashref, but it | ||||||||||||||||||||||||||||
| 98 | can actually be any scalar value. Checking is up to you.) The | ||||||||||||||||||||||||||||
| 99 | builder must return an instance of a class that does | ||||||||||||||||||||||||||||
| 100 | L<AnyEvent::Subprocess::Delegate|AnyEvent::Subprocess::Delegate>, | ||||||||||||||||||||||||||||
| 101 | although this condition is not checked by this module. (You will get | ||||||||||||||||||||||||||||
| 102 | a type error when you are building the class that uses the delegate.) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | In the common case where the args should be passed directly to some | ||||||||||||||||||||||||||||
| 105 | class' constructor, you can just supply the class name as the | ||||||||||||||||||||||||||||
| 106 | C<$builder>. C<new> will be called with any args the user supplies. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | You get a noisy warning if you reuse a C<$name>. This is almost | ||||||||||||||||||||||||||||
| 109 | always an error, though; only the most recent name/builder pair is | ||||||||||||||||||||||||||||
| 110 | remembered. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 build_delegate( $spec ) | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Given a C<$spec>, return an instance of the correct delegate. Dies if | ||||||||||||||||||||||||||||
| 115 | we don't know how to build a delegate according to C<$spec>. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | C<$spec> can be a string naming the delegate to be built, or it can be | ||||||||||||||||||||||||||||
| 118 | a hashref or arrayref of name/args pair. Name is the same name passed | ||||||||||||||||||||||||||||
| 119 | to C<register_delegate>, and the args should be a hashref. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | L<AnyEvent::Subprocess> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<AnyEvent::Subprocess::Delegate> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | L<AnyEvent::Subprocess::Role::WithDelegates> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | This software is copyright (c) 2011 by Jonathan Rockway. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 138 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||