blib/lib/DBIx/Class/ResultSet/ModifyMethods.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 2 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 95.9% 47 / 49 CC 4 SCAR 6.5
CC
4
Cov
97%
_wrap_methods
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.9
↑ Summary
1 package DBIx::Class::ResultSet::ModifyMethods;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub around { shift->_wrap_methods('around', @_) }
Subroutine around: called CC=1 SCAR=0.0
Pod documented
9 1 ▶sub before { shift->_wrap_methods('before', @_) }
Subroutine before: called CC=1 SCAR=0.0
Pod documented
10 1 ▶sub after { shift->_wrap_methods('after', @_) }
Subroutine after: called CC=1 SCAR=0.0
Pod documented
11  
12 1 my %WRAPPER_PKG;
13 1 my $ANON_CLASS_COUNT = 0;
14  
15 3 ▶sub _wrap_methods {
Subroutine _wrap_methods: called CC=4 SCAR=13.9
16 3 my($self, $type, $method_spec, $modifier) = @_;
17 3 ▶ my @methods = ref($method_spec) ? @$method_spec : ($method_spec);
Branch: 1 branch
BranchTrueFalseTotal
ref $method_spec ? : 1 2 3
18 3 ▶ my $pkg = $WRAPPER_PKG{$modifier .'_'. join('_', @methods)} ||= 'DBIC_HELPER_ANON'.$ANON_CLASS_COUNT++;
Condition50%1/2
$WRAPPER_PKG{$modifier . '_' . join("_", @methods)} ||= 'DBIC_HELPER_ANON' . $ANON_CLASS_COUNT++
l!l
03
MC/DC0%0/1
$WRAPPER_PKG{$modifier . '_' . join("_", @methods)} ||= 'DBIC_HELPER_ANON' . $ANON_CLASS_COUNT++
$WRAPPER_PKG{$modifier . '_' . join("_", @methods)}
Truth table50%1/2
A
A= $WRAPPER_PKG{$modifier . '_' . join("_", @methods)}
Aresult
11
00
19 3 my $modifier_str;
20 3 $modifier_str .= "$type '$_', \$modifier;" for @methods;
21 1 ▶ eval "package $pkg; use Moo::Role; $modifier_str; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 Role::Tiny->apply_roles_to_object($self, $pkg);
23 }
24  
25 1 1;
26  
27 =head1 NAME
28  
29 DBIx::Class::ResultSet::ModifyMethods - Wrap modifiers around your methods
30  
31 =head1 SYNOPSIS
32  
33 Given a L<DBIx::Class::ResultSet> that consumes this component, such as the
34 following:
35  
36 package MySchema::ResultSet::Bar;
37  
38 use strict;
39 use warnings;
40 use base 'DBIx::Class::ResultSet';
41  
42 __PACKAGE__->load_components('ResultSet::ModifyMethods');
43  
44 ## Additional custom resultset methods, if any
45  
46 1;
47  
48 Then later when you have a resulset of that class:
49  
50 my $rs = $schema->resultset('Bar');
51  
52 You can modify instance methods similarly to L<Moose> method modifiers.
53  
54 $rs->around( search => sub {
55 my ($orig, $rs, @args) = @_;
56 print "Search was called!";
57 return $rs->$orig(@args);
58 });
59  
60 The above would wrap the search method such that when someone called it on C<$rs>
61 you'd see "Search was called!"" in STDOUT
62  
63 =head1 DESCRIPTION
64  
65 There may be times when you wish to hook a resultset before passing a resultset
66 to another method. Since the anonymous coderef can be a closure, this opens
67 some possibilties for enabling observer style patterns. You can also use this
68 to modify C<@args>, etc, just as in L<Moose>, or even change the return value.
69  
70 =head1 METHODS
71  
72 This component defines the following methods.
73  
74 =head2 around
75  
76 Arguments: $method||\@methods, $coderef
77 Returns: Wrapped ResultSet (A Proxy instance)
78  
79 Allows you to dynamically add a L<Moose> style around method modifier for this
80 single ResultSet only which wraps a method or arrayref of methods and grants
81 you full control over how the wrapped method is called. You can even use this
82 to inspect and modify arguments.
83  
84 Example:
85  
86 $rs->around( search => sub {
87 my ($orig, $rs, @args) = @_;
88 return $rs->$orig(@args);
89 });
90  
91 =head2 before
92  
93 Arguments: $method||\@methods, $coderef
94 Returns: Wrapped ResultSet (A Proxy instance)
95  
96 Allows you to run some code before a method or arrayref of methods on your
97 ResultSet instance before it actually runs. You can't use this to modify
98 incoming C<args> to a method or control the method. Use this to safely
99 hook a method when you want to do something and not potentially effect the
100 actually running of the wrapped method.
101  
102 $rs->before( search => sub {
103 my ($rs, @args) = @_;
104 return $rs->$orig(@args);
105 });
106  
107 =head2 after
108  
109 Arguments: $method||\@methods, $coderef
110 Returns: Wrapped ResultSet (A Proxy instance)
111  
112 Allows you to run some code before a method or arrayref of methods on your
113 ResultSet instance after it actually runs. You can't use this to modify
114 incoming C<args> to a method or control the method. Use this to safely
115 hook a method when you want to do something and not potentially effect the
116 actually running of the wrapped method.
117  
118 $rs->after( search => sub {
119 my ($rs, @args) = @_;
120 return $rs->$orig(@args);
121 });
122  
123 =head1 AUTHOR
124  
125 John Napiorkowski L<email:jjnapiork@cpan.org>
126  
127 =head1 SEE ALSO
128  
129 L<DBIx::Class>
130  
131 =head1 COPYRIGHT & LICENSE
132  
133 Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org>
134  
135 This library is free software; you can redistribute it and/or modify it under
136 the same terms as Perl itself.
137  
138 =cut
↑ Summary