Mojo/DynamicMethods.pm

filter: Statementstmt 100% 54 / 54 Branchbran 91.6% 11 / 12 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 93.4% 85 / 91 CC 10 SCAR 9.6
CC
10
Cov
96%
__ANON__
4.0
can
4.0
import
3.0
CRAP 2.6
« Mojolicious.pm ↑ Summary Mojo/Content/Single.pm »
1 package Mojo::DynamicMethods;
2 22 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 22 ▶use Hash::Util::FieldHash qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Mojo::Util qw(monkey_patch);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 144 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
8 144 ▶ my ($flag, $caller) = ($_[1] // '', caller);
Condition100%2/2
$_[1] // ""
l!l
8361
MC/DC100%1/1
$_[1] // ""
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
9 144 ▶ return unless $flag eq '-dispatch';
Branch: 1 branch
BranchTrueFalseTotal
unless $flag eq "-dispatch" 61 83 144
10  
11 83 my $dyn_pkg = "${caller}::_Dynamic";
12 83 my $caller_can = $caller->can('SUPER::can');
13 196 ▶ monkey_patch $dyn_pkg, 'can', sub {
Subroutine can: called CC=4 SCAR=13.9
Subroutine __ANON__: called CC=4 SCAR=13.9
14 196 my ($self, $method, @rest) = @_;
15  
16 # Delegate to our parent's "can" if there is one, without breaking if not
17 196 my $can = $self->$caller_can($method, @rest);
18 196 ▶ return undef unless $can;
Branch: 2 branches
BranchTrueFalseTotal
unless $can 37 159 196
unless $can 4 7 11
19 22 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 22 ▶ my $h = do { no strict 'refs'; *{"${dyn_pkg}::${method}"}{CODE} };
Subroutine BEGIN: called CC=1 SCAR=0.0
21 159 ▶ return $h && $h eq $can ? undef : $can;
Branch: 2 branches
BranchTrueFalseTotal
$h && $h eq $can ? : 0 159 159
$h && $h eq $can ? : 1 6 7
Condition66%4/6
$h and $h eq $can
!ll&&!rl&&r
15450
$h and $h eq $can
!ll&&!rl&&r
601
MC/DC0%0/2
$h and $h eq $can
$h $h eq $can
MC/DC50%1/2
$h and $h eq $can
$h $h eq $can
Truth table66%2/3
A and B
A= $hB= $h eq $can
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $hB= $h eq $can
ABresult
0X0
100
111
22 83 };
23  
24 {
25 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 83 unshift @{"${caller}::ISA"}, $dyn_pkg;
27 }
28 }
29  
30 14583 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
31 14583 my ($target, $object, $name, $code) = @_;
32  
33 14583 state %dyn_methods;
34 14583 state $setup = do { fieldhash %dyn_methods; 1 };
35  
36 14583 my $dyn_pkg = "${target}::_Dynamic";
37 monkey_patch($dyn_pkg, $name, $target->BUILD_DYNAMIC($name, \%dyn_methods))
38 22 ▶ unless do { no strict 'refs'; *{"${dyn_pkg}::${name}"}{CODE} };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless do { no strict 'refs'; *{"${dyn_pkg}::$name";}{"CODE"} } 2553 12030 14583
39 14583 $dyn_methods{$object}{$name} = $code;
40 }
41  
42 22 "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn";
43  
44 =encoding utf8
45  
46 =head1 NAME
47  
48 Mojo::DynamicMethods - Fast dynamic method dispatch
49  
50 =head1 SYNOPSIS
51  
52 package MyClass;
53 use Mojo::Base -base, -signatures;
54  
55 use Mojo::DynamicMethods -dispatch;
56  
57 sub BUILD_DYNAMIC ($class, $method, $dyn_methods) {
58 return sub {...};
59 }
60  
61 sub add_helper ($self, $name, $cb) {
62 Mojo::DynamicMethods::register 'MyClass', $self, $name, $cb;
63 }
64  
65 package main;
66  
67 # Generate methods dynamically (and hide them from "$obj->can(...)")
68 my $obj = MyClass->new;
69 $obj->add_helper(foo => sub { warn 'Hello Helper!' });
70 $obj->foo;
71  
72 =head1 DESCRIPTION
73  
74 L<Mojo::DynamicMethods> provides dynamic method dispatch for per-object helper methods without requiring use of
75 C<AUTOLOAD>.
76  
77 To opt your class into dynamic dispatch simply pass the C<-dispatch> flag.
78  
79 use Mojo::DynamicMethods -dispatch;
80  
81 And then implement a C<BUILD_DYNAMIC> method in your class, making sure that the key you use to lookup methods in
82 C<$dyn_methods> is the same thing you pass as C<$ref> to L</"register">.
83  
84 sub BUILD_DYNAMIC ($class, $method, $dyn_methods) {
85 return sub ($self, @args) {
86 my $dynamic = $dyn_methods->{$self}{$method};
87 return $self->$dynamic(@args) if $dynamic;
88 my $package = ref $self;
89 croak qq{Can't locate object method "$method" via package "$package"};
90 };
91 }
92  
93 Note that this module will summon B<Cthulhu>, use it at your own risk!
94  
95 =head1 FUNCTIONS
96  
97 L<Mojo::DynamicMethods> implements the following functions.
98  
99 =head2 register
100  
101 Mojo::DynamicMethods::register $class, $ref, $name, $cb;
102  
103 Registers the method C<$name> as eligible for dynamic dispatch for C<$class>, and sets C<$cb> to be looked up for
104 C<$name> by reference C<$ref> in a dynamic method constructed by C<BUILD_DYNAMIC>.
105  
106 =head1 SEE ALSO
107  
108 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
109  
110 =cut
« Mojolicious.pm ↑ Summary Mojo/Content/Single.pm »