blib/lib/MooX/PDL/Role/Proxy.pm

filter: Statementstmt 90.2% 139 / 154 Branchbran 67.8% 19 / 28 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 88.1% 37 / 42 Podpod 100% 14 / 14 totaltotal 85.3% 210 / 246 CC 21 SCAR 10.6
CC
21
Cov
85%
__ANON__
12.0
__ANON__
6.5
_apply_to_tagged_attrs
5.0
CRAP 2.9
↑ Summary
1 package MooX::PDL::Role::Proxy;
2  
3 # ABSTRACT: treat a container of ndarrays (piddles) as if it were an ndarray (piddle)
4  
5 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.08';
10  
11 7 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use PDL::Primitive ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Hash::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use List::Util 1.45 'uniqstr';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Lexical::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 INPLACE_SET => 1,
25 INPLACE_STORE => 2,
26 7 };
27  
28 7 ▶use MooX::TaggedAttributes -tags => [qw( piddle ndarray )];
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 0 ▶my $croak = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 require Carp;
32 0 goto \&Carp::croak;
33 7 };
34  
35 0 ▶my $can_either = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
36 0 my $self = shift;
37 0 for ( @_ ) {
38 0 ▶ return $self->can( $_ ) // next;
Condition0%0/2
$self->can($_) // (next)
l!l
00
MC/DC0%0/1
$self->can($_) // (next)
$self->can($_)
Truth table0%0/2
A
A= $self->can($_)
Aresult
11
00
39 }
40 7 };
41  
42 lexical_has clone_v2 => (
43 is => 'lazy',
44 weak_ref => 1,
45 reader => \( my $clone_v2 ),
46 27 ▶ default => sub { $_[0]->can( '_clone_with_ndarrays' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 7 );
48  
49 lexical_has clone_v1 => (
50 is => 'lazy',
51 weak_ref => 1,
52 reader => \( my $clone_v1 ),
53 0 ▶ default => sub { $_[0]->can( 'clone_with_piddles' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 7 );
55  
56 7 lexical_has clone_args => (
57 is => 'rw',
58 reader => \( my $get_clone_args ),
59 clearer => \( my $clear_clone_args ),
60 writer => \( my $set_clone_args ),
61 predicate => \( my $has_clone_args ),
62 );
63  
64 lexical_has attr_subs => (
65 is => 'ro',
66 isa => HashRef,
67 reader => \( my $attr_subs ),
68 38 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 7 );
70  
71 7 lexical_has 'is_inplace' => (
72 is => 'rw',
73 clearer => \( my $clear_inplace ),
74 reader => \( my $is_inplace ),
75 writer => \( my $set_inplace ),
76 default => 0,
77 );
78  
79  
80 27 ▶my $clone = sub {
Subroutine __ANON__: called CC=4 SCAR=18.7
81 27 my ( $self, $attrs ) = @_;
82  
83 27 ▶ if ( my $func = $self->$clone_v2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $func = $self->$clone_v2) { } 27 0 27
elsif ($func = $self->$clone_v1) { } 0 0 0
84 27 ▶ $self->$func( $attrs, $self->$has_clone_args ? $self->$get_clone_args : () );
Branch: 1 branch
BranchTrueFalseTotal
$self->$has_clone_args ? : 1 26 27
85 }
86 elsif ( $func = $self->$clone_v1 ) {
87 0 $self->$func( %{$attrs} );
88 }
89 else {
90 0 $croak->( "couldn't find clone method for class '@{[ ref $self ]}'" );
91 }
92 7 };
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111 has _ndarrays => (
112 is => 'lazy',
113 isa => ArrayRef [Str],
114 init_arg => undef,
115 clearer => 1,
116 40 ▶ builder => sub {
Subroutine _build__ndarrays: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
117 40 my $self = shift;
118 40 my $tags = $self->_tags->tag_hash;
119 # make backwards compatible with 'piddle'. the returned hash
120 # is locked, so only access keys known to exist
121 [
122 uniqstr
123 41 map { keys %{ $tags->{$_} } }
124 40 grep { /^ndarray|piddle$/ } keys %{$tags} ];
125 },
126 7 );
127  
128 # alias for backwards compatibility
129 7 *_piddles = \&_ndarrays;
130 7 *_clear_piddles = \&_clear_ndarrays;
131 7 *_build_piddles = \&_build__ndarrays;
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150 34 ▶sub _apply_to_tagged_attrs {
Subroutine _apply_to_tagged_attrs: called CC=5 SCAR=16.1
151 34 my ( $self, $action ) = @_;
152  
153 34 my $inplace = $self->$is_inplace;
154  
155 my %attr = map { ## no critic (ComplexMappings)
156 68 my $field = $_;
157 68 $field => $action->( $self->$field, $inplace );
158 34 } @{ $self->_ndarrays };
159  
160 34 ▶ if ( $inplace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace) 12 22 34
161 12 $self->$clear_inplace;
162  
163 12 ▶ if ( $inplace == INPLACE_SET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inplace == 1) { } 11 1 12
elsif ($inplace == 2) { } 1 0 1
164 11 $self->_set_attr( %attr );
165 }
166  
167 elsif ( $inplace == INPLACE_STORE ) {
168 1 for my $attr ( keys %attr ) {
169 # $attr{$attr} may be linked to $self->$attr,
170 # so if we reshape $self->$attr, it really
171 # messes up $attr{$attr}. sever it to be sure.
172 2 my $pdl = $attr{$attr}->sever;
173 2 ( my $tmp = $self->$attr->reshape( $pdl->dims ) ) .= $pdl;
174 }
175 }
176  
177 else {
178 0 $croak->( "unrecognized inplace flag value: $inplace\n" );
179 }
180  
181 12 return $self;
182 }
183  
184 22 return $self->$clone( \%attr );
185 }
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206 13 ▶sub inplace {
Subroutine inplace: called CC=2 SCAR=7.2
Pod documented
207 13 ▶ $_[0]->$set_inplace( @_ > 1 ? $_[1] : INPLACE_SET );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 13 13
208 13 return $_[0];
209 }
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229 1 ▶sub inplace_store {
Subroutine inplace_store: called CC=1 SCAR=0.0
Pod documented
230 1 $_[0]->$set_inplace( INPLACE_STORE );
231 1 return $_[0];
232 }
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252 0 ▶sub inplace_set {
Subroutine inplace_set: not called CC=1 SCAR=6.9
Pod documented
253 0 $_[0]->$set_inplace( INPLACE_SET );
254 0 return $_[0];
255 }
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280 3 ▶sub set_inplace {
Subroutine set_inplace: called CC=3 SCAR=11.7
Pod documented
281 3 ▶ 2 == @_ or $croak->( 'set_inplace requires two arguments' );
Branch: 1 branch
BranchTrueFalseTotal
2 == @_ or &$croak("set_inplace requires two arguments") 3 0 3
282 3 ▶ $_[1] >= 0
Branch: 1 branch
BranchTrueFalseTotal
$_[1] >= 0 and $_[0]->$set_inplace($_[1]) 3 0 3
283 && $_[0]->$set_inplace( $_[1] );
284 3 return;
285 }
286  
287  
288  
289  
290  
291  
292  
293  
294  
295 10 ▶sub is_inplace { goto &$is_inplace }
Subroutine is_inplace: called CC=1 SCAR=0.0
Pod documented
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309 6 ▶sub copy {
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
310 6 my $self = shift;
311  
312 6 ▶ if ( $self->is_inplace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_inplace) 1 5 6
313 1 $self->set_inplace( 0 );
314 1 return $self;
315 }
316 5 my %attr = map { $_ => $self->$_->copy } @{ $self->_ndarrays };
317 5 return $self->$clone( \%attr );
318 }
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329 4 ▶sub sever {
Subroutine sever: called CC=2 SCAR=6.9
Pod documented
330 4 my $self = shift;
331 4 $self->$_->sever for @{ $self->_ndarrays };
332 4 return $self;
333 }
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344 16 ▶sub index { ## no critic (Homonym)
Subroutine index: called CC=1 SCAR=0.0
Pod documented
345 16 my ( $self, $index ) = @_;
346 16 ▶ return $self->_apply_to_tagged_attrs( sub { $_[0]->index( $index ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
347 }
348  
349 # is there a use for this?
350 # sub which {
351 # my ( $self, $which ) = @_;
352 # return PDL::Primitive::which(
353 # 'CODE' eq ref $which
354 # ? do { local $_ = $self; $which->() }
355 # : $which
356 # );
357 # }
358  
359  
360  
361  
362  
363  
364  
365  
366  
367  
368  
369 4 ▶sub at {
Subroutine at: called CC=1 SCAR=0.0
Pod documented
370 4 my ( $self, @idx ) = @_;
371 4 wrap_hash( { map { $_ => $self->$_->at( @idx ) } @{ $self->_ndarrays } } );
372 }
373  
374  
375  
376  
377  
378  
379  
380  
381  
382  
383 16 ▶sub where {
Subroutine where: called CC=1 SCAR=0.0
Pod documented
384 16 my ( $self, $where ) = @_;
385  
386 16 ▶ return $self->_apply_to_tagged_attrs( sub { $_[0]->where( $where ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
387 }
388  
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399 1 ▶sub _set_clone_args {
Subroutine _set_clone_args: called CC=1 SCAR=0.0
400 1 $_[0]->$set_clone_args( $_[1] );
401 }
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412 0 ▶sub _clear_clone_args {
Subroutine _clear_clone_args: not called CC=1 SCAR=6.9
413 0 $_[0]->$clear_clone_args;
414 }
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427 38 ▶sub _set_attr {
Subroutine _set_attr: called CC=4 SCAR=14.2
428 38 my ( $self, %attr ) = @_;
429 38 my $subs = $self->$attr_subs;
430  
431 38 for my $key ( keys %attr ) {
432 76 my $sub = $subs->{$key};
433  
434 76 ▶ if ( !defined $sub ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub) 76 0 76
435 76 ▶ Scalar::Util::weaken( $subs->{$key} = $self->can( "_set_${key}" ) // $self->can( $key ) );
Condition33%1/3
$self->can("_set_$key") // $self->can($key)
l!l&&r!l&&!r
7600
MC/DC0%0/2
$self->can("_set_$key") // $self->can($key)
$self->can("_set_$key") $self->can($key)
Truth table33%1/3
A // B
A= $self->can("_set_$key")B= $self->can($key)
ABresult
1X1
011
000
436 76 $sub = $subs->{$key};
437 }
438  
439 76 $sub->( $self, $attr{$key} );
440 }
441  
442 38 return $self;
443 }
444  
445  
446  
447  
448  
449  
450  
451  
452  
453  
454  
455  
456  
457  
458  
459  
460  
461  
462  
463  
464  
465  
466  
467 3 ▶sub qsort {
Subroutine qsort: called CC=1 SCAR=0.0
Pod documented
468  
469 3 $_[0]->index( $_[0]->qsorti );
470 }
471  
472  
473  
474  
475  
476  
477  
478  
479  
480  
481  
482  
483 2 ▶sub qsort_on {
Subroutine qsort_on: called CC=1 SCAR=0.0
Pod documented
484  
485 2 my ( $self, $attr ) = @_;
486  
487 2 $self->index( $attr->qsorti );
488 }
489  
490  
491  
492  
493  
494  
495  
496  
497  
498  
499  
500  
501  
502  
503  
504 6 ▶sub clip_on {
Subroutine clip_on: called CC=4 SCAR=14.0
Pod documented
505  
506 6 my ( $self, $attr, $min, $max ) = @_;
507  
508 6 my $mask;
509  
510 6 ▶ if ( defined $min ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $min) { } 4 2 6
elsif (defined $max) { } 2 0 2
511 4 $mask = $attr >= $min;
512 4 ▶ $mask &= $attr < $max
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 2 2 4
513 if defined $max;
514 }
515 elsif ( defined $max ) {
516 2 $mask = $attr < $max;
517 }
518 else {
519 0 $croak->( "one of min or max must be defined\n" );
520 }
521  
522 6 $self->where( $mask );
523 }
524  
525  
526  
527  
528  
529  
530  
531  
532  
533  
534  
535  
536  
537 2 ▶sub slice {
Subroutine slice: called CC=1 SCAR=0.0
Pod documented
538  
539 2 my ( $self, $slice ) = @_;
540  
541 2 ▶ return $self->_apply_to_tagged_attrs( sub { $_[0]->slice( $slice ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
542 }
543  
544  
545  
546 7 1;
547  
548 #
549 # This file is part of MooX-PDL-Role-Proxy
550 #
551 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
552 #
553 # This is free software, licensed under:
554 #
555 # The GNU General Public License, Version 3, June 2007
556 #
557  
558 __END__
↑ Summary