MooX/HandlesVia.pm

filter: Statementstmt 92.8% 52 / 56 Branchbran 81.8% 18 / 22 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 91.6% 11 / 12 Podpod 100% 1 / 1 totaltotal 83.0% 88 / 106 CC 15 SCAR 20.4
CC
15
Cov
85%
process_has
13.3
__ANON__
12.0
import
3.0
CRAP 7.7
↑ Summary Data/Perl/Collection/Hash/MooseLike.pm »
1 package MooX::HandlesVia;
2 # ABSTRACT: NativeTrait-like behavior for Moo.
3 13 $MooX::HandlesVia::VERSION = '0.001009';
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Moo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Module::Runtime qw/require_module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # reserved hardcoded mappings for classname shortcuts.
12 13 my %RESERVED = (
13 'Array' => 'Data::Perl::Collection::Array::MooseLike',
14 'Hash' => 'Data::Perl::Collection::Hash::MooseLike',
15 'String' => 'Data::Perl::String::MooseLike',
16 'Bool' => 'Data::Perl::Bool::MooseLike',
17 'Number' => 'Data::Perl::Number::MooseLike',
18 'Code' => 'Data::Perl::Code',
19 );
20 13 my %REVERSED = reverse %RESERVED;
21  
22 31 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
23 31 my ($class) = @_;
24  
25 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 13 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 31 my $target = caller;
29 31 ▶ if (my $has = $target->can('has')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $has = $target->can("has")) 30 1 31
30 31 ▶ my $newsub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine has: called CC=1 SCAR=0.0
31 31 $has->(process_has(@_));
32 30 };
33  
34 30 ▶ if (Moo::Role->is_role($target)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Moo::Role'->is_role($target)) { } 3 27 30
35 3 Moo::Role::_install_tracked($target, "has", $newsub);
36 }
37 else {
38 27 Moo::_install_tracked($target, "has", $newsub);
39 }
40 }
41 }
42  
43 36 ▶sub process_has {
Subroutine process_has: called CC=11 SCAR=25.8
Pod documented
44 36 my ($name, %opts) = @_;
45 36 my $handles = $opts{handles};
46 36 ▶ return ($name, %opts) if not $handles or ref $handles ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not $handles or ref $handles ne "HASH" 2 34 36
Condition100%3/3
not $handles or ref $handles ne "HASH"
l!l&&r!l&&!r
1134
MC/DC100%2/2
not $handles or ref $handles ne "HASH"
not $handles ref $handles ne "HASH"
Truth table100%3/3
not(A) or B
A= not $handlesB= ref $handles ne "HASH"
ABresult
1X1
011
000
47  
48 34 ▶ if (my $via = delete $opts{handles_via}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $via = delete $opts{"handles_via"}) 30 4 34
49 30 ▶ $via = ref $via eq 'ARRAY' ? $via->[0] : $via;
Branch: 1 branch
BranchTrueFalseTotal
ref $via eq 'ARRAY' ? : 0 30 30
50  
51 # try to load the reserved mapping, if it exists, else the full name
52 30 ▶ $via = $RESERVED{$via} || $via;
Condition33%1/3
$RESERVED{$via} || $via
l!l&&r!l&&!r
3000
MC/DC0%0/2
$RESERVED{$via} || $via
$RESERVED{$via} $via
Truth table33%1/3
A || B
A= $RESERVED{$via}B= $via
ABresult
1X1
011
000
53 30 require_module($via);
54  
55 # clone handles for HandlesMoose support
56 30 my %handles_clone = %$handles;
57  
58 30 ▶ while (my ($target, $delegation) = each %$handles) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$handles) 325 30 355
59 # if passed an array, handle the curry
60 325 ▶ if (ref $delegation eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $delegation eq 'ARRAY') { } 107 218 325
elsif (ref $delegation eq '') { } 218 0 218
61 107 my ($method, @curry) = @$delegation;
62 107 ▶ if ($via->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($via->can($method)) 33 74 107
63 33 $handles->{$target} = ['${\\'.$via.'->can("'.$method.'")}', @curry];
64 }
65 }
66 elsif (ref $delegation eq '') {
67 218 ▶ if ($via->can($delegation)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($via->can($delegation)) 76 142 218
68 76 $handles->{$target} = '${\\'.$via.'->can("'.$delegation.'")}';
69 }
70 }
71 }
72  
73 # install our support for moose upgrading of class/role
74 # we deleted the handles_via key above, but install it as a native trait
75 30 my $inflator = $opts{moosify};
76 0 ▶ $opts{moosify} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
77 0 my ($spec) = @_;
78  
79 0 $spec->{handles} = \%handles_clone;
80 0 ▶ $spec->{traits} = [$REVERSED{$via} || $via];
Condition0%0/3
$REVERSED{$via} || $via
l!l&&r!l&&!r
000
MC/DC0%0/2
$REVERSED{$via} || $via
$REVERSED{$via} $via
Truth table0%0/3
A || B
A= $REVERSED{$via}B= $via
ABresult
1X1
011
000
81  
82 # pass through if needed
83 0 ▶ $inflator->($spec) if ref($inflator) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $inflator eq "CODE" 0 0 0
84 30 };
85 }
86  
87 34 ($name, %opts);
88 }
89  
90 13 1;
91  
92 =pod
93  
94 =encoding UTF-8
95  
96 =head1 NAME
97  
98 MooX::HandlesVia - NativeTrait-like behavior for Moo.
99  
100 =head1 VERSION
101  
102 version 0.001009
103  
104 =head1 SYNOPSIS
105  
106 {
107 package Hashy;
108 use Moo;
109 use MooX::HandlesVia;
110  
111 has hash => (
112 is => 'rw',
113 handles_via => 'Hash',
114 handles => {
115 get_val => 'get',
116 set_val => 'set',
117 all_keys => 'keys'
118 }
119 );
120 }
121  
122 my $h = Hashy->new(hash => { a => 1, b => 2});
123  
124 $h->get_val('b'); # 2
125  
126 $h->set_val('a', 'BAR'); # sets a to BAR
127  
128 my @keys = $h->all_keys; # returns a, b
129  
130 =head1 DESCRIPTION
131  
132 MooX::HandlesVia is an extension of Moo's 'handles' attribute functionality. It
133 provides a means of proxying functionality from an external class to the given
134 atttribute. This is most commonly used as a way to emulate 'Native Trait'
135 behavior that has become commonplace in Moose code, for which there was no Moo
136 alternative.
137  
138 =head1 SHORTCOMINGS
139  
140 Due to current Moo implementation details there are some deficiencies in how
141 MooX::HandlesVia in comparison to what you would expect from Moose native
142 traits.
143  
144 =over 4
145  
146 =item * methods delegated via the Moo 'handles' interface are passed the
147 attribue value directly. and there is no way to access the parent class. This
148 means if an attribute is updated any triggers or type coercions B<WILL NOT>
149 fire.
150  
151 =item * Moo attribute method delegations are passed the attribute value. This
152 is fine for references (objects, arrays, hashrefs..) it means simple scalar
153 types are B<READ ONLY>. This unfortunately means Number, String, Counter, Bool
154 cannot modify the attributes value, rendering them largely useless.
155  
156 =back
157  
158 If these are issues for you, consider L<Sub::HandlesVia>, which uses a
159 different architecture, respecting triggers and coercions, and allowing
160 read-write access to non-reference values. It should be possible to use
161 Sub::HandlesVia as a drop-in replacement for MooX::HandlesVia.
162  
163 =head1 PROVIDED INTERFACE/FUNCTIONS
164  
165 =over 4
166  
167 =item B<process_has(@_)>
168  
169 MooX::HandlesVia preprocesses arguments passed to has() attribute declarations
170 via the process_has function. In a given Moo class, If 'handles_via' is set to
171 a ClassName string, and 'handles' is set with a hashref mapping of desired moo
172 class methods that should map to ClassName methods, process_has() will create
173 the appropriate binding to create the mapping IF ClassName provides that named
174 method.
175  
176 has options => (
177 is => 'rw',
178 handles_via => 'Array',
179 handles => {
180 mixup => 'shuffle',
181 unique_options => 'uniq',
182 all_options => 'elements'
183 }
184 );
185  
186 =back
187  
188 The following handles_via keywords are reserved as shorthand for mapping to
189 L<Data::Perl>:
190  
191 =over 4
192  
193 =item * B<Hash> maps to L<Data::Perl::Collection::Hash::MooseLike>
194  
195 =item * B<Array> maps to L<Data::Perl::Collection::Array::MooseLike>
196  
197 =item * B<String> maps to L<Data::Perl::String::MooseLike>
198  
199 =item * B<Number> maps to L<Data::Perl::Number::MooseLike>
200  
201 =item * B<Bool> maps to L<Data::Perl::Bool::MooseLike>
202  
203 =item * B<Code> maps to L<Data::Perl::Code>
204  
205 =back
206  
207 =head1 SEE ALSO
208  
209 =over 4
210  
211 =item * L<Moo>
212  
213 =item * L<MooX::late>
214  
215 =item * L<Sub::HandlesVia>
216  
217 =back
218  
219 =head1 ORIGINAL AUTHOR
220  
221 Matthew Phillips <mattp@cpan.org>
222  
223 =head1 AUTHOR
224  
225 Toby Inkster <tobyink@cpan.org>
226  
227 =head1 COPYRIGHT AND LICENSE
228  
229 This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>.
230  
231 This is free software; you can redistribute it and/or modify it under
232 the same terms as the Perl 5 programming language system itself.
233  
234 =cut
235  
236 __END__
↑ Summary Data/Perl/Collection/Hash/MooseLike.pm »