| 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
| |||||||||||||||||||
| 15 | 356 | ▶ | if(ref($args{index}) eq "ARRAY") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 22 | 356 | ▶ | croak "index must be defined" if grep { !defined($_) } @$indices; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 34 | 14805 | my $ref = reftype($target); | |||||||||||||||||
| 35 | 14805 | ▶ | return defined($ref) ? $ref : ""; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 61 | 14723 | ▶ | if(!defined($whole)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 62 | ## autovivifying | ||||||||||||||||||
| 63 | 3102 | ▶ | if(grep { $_ !~ /^\d+$/ } @{$self->{indices}}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 73 | 6007 | my @indices = map { int($_) } @{$self->{indices}}; | |||||||||||||||||
| 74 | 6007 | ▶ | my $ret = $self->{immutable} ? [@$whole] : $whole; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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__ | ||||||||||||||||||