blib/lib/Zuzu/Value/PairList.pm

filter: Statementstmt 58.6% 115 / 196 Branchbran 50.0% 33 / 66 Conditioncond 16.6% 3 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 65.8% 27 / 41 Podpod 0.0% 0 / 28 totaltotal 49.5% 178 / 359 CC 43 SCAR 25.0
CC
43
Cov
54%
add
39.9
remove
38.8
enumerate
6.0
CRAP 12.2
« blib/lib/Zuzu/Module/XML.pm ↑ Summary blib/lib/Zuzu/Module/Eval.pm »
1 package Zuzu::Value::PairList;
2  
3 69 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 69 our $VERSION = '0.007002';
6  
7 69 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 69 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 69 ▶use Zuzu::Value::Bag;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 69 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 69 ▶use Zuzu::Weak qw( slot_value store_value );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 29 ▶has 'list' => ( is => 'rw', default => sub { [] } ); # [ [key, value], ... ]
Subroutine __ANON__: called CC=1 SCAR=0.0
15 93 ▶has 'weak' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 149 ▶sub _normalize_key {
Subroutine _normalize_key: called CC=2 SCAR=7.2
18 149 my ( $key ) = @_;
19  
20 149 ▶ return defined $key ? "$key" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 149 0 149
21 }
22  
23 79 ▶sub _store_at {
Subroutine _store_at: called CC=2 SCAR=6.9
24 79 my ( $self, $index, $key, $value, $weak ) = @_;
25  
26 79 $self->list->[$index] = [ _normalize_key($key), undef ];
27 79 ▶ $self->weak->[$index] = $weak ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$weak ? : 9 70 79
28 79 store_value( \$self->list->[$index][1], $value, $weak );
29  
30 79 return $self;
31 }
32  
33 79 ▶sub _append {
Subroutine _append: called CC=1 SCAR=0.0
34 79 my ( $self, $key, $value, $weak ) = @_;
35  
36 79 my $index = scalar @{ $self->list };
37  
38 79 return $self->_store_at( $index, $key, $value, $weak );
39 }
40  
41 112 ▶sub _value_at {
Subroutine _value_at: called CC=1 SCAR=0.0
42 112 my ( $self, $index ) = @_;
43  
44 112 return slot_value( \$self->list->[$index][1] );
45 }
46  
47 11 ▶sub copy {
Subroutine copy: called CC=3 SCAR=11.0
Pod undocumented
48 11 my ( $self ) = @_;
49 11 my $copy = Zuzu::Value::PairList->new( list => [] );
50  
51 11 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 18 11 29
52 18 ▶ $copy->_append(
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 1 17 18
53 $self->list->[$i][0],
54 $self->_value_at($i),
55 $self->weak->[$i] ? 1 : 0,
56 );
57 }
58  
59 11 return $copy;
60 }
61  
62 4 ▶sub keys {
Subroutine keys: called CC=1 SCAR=0.0
Pod undocumented
63 4 my ( $self ) = @_;
64 4 my @keys = map { $_->[0] } @{ $self->list };
65  
66 4 return Zuzu::Value::Array->new( items => \@keys );
67 }
68  
69 1 ▶sub values {
Subroutine values: called CC=2 SCAR=6.9
Pod undocumented
70 1 my ( $self ) = @_;
71 1 my @values;
72 1 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 3 1 4
73 3 push @values, $self->_value_at($i);
74 }
75  
76 1 return Zuzu::Value::Array->new( items => \@values );
77 }
78  
79 0 ▶sub enumerate {
Subroutine enumerate: not called CC=2 SCAR=17.9
Pod undocumented
80 0 my ( $self ) = @_;
81 0 my @pairs;
82 0 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 0 0 0
83 0 push @pairs, [ $self->list->[$i][0], $self->_value_at($i) ];
84 }
85  
86 0 return Zuzu::Value::Bag->new( items => \@pairs );
87 }
88  
89 4 ▶sub contains_key {
Subroutine contains_key: called CC=3 SCAR=11.4
Pod undocumented
90 4 my ( $self, $key ) = @_;
91 4 $key = _normalize_key( $key );
92 4 for my $pair ( @{ $self->list } ) {
93 4 ▶ return 1 if $pair->[0] eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $pair->[0] eq $key 4 0 4
94 }
95  
96 0 return 0;
97 }
98  
99 3 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
100 3 my ( $self, $key ) = @_;
101  
102 3 return $self->contains_key( $key );
103 }
104  
105 1 ▶sub defined {
Subroutine defined: called CC=4 SCAR=15.0
Pod undocumented
106 1 my ( $self, $key ) = @_;
107 1 $key = _normalize_key( $key );
108 1 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 1 0 1
109 1 ▶ next if $self->list->[$i][0] ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->list->[$i][0] ne $key 0 1 1
110 1 ▶ return defined $self->_value_at($i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_value_at($i) ? : 0 1 1
111 }
112  
113 0 return 0;
114 }
115  
116 54 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
117 54 my ( $self, $key, $default ) = @_;
118 54 $key = _normalize_key( $key );
119 54 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 65 24 89
120 65 ▶ return $self->_value_at($i) if $self->list->[$i][0] eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->list->[$i][0] eq $key 30 35 65
121 }
122  
123 24 return $default;
124 }
125  
126 9 ▶sub get_all {
Subroutine get_all: called CC=3 SCAR=11.0
Pod undocumented
127 9 my ( $self, $key ) = @_;
128 9 $key = _normalize_key( $key );
129 9 my @values;
130 9 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 31 9 40
131 31 ▶ push @values, $self->_value_at($i)
Branch: 1 branch
BranchTrueFalseTotal
if $self->list->[$i][0] eq $key 16 15 31
132 if $self->list->[$i][0] eq $key;
133 }
134  
135 9 return Zuzu::Value::Array->new( items => \@values );
136 }
137  
138 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my ( $self, $key ) = @_;
140  
141 0 return $self->get_all( $key );
142 }
143  
144 0 ▶sub to_Array {
Subroutine to_Array: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my ( $self ) = @_;
146 0 my @pairs;
147 0 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 0 0 0
148 0 push @pairs, [ $self->list->[$i][0], $self->_value_at($i) ];
149 }
150  
151 0 return Zuzu::Value::Array->new( items => \@pairs );
152 }
153  
154 0 ▶sub to_Iterator {
Subroutine to_Iterator: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my ( $self ) = @_;
156  
157 0 my @keys = map { $_->[0] } @{ $self->list };
158 0 my $index = 0;
159  
160 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
161 0 ▶ die 'ExhaustedException' if $index >= @keys;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= @keys 0 0 0
162 0 my $value = $keys[$index];
163 0 $index++;
164  
165 0 return $value;
166 0 };
167 }
168  
169 44 ▶sub add {
Subroutine add: called CC=8 SCAR=36.9
Pod undocumented
170 44 my ( $self, @args ) = @_;
171  
172 44 ▶ if ( @args == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 2) 44 0 44
173 44 my ( $key, $value ) = @args;
174 44 $self->_append( $key, $value, 0 );
175  
176 44 return $self;
177 }
178  
179 0 for my $pair ( @args ) {
180 0 ▶ if ( ref($pair) eq 'ARRAY' and @{ $pair } >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pair eq "ARRAY" and @{$pair;} >= 2) 0 0 0
Condition0%0/3
ref $pair eq "ARRAY" and @{$pair;} >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
ref $pair eq "ARRAY" and @{$pair;} >= 2
ref $pair eq "ARRAY" @{$pair;} >= 2
Truth table0%0/3
A and B
A= ref $pair eq "ARRAY"B= @{$pair;} >= 2
ABresult
0X0
100
111
181 0 $self->_append( $pair->[0], $pair->[1], 0 );
182 0 next;
183 }
184 0 ▶ if ( blessed($pair) and $pair->can('key') and $pair->can('value') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($pair) and $pair->can("key") and $pair->can("value")) 0 0 0
Condition0%0/6
builtin::blessed($pair) and $pair->can("key") and $pair->can("value")
!ll&&!rl&&r
000
builtin::blessed($pair) and $pair->can("key")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($pair) and $pair->can("key") and $pair->can("value")
builtin::blessed($pair) $pair->can("key") $pair->can("value")
Truth table0%0/4
A and B and C
A= builtin::blessed($pair)B= $pair->can("key")C= $pair->can("value")
ABCresult
0XX0
10X0
1100
1111
185 0 $self->_append( $pair->key, $pair->value, 0 );
186 0 next;
187 }
188 }
189  
190 0 return $self;
191 }
192  
193 6 ▶sub add_weak {
Subroutine add_weak: called CC=1 SCAR=0.0
Pod undocumented
194 6 my ( $self, $key, $value ) = @_;
195  
196 6 return $self->_append( $key, $value, 1 );
197 }
198  
199 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
200 0 my ( $self, $key, $value ) = @_;
201  
202 0 return $self->add( $key, $value );
203 }
204  
205 1 ▶sub set_weak {
Subroutine set_weak: called CC=1 SCAR=0.0
Pod undocumented
206 1 my ( $self, $key, $value ) = @_;
207  
208 1 return $self->add_weak( $key, $value );
209 }
210  
211 2 ▶sub remove {
Subroutine remove: called CC=13 SCAR=36.6
Pod undocumented
212 2 my ( $self, $k_or_cb ) = @_;
213  
214 2 ▶ if ( ref($k_or_cb) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $k_or_cb eq "CODE") 0 2 2
215 0 my @kept;
216 my @weak;
217 0 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 0 0 0
218 0 my $pair = $self->list->[$i];
219 0 my $value = $self->_value_at($i);
220 0 ▶ if ( !$k_or_cb->( [ $pair->[0], $value ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$k_or_cb([$pair->[0], $value])) 0 0 0
221 0 push @kept, [ $pair->[0], undef ];
222 0 ▶ push @weak, $self->weak->[$i] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 0 0 0
223 0 store_value( \$kept[-1][1], $value, $weak[-1] );
224 }
225 }
226 0 $self->list( \@kept );
227 0 $self->weak( \@weak );
228  
229 0 return $self;
230 }
231  
232 0 ▶ if ( ref($k_or_cb) eq 'ARRAY' and @{ $k_or_cb } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $k_or_cb eq 'ARRAY' and @{$k_or_cb;}) { } 0 2 2
elsif (ref $k_or_cb and builtin::blessed($k_or_cb) and $k_or_cb->can('key')) { } 0 4 4
Condition33%1/3
ref $k_or_cb eq "ARRAY" and @{$k_or_cb;}
!ll&&!rl&&r
200
MC/DC0%0/2
ref $k_or_cb eq "ARRAY" and @{$k_or_cb;}
ref $k_or_cb eq "ARRAY" @{$k_or_cb;}
Truth table33%1/3
A and B
A= ref $k_or_cb eq "ARRAY"B= @{$k_or_cb;}
ABresult
0X0
100
111
233 0 $k_or_cb = $k_or_cb->[0];
234 }
235 ▶ elsif ( ref($k_or_cb) and blessed($k_or_cb) and $k_or_cb->can('key') ) {
Condition33%2/6
ref $k_or_cb and builtin::blessed($k_or_cb) and $k_or_cb->can("key")
!ll&&!rl&&r
200
ref $k_or_cb and builtin::blessed($k_or_cb)
!ll&&!rl&&r
200
MC/DC0%0/3
ref $k_or_cb and builtin::blessed($k_or_cb) and $k_or_cb->can("key")
ref $k_or_cb builtin::blessed($k_or_cb) $k_or_cb->can("key")
Truth table25%1/4
A and B and C
A= ref $k_or_cbB= builtin::blessed($k_or_cb)C= $k_or_cb->can("key")
ABCresult
0XX0
10X0
1100
1111
236 0 $k_or_cb = $k_or_cb->key;
237 }
238 2 my $key = _normalize_key( $k_or_cb );
239 2 my @kept;
240 my @weak;
241 2 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 4 2 6
242 4 my $pair = $self->list->[$i];
243 4 ▶ next if $pair->[0] eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if $pair->[0] eq $key 2 2 4
244 2 my $value = $self->_value_at($i);
245 2 push @kept, [ $pair->[0], undef ];
246 2 ▶ push @weak, $self->weak->[$i] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 2 0 2
247 2 store_value( \$kept[-1][1], $value, $weak[-1] );
248 }
249 2 $self->list( \@kept );
250 2 $self->weak( \@weak );
251  
252 2 return $self;
253 }
254  
255 1 ▶sub for_each_pair {
Subroutine for_each_pair: called CC=2 SCAR=6.9
Pod undocumented
256 1 my ( $self, $callback ) = @_;
257 1 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 2 1 3
258 2 my $pair = $self->list->[$i];
259 2 $callback->( [ $pair->[0], $self->_value_at($i) ] );
260 }
261  
262 1 return $self;
263 }
264  
265 0 ▶sub for_each_key {
Subroutine for_each_key: not called CC=2 SCAR=17.9
Pod undocumented
266 0 my ( $self, $callback ) = @_;
267 0 for my $pair ( @{ $self->list } ) {
268 0 $callback->( $pair->[0] );
269 }
270  
271 0 return $self;
272 }
273  
274 0 ▶sub for_each_value {
Subroutine for_each_value: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my ( $self, $callback ) = @_;
276 0 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 0 0 0
277 0 $callback->( $self->_value_at($i) );
278 }
279  
280 0 return $self;
281 }
282  
283 0 ▶sub kv {
Subroutine kv: not called CC=2 SCAR=17.9
Pod undocumented
284 0 my ( $self ) = @_;
285 0 my @out;
286 0 ▶ for ( my $i = 0; $i < @{ $self->list }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->list;}) 0 0 0
287 0 push @out, $self->list->[$i][0], $self->_value_at($i);
288 }
289  
290 0 return Zuzu::Value::Array->new( items => \@out );
291 }
292  
293 0 ▶sub sorted_keys {
Subroutine sorted_keys: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my ( $self ) = @_;
295 0 my @keys = map { $_->[0] } @{ $self->list };
296 0 @keys = sort @keys;
297  
298 0 return Zuzu::Value::Array->new( items => \@keys );
299 }
300  
301 6 ▶sub length { scalar @{ $_[0]->list } }
Subroutine length: called CC=1 SCAR=0.0
Pod undocumented
302 0 ▶sub count { $_[0]->length }
Subroutine count: not called CC=1 SCAR=6.9
Pod undocumented
303 0 ▶sub empty { $_[0]->length ? 0 : 1 }
Subroutine empty: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->length ? : 0 0 0
304 0 ▶sub is_empty { $_[0]->empty }
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod undocumented
305  
306 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
307 0 my ( $self ) = @_;
308 0 $self->list( [] );
309 0 $self->weak( [] );
310  
311 0 return $self;
312 }
313  
314 1 ▶sub is_truthy { scalar @{ $_[0]->list } ? 1 : 0 }
Subroutine is_truthy: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]->list;} ? : 0 1 1
315  
316 69 1;
317  
318 =pod
319  
320 =head1 COPYRIGHT AND LICENCE
321  
322 B<< Zuzu::Value::PairList >> is copyright Toby Inkster.
323  
324 It is free software; you may redistribute it and/or modify it under
325 the terms of either the Artistic License 1.0 or the GNU General Public
326 License version 2.
327  
328 =cut
« blib/lib/Zuzu/Module/XML.pm ↑ Summary blib/lib/Zuzu/Module/Eval.pm »