Morph/Backend/Raw.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 88.2% 30 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 94.5% 105 / 111 CC 20 SCAR 22.0
CC
20
Cov
94%
__ANON__
15.1
__ANON__
6.0
BEGIN
1.0
CRAP 9.1
↑ Summary Morph.pm »
1 package Data::Morph::Backend::Raw;
2 2 $Data::Morph::Backend::Raw::VERSION = '1.140400';
3 #ABSTRACT: Provides a backend that produces simple HashRefs
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MooseX::Types::Moose(':all');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::DPath(qw|dpath dpathr|);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Devel::PartialDump('dump');
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 has new_instance =>
14 (
15 is => 'ro',
16 isa => CodeRef,
17 1 ▶ default => sub { sub { +{} } },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
18 2 );
19  
20  
21 4 ▶sub epilogue { }
Subroutine epilogue: called CC=1 SCAR=0.0
Pod documented
22  
23 with 'Data::Morph::Role::Backend' =>
24 {
25 input_type => HashRef,
26 get_val => sub
27 11 ▶ {
Subroutine __ANON__: called CC=6 SCAR=18.0
28 11 my ($obj, $key) = @_;
29 11 my @keys = split('\|', $key);
30 11 my @refs;
31  
32 11 ▶ if(scalar(@keys) > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys > 1) { } 4 7 11
33 {
34 4 foreach my $alt (@keys)
35 {
36 5 @refs = dpath($alt)->match($obj);
37 5 ▶ last if scalar(@refs);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @refs 4 1 5
38 }
39 }
40 else
41 {
42 7 @refs = dpath($key)->match($obj);
43 }
44  
45 11 ▶ die "No matching points for key '$key' in: \n". dump($obj)
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @refs 0 11 11
46 unless scalar(@refs);
47  
48 11 ▶ die "Too many maching points for '$key' in: \n". dump($obj)
Branch: 1 branch
BranchTrueFalseTotal
if scalar @refs > 1 0 11 11
49 if scalar(@refs) > 1;
50  
51 11 return $refs[0];
52 },
53 set_val => sub
54 20 ▶ {
Subroutine __ANON__: called CC=15 SCAR=27.1
55 20 my ($obj, $key, $val) = @_;
56  
57 20 ▶ if(index($key, '|') > -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($key, "|") != -1) 1 19 20
58 {
59 1 die "Alternations are not supported for write directives: '$key'"
60 }
61  
62 19 my @refs = dpathr($key)->match($obj);
63  
64 19 ▶ die "Too many maching points for '$key' in: \n". dump($obj)
Branch: 1 branch
BranchTrueFalseTotal
if scalar @refs > 1 0 19 19
65 if scalar(@refs) > 1;
66  
67 19 ▶ unless(scalar(@refs))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @refs) { } 0 19 19
68 {
69 19 my @paths = split('/', $key);
70 19 my $place = $obj;
71  
72 19 for(0..$#paths)
73 {
74 76 ▶ next if $paths[$_] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $paths[$_] eq "" 19 57 76
75 57 my $path = $paths[$_];
76
77 # handling arrays in path
78 57 ▶ if ($path =~ /\*\[\d+\]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\*\[\d+\]/) { } 13 44 57
79 {
80 13 my ($index) = $path =~ /\*\[(\d+)\]/;
81
82 13 ▶ if($_ == $#paths)
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == $#paths) { } 2 11 13
83 {
84 2 $place->[$index] = $val;
85 }
86 else
87 {
88 11 ▶ if (!defined($place->[$index]))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $place->[$index]) 9 2 11
89 {
90 9 ▶ $place->[$index] = ($paths[$_+1] =~ m/\*\[\d+\]/ ? [] : {});
Branch: 1 branch
BranchTrueFalseTotal
$paths[$_ + 1] =~ /\*\[\d+\]/ ? : 1 8 9
91 }
92
93 11 ▶ $place = ref($place->[$index]) eq 'HASH' ? \%{$place->[$index]} : \@{$place->[$index]};
Branch: 1 branch
BranchTrueFalseTotal
ref $place->[$index] eq 'HASH' ? : 10 1 11
94 }
95  
96 }
97 else
98 {
99 44 ▶ if($_ == $#paths)
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == $#paths) { } 17 27 44
100 {
101 17 $place->{$path} = $val;
102 }
103 else
104 {
105 27 ▶ if (!exists($place->{$path}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $place->{$path}) 22 5 27
106 {
107 22 ▶ $place->{$path} = ($paths[$_+1] =~ m/\*\[\d+\]/ ? [] : {});
Branch: 1 branch
BranchTrueFalseTotal
$paths[$_ + 1] =~ /\*\[\d+\]/ ? : 8 14 22
108 }
109
110 27 ▶ $place = ref($place->{$path}) eq 'HASH' ? \%{$place->{$path}} : \@{$place->{$path}};
Branch: 1 branch
BranchTrueFalseTotal
ref $place->{$path} eq 'HASH' ? : 15 12 27
111 }
112 }
113 }
114 }
115 else
116 {
117 0 ${$refs[0]} = $val;
118 }
119 },
120 2 };
121  
122 2 __PACKAGE__->meta->make_immutable();
123 2 1;
124  
125 __END__
↑ Summary Morph.pm »