blib/lib/Zuzu/Value/Dict.pm

filter: Statementstmt 77.4% 127 / 164 Branchbran 64.7% 22 / 34 Conditioncond 38.8% 7 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 80.0% 32 / 40 Podpod 40.7% 11 / 27 totaltotal 68.2% 200 / 293 CC 31 SCAR 18.3
CC
31
Cov
72%
remove
28.1
add
9.3
__ANON__
6.0
CRAP 6.2
« blib/lib/Zuzu/Marshal/CBOR.pm ↑ Summary blib/lib/Zuzu/Web/PSGI/CLI.pm »
1 package Zuzu::Value::Dict;
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 Zuzu::Value::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 69 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 69 ▶use Zuzu::Weak qw( slot_value store_value );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 26 ▶has 'map' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 4654 ▶has 'weak' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 8273 ▶sub _normalize_key {
Subroutine _normalize_key: called CC=2 SCAR=7.2
19 8273 my ( $key ) = @_;
20  
21 8273 ▶ return defined $key ? "$key" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 8273 0 8273
22 }
23  
24 7067 ▶sub _store_key {
Subroutine _store_key: called CC=2 SCAR=6.9
25 7067 my ( $self, $key, $value, $weak ) = @_;
26  
27 7067 $key = _normalize_key($key);
28 7067 ▶ $self->weak->{$key} = $weak ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$weak ? : 7 7060 7067
29 7067 store_value( \$self->map->{$key}, $value, $weak );
30  
31 7067 return $self;
32 }
33  
34 215 ▶sub _value_for_key {
Subroutine _value_for_key: called CC=1 SCAR=0.0
35 215 my ( $self, $key ) = @_;
36  
37 215 $key = _normalize_key($key);
38  
39 215 return slot_value( \$self->map->{$key} );
40 }
41  
42 8 ▶sub copy {
Subroutine copy: called CC=3 SCAR=11.0
Pod undocumented
43 8 my ( $self ) = @_;
44 8 my $copy = Zuzu::Value::Dict->new( map => {} );
45  
46 8 for my $key ( CORE::keys %{ $self->map } ) {
47 $copy->_store_key(
48 $key,
49 $self->_value_for_key($key),
50 10 ▶ $self->weak->{$key} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->{$key} ? : 1 9 10
51 );
52 }
53  
54 8 return $copy;
55 }
56  
57 66 ▶sub keys {
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
58 66 my ( $self ) = @_;
59  
60 66 my $set = Zuzu::Value::Set->new( items => [] );
61 66 $set->add( CORE::keys %{ $self->map } );
62  
63 66 return $set;
64 }
65  
66 1 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
Pod documented
67 1 my ( $self ) = @_;
68  
69 my @values = map {
70 3 $self->_value_for_key($_)
71 1 } sort CORE::keys %{ $self->map };
72  
73 1 return Zuzu::Value::Bag->new( items => \@values );
74 }
75  
76 0 ▶sub enumerate {
Subroutine enumerate: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ( $self ) = @_;
78  
79 my @pairs = map {
80 0 [ $_, $self->_value_for_key($_) ]
81 0 } sort CORE::keys %{ $self->map };
82  
83 0 return Zuzu::Value::Bag->new( items => \@pairs );
84 }
85  
86  
87 664 ▶sub contains_key {
Subroutine contains_key: called CC=2 SCAR=6.9
Pod documented
88 664 my ( $self, $key ) = @_;
89 664 my $map = $self->map;
90  
91 664 $key = _normalize_key($key);
92  
93 664 ▶ return exists $map->{ $key } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $map->{$key} ? : 161 503 664
94 }
95  
96 0 ▶sub contains {
Subroutine contains: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ( $self, $key ) = @_;
98  
99 0 return $self->contains_key($key);
100 }
101  
102 663 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
103 663 my ( $self, $key ) = @_;
104  
105 663 return $self->contains_key($key);
106 }
107  
108 33 ▶sub defined {
Subroutine defined: called CC=3 SCAR=11.0
Pod undocumented
109 33 my ( $self, $key ) = @_;
110 33 my $map = $self->map;
111  
112 33 $key = _normalize_key($key);
113  
114 33 ▶ return 0 if ! exists $map->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map->{$key} 30 3 33
115  
116 3 ▶ return defined $self->_value_for_key($key) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_value_for_key($key) ? : 1 2 3
117 }
118  
119 293 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
120 293 my ( $self, $key, $default ) = @_;
121 293 my $map = $self->map;
122  
123 293 $key = _normalize_key($key);
124  
125 293 ▶ return $self->_value_for_key($key) if exists $map->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if exists $map->{$key} 169 124 293
126  
127 124 return $default;
128 }
129  
130 0 ▶sub to_Array {
Subroutine to_Array: not called CC=1 SCAR=6.9
Pod documented
131 0 my ( $self ) = @_;
132 my @pairs = map {
133 0 [ $_, $self->_value_for_key($_) ]
134 0 } sort CORE::keys %{ $self->map };
135  
136 0 return Zuzu::Value::Array->new( items => \@pairs );
137 }
138  
139 0 ▶sub to_Iterator {
Subroutine to_Iterator: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my ( $self ) = @_;
141  
142 0 my @keys = sort CORE::keys %{ $self->map };
143 0 my $index = 0;
144  
145 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 ▶ die 'ExhaustedException' if $index >= @keys;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= @keys 0 0 0
147 0 my $value = $keys[$index];
148 0 $index++;
149  
150 0 return $value;
151 0 };
152 }
153  
154 27 ▶sub add {
Subroutine add: called CC=8 SCAR=22.3
Pod documented
155 27 my ( $self, @args ) = @_;
156  
157 27 ▶ if ( @args == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 2) 1 26 27
158 1 my ( $k, $v ) = @args;
159 1 $self->_store_key( $k, $v, 0 );
160  
161 1 return $self;
162 }
163 26 for my $pair ( @args ) {
164 434 ▶ if ( ref($pair) eq 'ARRAY' and @$pair >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pair eq "ARRAY" and @$pair >= 2) 433 1 434
Condition66%2/3
ref $pair eq "ARRAY" and @$pair >= 2
!ll&&!rl&&r
01433
MC/DC50%1/2
ref $pair eq "ARRAY" and @$pair >= 2
ref $pair eq "ARRAY" @$pair >= 2
Truth table66%2/3
A and B
A= ref $pair eq "ARRAY"B= @$pair >= 2
ABresult
0X0
100
111
165 433 my ( $k, $v ) = @$pair;
166 433 $self->_store_key( $k, $v, 0 );
167 433 next;
168 }
169 1 ▶ 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 1 1
Condition33%2/6
builtin::blessed($pair) and $pair->can("key") and $pair->can("value")
!ll&&!rl&&r
100
builtin::blessed($pair) and $pair->can("key")
!ll&&!rl&&r
100
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 table25%1/4
A and B and C
A= builtin::blessed($pair)B= $pair->can("key")C= $pair->can("value")
ABCresult
0XX0
10X0
1100
1111
170 0 $self->_store_key( $pair->key, $pair->value, 0 );
171 0 next;
172 }
173 }
174  
175 26 return $self;
176 }
177  
178 6614 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
179 6614 my ( $self, $key, $value ) = @_;
180  
181 6614 return $self->_store_key( $key, $value, 0 );
182 }
183  
184 1 ▶sub add_weak {
Subroutine add_weak: called CC=1 SCAR=0.0
Pod undocumented
185 1 my ( $self, $key, $value ) = @_;
186  
187 1 return $self->_store_key( $key, $value, 1 );
188 }
189  
190 5 ▶sub set_weak {
Subroutine set_weak: called CC=1 SCAR=0.0
Pod undocumented
191 5 my ( $self, $key, $value ) = @_;
192  
193 5 return $self->_store_key( $key, $value, 1 );
194 }
195  
196 1 ▶sub remove {
Subroutine remove: called CC=9 SCAR=33.4
Pod documented
197 1 my ( $self, $k_or_cb ) = @_;
198  
199 1 ▶ if ( ref($k_or_cb) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $k_or_cb eq "CODE") 0 1 1
200 0 my @keys = CORE::keys %{ $self->map };
201 0 for my $k ( @keys ) {
202 0 my $pair = [ $k, $self->_value_for_key($k) ];
203 0 ▶ if ( $k_or_cb->($pair) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$k_or_cb($pair)) 0 0 0
204 0 delete $self->map->{ $k };
205 0 delete $self->weak->{ $k };
206 }
207 }
208  
209 0 return $self;
210 }
211 1 ▶ 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 1 1
elsif (ref $k_or_cb and builtin::blessed($k_or_cb) and $k_or_cb->can('key')) { } 0 2 2
Condition33%1/3
ref $k_or_cb eq "ARRAY" and @$k_or_cb
!ll&&!rl&&r
100
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
212 0 $k_or_cb = $k_or_cb->[0];
213 }
214 ▶ 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
100
ref $k_or_cb and builtin::blessed($k_or_cb)
!ll&&!rl&&r
100
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
215 0 $k_or_cb = $k_or_cb->key;
216 }
217  
218 1 $k_or_cb = _normalize_key($k_or_cb);
219 1 delete $self->map->{ $k_or_cb };
220 1 delete $self->weak->{ $k_or_cb };
221  
222 1 return $self;
223 }
224  
225 1 ▶sub for_each_pair {
Subroutine for_each_pair: called CC=2 SCAR=6.9
Pod undocumented
226 1 my ( $self, $callback ) = @_;
227  
228 1 for my $k ( sort CORE::keys %{ $self->map } ) {
229 4 $callback->( [ $k, $self->_value_for_key($k) ] );
230 }
231  
232 1 return $self;
233 }
234  
235 1 ▶sub for_each_key {
Subroutine for_each_key: called CC=2 SCAR=6.9
Pod undocumented
236 1 my ( $self, $callback ) = @_;
237  
238 1 for my $k ( sort CORE::keys %{ $self->map } ) {
239 4 $callback->($k);
240 }
241  
242 1 return $self;
243 }
244  
245 1 ▶sub for_each_value {
Subroutine for_each_value: called CC=2 SCAR=6.9
Pod undocumented
246 1 my ( $self, $callback ) = @_;
247  
248 1 for my $k ( sort CORE::keys %{ $self->map } ) {
249 4 $callback->( $self->_value_for_key($k) );
250 }
251  
252 1 return $self;
253 }
254  
255 1 ▶sub kv {
Subroutine kv: called CC=2 SCAR=6.9
Pod undocumented
256 1 my ( $self ) = @_;
257 1 my @out;
258 1 for my $k ( sort CORE::keys %{ $self->map } ) {
259 4 push @out, $k, $self->_value_for_key($k);
260 }
261  
262 1 return Zuzu::Value::Array->new( items => \@out );
263 }
264  
265 4 ▶sub sorted_keys {
Subroutine sorted_keys: called CC=1 SCAR=0.0
Pod undocumented
266 4 my ( $self ) = @_;
267 4 my @keys = sort CORE::keys %{ $self->map };
268  
269 4 return Zuzu::Value::Array->new( items => \@keys );
270 }
271  
272 7 ▶sub length { scalar CORE::keys %{ $_[0]->map } }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
273  
274 0 ▶sub empty { scalar CORE::keys %{ $_[0]->map } ? 0 : 1 }
Subroutine empty: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar CORE::keys %{$_[0]->map;} ? : 0 0 0
275  
276 0 ▶sub count { $_[0]->length }
Subroutine count: not called CC=1 SCAR=6.9
Pod undocumented
277  
278 0 ▶sub is_empty { $_[0]->empty }
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod undocumented
279  
280 1 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
281 1 my ( $self ) = @_;
282 1 $self->map( {} );
283 1 $self->weak( {} );
284  
285 1 return $self;
286 }
287  
288 1 ▶sub is_truthy { scalar(CORE::keys %{$_[0]->map}) ? 1 : 0 }
Subroutine is_truthy: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar CORE::keys %{$_[0]->map;} ? : 0 1 1
289  
290 =pod
291  
292 =head1 NAME
293  
294 Zuzu::Value::Dict - runtime value class for dict values
295  
296 =head1 DESCRIPTION
297  
298 Wraps runtime dictionary/hash values and provides truthiness semantics for collections.
299  
300 =head1 INHERITANCE
301  
302 Inherits from C<Moo::Object>.
303  
304 =head1 ROLES
305  
306 None.
307  
308 =head1 ATTRIBUTES
309  
310 =head2 map
311  
312 Type: B<HashRef>.
313  
314 Dictionary storage map from keys to runtime values.
315  
316 =head1 METHODS
317  
318 =head2 new
319  
320 Constructs and returns a new instance of this class.
321  
322 =head2 is_truthy
323  
324 Returns this runtime value's truthiness in ZuzuScript.
325  
326 =head2 keys
327  
328 Returns dictionary keys as a set.
329  
330 =head2 values
331  
332 Returns dictionary values as a bag.
333  
334 =head2 contains_key
335  
336 Returns true when the provided key exists.
337  
338 =head2 get
339  
340 Returns the value for a key or an optional default value.
341  
342 =head2 to_Array
343  
344 Returns an array of C<[ key, value ]> pairs.
345  
346 =head2 add
347  
348 Adds one or more key-value pairs and returns this dictionary.
349  
350 =head2 remove
351  
352 Deletes one key and returns this dictionary.
353  
354 =head2 length
355  
356 Returns the number of keys.
357  
358 =head2 empty
359  
360 Returns true when the dictionary has no keys.
361  
362 =head2 clear
363  
364 Removes all keys and returns this dictionary.
365  
366 =head1 SEE ALSO
367  
368 Subclasses: none in this distribution.
369  
370 =head1 COPYRIGHT AND LICENCE
371  
372 B<< Zuzu::Value::Dict >> is copyright Toby Inkster.
373  
374 It is free software; you may redistribute it and/or modify it under
375 the terms of either the Artistic License 1.0 or the GNU General Public
376 License version 2.
377  
378 =cut
379  
380 69 1;
« blib/lib/Zuzu/Marshal/CBOR.pm ↑ Summary blib/lib/Zuzu/Web/PSGI/CLI.pm »