blib/lib/Class/MakeMethods/Composite/Universal.pm

filter: Statementstmt 100% 27 / 27 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 93.0% 40 / 43 CC 3 SCAR 4.4
CC
3
Cov
94%
__ANON__
3.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
« blib/lib/Class/MakeMethods/Evaled.pm ↑ Summary blib/lib/Class/MakeMethods/Emulator/mcoder.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Composite::Universal - Composite Method Tricks
4  
5 =head1 SYNOPSIS
6  
7 Class::MakeMethods::Composite::Universal->make_patch(
8 -TargetClass => 'SomeClass::OverYonder',
9 name => 'foo',
10 pre_rules => [
11 sub {
12 my $method = pop;
13 warn "Arguments for foo:", @_
14 }
15 ]
16 post_rules => [
17 sub {
18 warn "Result of foo:", Class::MakeMethods::Composite->CurrentResults
19 }
20 ]
21 );
22  
23 =head1 DESCRIPTION
24  
25 The Composite::Universal suclass of MakeMethods provides some generally-applicable types of methods based on Class::MakeMethods::Composite.
26  
27 =cut
28  
29 package Class::MakeMethods::Composite::Universal;
30  
31 1 $VERSION = 1.000;
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Class::MakeMethods::Composite '-isasubclass';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 ########################################################################
37  
38 =head1 METHOD GENERATOR TYPES
39  
40 =head2 patch
41  
42 The patch ruleset generates composites whose core behavior is based on an existing subroutine.
43  
44 Here's a sample usage:
45  
46 sub foo {
47 my $count = shift;
48 return 'foo' x $count;
49 }
50
51 Class::MakeMethods::Composite::Universal->make(
52 -ForceInstall => 1,
53 patch => {
54 name => 'foo',
55 pre_rules => [
56 sub {
57 my $method = pop @_;
58 if ( ! scalar @_ ) {
59 @{ $method->{args} } = ( 2 );
60 }
61 },
62 sub {
63 my $method = pop @_;
64 my $count = shift;
65 if ( $count > 99 ) {
66 Carp::confess "Won't foo '$count' -- that's too many!"
67 }
68 },
69 ],
70 post_rules => [
71 sub {
72 my $method = pop @_;
73 if ( ref $method->{result} eq 'SCALAR' ) {
74 ${ $method->{result} } =~ s/oof/oozle-f/g;
75 } elsif ( ref $method->{result} eq 'ARRAY' ) {
76 map { s/oof/oozle-f/g } @{ $method->{result} };
77 }
78 }
79 ],
80 },
81 );
82  
83 =cut
84  
85 1 ▶use vars qw( %PatchFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 1 ▶sub patch {
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
88 1 (shift)->_build_composite( \%PatchFragments, @_ );
89 }
90  
91 %PatchFragments = (
92 '' => [
93 1 ▶ '+init' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
94 1 my $method = pop @_;
95 my $origin = ( $Class::MethodMaker::CONTEXT{TargetClass} || '' ) .
96 1 ▶ '::' . $method->{name};
Condition50%1/2
$Class::MethodMaker::CONTEXT{'TargetClass'} || ''
l!l
01
MC/DC0%0/1
$Class::MethodMaker::CONTEXT{'TargetClass'} || ''
$Class::MethodMaker::CONTEXT{'TargetClass'}
Truth table50%1/2
A
A= $Class::MethodMaker::CONTEXT{'TargetClass'}
Aresult
11
00
97 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 $method->{patch_original} = *{ $origin }{CODE}
99 1 ▶ or croak "No subroutine $origin() to patch";
Branch: 1 branch
BranchTrueFalseTotal
$method->{"patch_original"} = *{$origin;}{"CODE"} or croak("No subroutine $origin() to patch") 1 0 1
100 },
101 5 ▶ 'do' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 5 my $method = pop @_;
103 5 my $sub = $method->{patch_original};
104 5 &$sub( @_ );
105 },
106 1 ],
107 );
108  
109 =head2 make_patch
110  
111 A convenient wrapper for C<make()> and the C<patch> method generator.
112  
113 Provides the '-ForceInstall' flag, which is required to ensure that the patched subroutine replaces the original.
114  
115 For example, one could add logging to an existing method as follows:
116  
117 Class::MakeMethods::Composite::Universal->make_patch(
118 -TargetClass => 'SomeClass::OverYonder',
119 name => 'foo',
120 pre_rules => [
121 sub {
122 my $method = pop;
123 warn "Arguments for foo:", @_
124 }
125 ]
126 post_rules => [
127 sub {
128 warn "Result of foo:", Class::MakeMethods::Composite->CurrentResults
129 }
130 ]
131 );
132  
133 =cut
134  
135 1 ▶sub make_patch {
Subroutine make_patch: called CC=1 SCAR=0.0
Pod documented
136 1 (shift)->make( -ForceInstall => 1, patch => { @_ } );
137 }
138  
139  
140 ########################################################################
141  
142 =head1 SEE ALSO
143  
144 See L<Class::MakeMethods> for general information about this distribution.
145  
146 See L<Class::MakeMethods::Composite> for more about this family of subclasses.
147  
148 =cut
149  
150 1 1;
« blib/lib/Class/MakeMethods/Evaled.pm ↑ Summary blib/lib/Class/MakeMethods/Emulator/mcoder.pm »