| 1 | package MooX::Async; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.006'; | |||||||||||||||||||
| 4 | 1 | $VERSION = eval $VERSION; | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | MooX::IOAsync - BovIinate Oout :of: ASsync | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | package Thing; | ||||||||||||||||||||
| 13 | use Moo; | ||||||||||||||||||||
| 14 | use MooX::Async; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # Extend an IO::Async module with MooX::Async: | ||||||||||||||||||||
| 17 | extends MooXAsync('Notifier'); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # Define a lazy attribute which will hold a callback subref with | ||||||||||||||||||||
| 20 | # an (optional) default implementation. | ||||||||||||||||||||
| 21 | event on_foo => sub { say "foo" }; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Allows a L<Moo> class to extend a L<IO::Async::Notifier> subclass with | ||||||||||||||||||||
| 26 | the L<MooX::Async> role and the magic necessary to make | ||||||||||||||||||||
| 27 | L<IO::Async::Notifier> work as a L<Moo> object. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 BUGS | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | Certainly. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 1 | ▶ | use Modern::Perl '2017'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 1 | ▶ | use strictures 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 1 | ▶ | use Moo (); # For _install_tracked | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | ▶ | use Moo::Role; # For me | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 1 | ▶ | use Module::Runtime qw(compose_module_name module_notional_filename); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 4 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 43 | 4 | my $pkg = caller; | |||||||||||||||||||
| 44 | 4 | ▶ | my $has = $pkg->can('has') or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 8 | ▶ | Moo::_install_tracked $pkg, event => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||
| 46 | 8 | my $event = shift; | |||||||||||||||||||
| 47 | 0 | ▶ | my $sub = @_ % 2 ? pop : sub { die "$event event unimplemented" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | ▶ | $has->($event, @_, is => lazy => builder => sub { $sub }); | ||||||||||||||||||
|
Subroutine
_build_on_command: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 49 | 3 | }; | |||||||||||||||||||
| 50 | 3 | Moo::_install_tracked $pkg, MooXAsync => \&MooXAsync; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head1 EXPORTS | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | L</MooXAsync> and L</event> are exported unconditionally. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =over | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =item event($name, [@args], [$subref]) => void | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Install a lazy attribute to handle the event C<$name>. This is basically just: | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | has $event => (@args, builder => sub { $subref }); | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =item MooXAsync($notifier) => $async_moo_class | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Creates and returns the name of a class which extends C<$notifier>, | ||||||||||||||||||||
| 68 | which can be an object or the name of a class which subclasses | ||||||||||||||||||||
| 69 | L<IO::Async::Notifier> with L<Moo> and L<MooX::Async>. | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Prepends C<IO::Async::> to C<$notifier> if it doesn't contain | ||||||||||||||||||||
| 72 | C<::>. If C<$notifier> begins with C<::> then it is removed. | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | If C<$notifier> is an object then it is re-blessed into the new | ||||||||||||||||||||
| 75 | package. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =back | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 2 | ▶ | sub MooXAsync { | ||||||||||||||||||
|
Subroutine
MooXAsync: called CC=6 SCAR=19.3
| |||||||||||||||||||||
| 82 | 2 | my $notifier = shift; | |||||||||||||||||||
| 83 | # Ensure DOES('IO::Async::Notifier') and DOESN'T MooX::Async | ||||||||||||||||||||
| 84 | 2 | ▶ | my $class = ref $notifier || $notifier; # Those who bless into '0' deserve what they get | ||||||||||||||||||
|
Condition33%1/3
ref $notifier || $notifier
MC/DC0%0/2
ref $notifier || $notifier
ref $notifier
$notifier
Truth table33%1/3
A || B
A= ref $notifierB= $notifier
| |||||||||||||||||||||
| 85 | 2 | ▶ | my $parent = $class =~ /^IO::Async::/ ? $class : compose_module_name('IO::Async', $class); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 2 | my $pkg = $parent . '::' . __PACKAGE__; | |||||||||||||||||||
| 87 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | # This is ugly but I'm lazy. | ||||||||||||||||||||
| 89 | 2 | ▶ | if (not scalar keys %{"$pkg\::"}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 0 | ▶ | eval <<"TOP" . <<'BOTTOM' or die; # Keep interpolated part seperate | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
FOREIGNBUILDARGS: not called CC=1 SCAR=0.6
Subroutine
FOREIGNBUILDARGS: not called CC=1 SCAR=0.6
Subroutine
configure_unknown: not called CC=1 SCAR=0.6
Subroutine
configure_unknown: not called CC=1 SCAR=0.6
Pod
undocumented
Pod
undocumented
Pod
documented
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 91 | package $pkg; | ||||||||||||||||||||
| 92 | use Moo; | ||||||||||||||||||||
| 93 | extends '$parent'; | ||||||||||||||||||||
| 94 | TOP | ||||||||||||||||||||
| 95 | sub configure_unknown { } | ||||||||||||||||||||
| 96 | sub FOREIGNBUILDARGS { shift; @_ } | ||||||||||||||||||||
| 97 | around can_event => sub { | ||||||||||||||||||||
| 98 | my ($orig, $self, $event) = @_; | ||||||||||||||||||||
| 99 | return $self->$event if $self->can("_build_$event"); | ||||||||||||||||||||
| 100 | $self->$orig($event); | ||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||
| 102 | 1; | ||||||||||||||||||||
| 103 | BOTTOM | ||||||||||||||||||||
| 104 | 2 | $INC{module_notional_filename($pkg)} = 1; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | 2 | ▶ | bless $notifier, $pkg if ref $notifier; # Any extra mooification? | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 2 | return $pkg; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | ▶ | use namespace::clean qw(MooXAsync); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 HISTORY | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =over | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =item MooX::Async 0.004 | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Have I got it right yet? | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =back | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 SEE ALSO | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | L<Moo> | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | L<IO::Async> | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head1 AUTHOR | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Matthew King <chohag@jtan.com> | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||