Focus/Lens/HashArray/Index.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 88.2% 30 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 96.0% 121 / 126 CC 21 SCAR 19.0
CC
21
Cov
96%
_setter
12.0
new
5.1
_getter
4.0
CRAP 6.7
↑ Summary Focus/Lens/Dynamic.pm »
1 package Data::Focus::Lens::HashArray::Index;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use parent qw(Data::Focus::Lens);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Data::Focus::LensMaker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 our @CARP_NOT = qw(Data::Focus::Lens Data::Focus);
10  
11 356 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
12 356 my ($class, %args) = @_;
13 356 my $indices = [];
14 356 ▶ if(exists($args{index})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"index"}) 356 0 356
15 356 ▶ if(ref($args{index}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'index'} eq 'ARRAY') { } 101 255 356
16 101 $indices = $args{index};
17 }else {
18 255 $indices = [$args{index}];
19 }
20 }
21 356 ▶ croak "index must be mandatory" if !@$indices;
Branch: 1 branch
BranchTrueFalseTotal
unless @$indices 0 356 356
22 356 ▶ croak "index must be defined" if grep { !defined($_) } @$indices;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @$indices 0 356 356
23 my $self = bless {
24 indices => $indices,
25 immutable => $args{immutable},
26 allow_blessed => $args{allow_blessed},
27 356 }, $class;
28 356 return $self;
29 }
30  
31 29544 ▶sub _type_of {
Subroutine _type_of: called CC=3 SCAR=11.0
32 29544 my ($self, $target) = @_;
33 29544 ▶ if($self->{allow_blessed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'allow_blessed'}) { } 14805 14739 29544
34 14805 my $ref = reftype($target);
35 14805 ▶ return defined($ref) ? $ref : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $ref ? : 12841 1964 14805
36 }else {
37 14739 return ref($target);
38 }
39 }
40  
41 17923 ▶sub _getter {
Subroutine _getter: called CC=4 SCAR=13.9
42 17923 my ($self, $whole) = @_;
43 17923 my $type = $self->_type_of($whole);
44 17923 ▶ if(!defined($whole)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $whole) { } 3204 14719 17923
elsif ($type eq 'ARRAY') { } 6592 8127 14719
elsif ($type eq 'HASH') { } 6158 1969 8127
45 ## slots for autovivification
46 3204 return map { undef } @{$self->{indices}};
47 }elsif($type eq "ARRAY") {
48 6592 my @indices = map { int($_) } @{$self->{indices}};
49 6592 return @{$whole}[@indices];
50 }elsif($type eq "HASH") {
51 6158 return @{$whole}{@{$self->{indices}}};
52 }else {
53 ## no slot. cannot set.
54 1969 return ();
55 }
56 }
57
58 16575 ▶sub _setter {
Subroutine _setter: called CC=12 SCAR=24.9
59 16575 my ($self, $whole, @parts) = @_;
60 16575 ▶ return $whole if !@parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 1852 14723 16575
61 14723 ▶ if(!defined($whole)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $whole) 3102 11621 14723
62 ## autovivifying
63 3102 ▶ if(grep { $_ !~ /^\d+$/ } @{$self->{indices}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not $_ =~ /^\d+$/;} @{$$self{'indices'};}) { } 1559 1543 3102
64 1559 return +{ map { $self->{indices}[$_] => $parts[$_] } 0 .. $#{$self->{indices}} };
65 }else {
66 1543 my $ret = [];
67 1543 $ret->[$self->{indices}[$_]] = $parts[$_] foreach 0 .. $#{$self->{indices}};
68 1543 return $ret;
69 }
70 }
71 11621 my $type = $self->_type_of($whole);
72 11621 ▶ if($type eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 6007 5614 11621
elsif ($type eq 'HASH') { } 5614 0 5614
73 6007 my @indices = map { int($_) } @{$self->{indices}};
74 6007 ▶ my $ret = $self->{immutable} ? [@$whole] : $whole;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'immutable'} ? : 2995 3012 6007
75 6007 foreach my $i (0 .. $#indices) {
76 10637 my $index = $indices[$i];
77 10637 ▶ croak "$index: negative out-of-range index" if $index < -(@$ret);
Branch: 1 branch
BranchTrueFalseTotal
if $index < -@$ret 6 10631 10637
78 10631 $ret->[$index] = $parts[$i];
79 }
80 6001 return $ret;
81 }elsif($type eq "HASH") {
82 5614 ▶ my $ret = $self->{immutable} ? {%$whole} : $whole;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'immutable'} ? : 2788 2826 5614
83 5614 $ret->{$self->{indices}[$_]} = $parts[$_] foreach 0 .. $#{$self->{indices}};
84 5614 return $ret;
85 }else {
86 0 confess "This should not be executed because the getter should return an empty list.";
87 }
88 }
89  
90 17 Data::Focus::LensMaker::make_lens_from_accessors(\&_getter, \&_setter);
91  
92 17 1;
93  
94 __END__
↑ Summary Focus/Lens/Dynamic.pm »