blib/lib/Hash/DotPath.pm

filter: Statementstmt 95.5% 301 / 315 Branchbran 56.6% 102 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 97.8% 46 / 47 Podpod n/a totaltotal 82.8% 449 / 542 CC 91 SCAR 22.6
CC
91
Cov
81%
_exists
21.2
_buildPath
16.5
merge
15.4
CRAP 9.6
↑ Summary
1 package Hash::DotPath;
2 7 $Hash::DotPath::VERSION = '0.002';
3 7 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Kavorka 'method';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Util::Medley::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 with
12 'Util::Medley::Roles::Attributes::Hash',
13 'Util::Medley::Roles::Attributes::List',
14 'Util::Medley::Roles::Attributes::Logger',
15 'Util::Medley::Roles::Attributes::String';
16  
17 ########################################################
18  
19 =head1 NAME
20  
21 Hash::DotPath - Class for manipulating hashes via dot path notation.
22  
23 =head1 VERSION
24  
25 version 0.002
26  
27 =cut
28  
29 ########################################################
30  
31 =head1 SYNOPSIS
32  
33 $dot = Hash::DotPath->new;
34 $dot = Hash::DotPath->new(\%myhash);
35 $dot = Hash::DotPath->new(\%myhash, delimiter => '~');
36  
37 $val = $dot->get('foo.bar');
38 $val = $dot->get('biz.baz.0.zoo');
39  
40 $dot->set('foo', 'bar');
41 $dot->set('cats', []);
42 $dot->set('cats.0', 'calico');
43
44 $dot->delete('foo');
45
46 $newObj = $dot->merge({ biz => 'baz' });
47 $newObj = $dot->merge({ biz => 'other' }, 'RIGHT');
48  
49 %hash = $dot->toHash;
50 $href = $dot->toHashRef;
51
52 =cut
53  
54 =head1 ARRAYS
55  
56 You interact with array elements slightly differently than hash ones.
57  
58 To access an element in an array, use the numeric index in the path.
59  
60 $dot = Hash::DotPath->new({foo => ['a', 'b', 'c']});
61 $dot->get('foo.0');
62
63 To set a value on an existing array, the same notation works.
64
65 $dot->set('foo.3', 'd');
66  
67 To add a NEW array to the tree, you must first instantiate it. This is
68 because hash keys can be numeric. Since this is a hash class, the default
69 is to treat integers like hash keys unless a array already exists at the
70 given location.
71  
72 $dot = Hash::DotPath->new({ foo => 'bar'});
73
74 # WRONG - this is a non-existent array
75 $dot->set('biz.0', 'baz');
76
77 # CORRECT
78 $dot->set('biz', []);
79 $dot->set('biz.0', 'baz');
80
81 =cut
82  
83 ##############################################################################
84 # PUBLIC ATTRIBUTES
85 ##############################################################################
86  
87 =head1 ATTRIBUTES
88  
89 =cut
90  
91 # this attrib is used indirectly. therefore, it isn't documented.
92 has init => (
93 is => 'rw',
94 isa => 'HashRef',
95 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 7 );
97  
98 =head2 delimiter [Str] (optional)
99  
100 The delimiter to use when analyzing a dot path.
101  
102 Default: "."
103  
104 =cut
105  
106 7 has delimiter => (
107 is => 'rw',
108 isa => 'Str',
109 default => '.',
110 );
111  
112 ##############################################################################
113 # PRIVATE_ATTRIBUTES
114 ##############################################################################
115  
116 7 has _href => (
117 is => 'rw',
118 isa => 'HashRef',
119 );
120  
121 ##############################################################################
122 # CONSTRUCTOR
123 ##############################################################################
124  
125 around BUILDARGS => sub {
126  
127 10 ▶ my $orig = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
128 10 my $class = shift;
129  
130 10 my $href;
131 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 1 10
132  
133 # TODO: is there a way to use the 'Hash' attrib instead?
134 9 my $util = Util::Medley::Hash->new;
135 9 ▶ if ( $util->isHash( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($util->isHash($_[0])) 8 1 9
136 8 $href = shift @_;
137 }
138 }
139  
140 10 my %args = @_;
141 10 ▶ $args{init} = $href if $href;
Branch: 1 branch
BranchTrueFalseTotal
if $href 8 2 10
142  
143 10 return $class->$orig(%args);
144 7 };
145  
146 7 ▶method BUILD {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BUILD: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 10 0 10
147  
148 10 $self->_href( $self->init );
149 }
150  
151 ##############################################################################
152 # PUBLIC METHODS
153 ##############################################################################
154  
155 =head1 METHODS
156  
157 =head2 delete
158  
159 Deletes an element at the specified path. Returns the value of the element
160 that was deleted.
161  
162 =over
163  
164 =item usage:
165  
166 $val = $dot->delete('foo.bar');
167 $val = $dot->delete('biz.0.baz');
168  
169 =item args:
170  
171 =over
172  
173 =item path [Str]
174  
175 Dot-path of the element you wish to delete.
176  
177 =back
178  
179 =back
180  
181 =cut
182  
183 7 ▶method delete (Str $path!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine delete: called CC=9 SCAR=22.1
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 4 0 4
if @_ != 1 0 4 4
$#_ >= 0 ? : 4 0 4
if ($#_ >= 0) 4 0 4
Type::Tiny::XS::Str($path) or _failed_check(14, "Str", $path, "exception_class", "Error::TypeTiny::Assertion") 0 4 4
184  
185 4 my $ptr = $self->_href;
186 4 my @keys = $self->_splitKey($path);
187 4 my $lastKey = pop @keys;
188
189 4 ▶ if (@keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys) 3 1 4
190 3 $ptr = $self->_get( $self->_href, \@keys );
191 }
192
193 4 my $val;
194 4 ▶ if ( $self->List->isArray($ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->List->isArray($ptr)) { } 2 2 4
195 2 ▶ if ( $self->String->isInt($lastKey) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->String->isInt($lastKey)) { } 1 1 2
196 1 $val = splice(@$ptr, $lastKey, 1);
197 }
198 else {
199 1 confess "can't reference array index at $path by $lastKey";
200 }
201 }
202 else {
203 2 $val = $ptr->{$lastKey} ;
204 2 delete $ptr->{$lastKey};
205 }
206  
207 3 return $val; # --> Any
208 }
209  
210 =head2 get
211  
212 Gets an element at the specified path. Returns 'Any'.
213  
214 =over
215  
216 =item usage:
217  
218 $val = $dot->get('foo.bar');
219 $val = $dot->get('biz.0.baz');
220  
221 =item args:
222  
223 =over
224  
225 =item path [Str]
226  
227 Dot-path of the element you wish to get.
228  
229 =back
230  
231 =back
232  
233 =cut
234  
235 7 ▶method get (Str $path!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine get: called CC=6 SCAR=18.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 18 0 18
if @_ != 1 0 18 18
$#_ >= 0 ? : 18 0 18
if ($#_ >= 0) 18 0 18
Type::Tiny::XS::Str($path) or _failed_check(14, "Str", $path, "exception_class", "Error::TypeTiny::Assertion") 0 18 18
236  
237 18 my @keys = $self->_splitKey($path);
238  
239 18 return $self->_get( $self->_href, \@keys ); # --> Any
240 }
241  
242 =head2 exists
243  
244 Determines if an element exists at the given path. Returns 'Bool'.
245  
246 =over
247  
248 =item usage:
249  
250 $bool = $dot->exists('foo.bar');
251 $bool = $dot->exists('biz.0.baz');
252  
253 =item args:
254  
255 =over
256  
257 =item path [Str]
258  
259 Dot-path of the element you wish to get.
260  
261 =back
262  
263 =back
264  
265 =cut
266  
267 7 ▶method exists (Str $path!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine exists: called CC=6 SCAR=18.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 8 0 8
if @_ != 1 0 8 8
$#_ >= 0 ? : 8 0 8
if ($#_ >= 0) 8 0 8
Type::Tiny::XS::Str($path) or _failed_check(14, "Str", $path, "exception_class", "Error::TypeTiny::Assertion") 0 8 8
268  
269 8 my @keys = $self->_splitKey($path);
270  
271 8 return $self->_exists( $self->_href, \@keys ); # --> Bool
272 }
273  
274 =head2 merge
275  
276 Merges the provided dot-path object or hashref with the object. You indicate
277 which hash has precedence by providing the 'overwrite' arg.
278  
279 =over
280  
281 =item usage:
282  
283 $newDot = $dot->merge({foo => 'bar'}, [0|1]);
284  
285 $dot2 = Hash::DotPath->new(biz => 'baz');
286 $newDot = $dot->merge($dot2, [0|1]);
287
288 =item args:
289  
290 =over
291  
292 =item merge [HashRef|Hash::DotPath]
293  
294 Hashref you wish to merge into the dot-path object.
295  
296 =item overwrite [Bool] (optional)
297  
298 Indicates which hash has precedence over the other. A true value means
299 the element passed in will overwrite any pre-existing elements. A false value
300 will preserve existing elements and just merge the new ones in.
301  
302 Default: 1
303  
304 =back
305  
306 =back
307  
308 =cut
309  
310 method merge (Object|HashRef $merge!,
311 7 ▶ Bool $overwrite = 1) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine merge: called CC=12 SCAR=27.3
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 1 0 1
if @_ < 1 0 1 1
if @_ > 2 0 1 1
$#_ >= 0 ? : 1 0 1
if ($#_ >= 0) 1 0 1
Type::Tiny::XS::AUTO::TC1($merge) or _failed_check(45, "Object|HashRef", $merge, "exception_class", "Error::TypeTiny::Assertion") 0 1 1
$#_ >= 1 ? : 0 1 1
Type::Tiny::XS::Bool($overwrite) or _failed_check(10, "Bool", $overwrite, "exception_class", "Error::TypeTiny::Assertion") 0 1 1
312  
313 1 my $href;
314 1 ▶ if ( $self->Hash->isHash($merge) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Hash->isHash($merge)) { } 1 0 1
315 1 $href = $merge;
316 }
317 else {
318 0 my $ref = ref($merge);
319 0 ▶ if ($ref eq 'Hash::DotPath') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'Hash::DotPath') { } 0 0 0
320 0 $href = $merge->toHashRef;
321 }
322 else {
323 0 confess "can't use $ref as a hashref";
324 }
325 }
326  
327 1 my %args = (left => $self->_href, right => $href);
328 1 ▶ $args{precedent} = 'RIGHT' if $overwrite;
Branch: 1 branch
BranchTrueFalseTotal
if $overwrite 1 0 1
329 1 my $merged = $self->Hash->merge(%args);
330
331 1 return __PACKAGE__->new( $merged, delimiter => $self->delimiter );
332 }
333  
334 =head2 set
335  
336 Sets an element at the specified path. Returns the value that was passed in.
337  
338 =over
339  
340 =item usage:
341  
342 $val = $dot->set('foo.bar', 'abc');
343 $val = $dot->set('biz.0.baz', 'def');
344  
345 =item args:
346  
347 =over
348  
349 =item path [Str]
350  
351 Dot-path of the element you wish to set.
352  
353 =item value [Any]
354  
355 Value you wish to set at the given path.
356  
357 =back
358  
359 =back
360  
361 =cut
362  
363 method set (Str $path!,
364 7 ▶ Any $value!) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine set: called CC=10 SCAR=23.6
Branch: 7 branches
BranchTrueFalseTotal
@_ ? : 7 0 7
if @_ != 2 0 7 7
$#_ >= 0 ? : 7 0 7
if ($#_ >= 0) 7 0 7
Type::Tiny::XS::Str($path) or _failed_check(14, "Str", $path, "exception_class", "Error::TypeTiny::Assertion") 0 7 7
$#_ >= 1 ? : 7 0 7
if ($#_ >= 1) 7 0 7
365  
366 7 my @keys = $self->_splitKey($path);
367 7 my $lastKey = pop @keys;
368 7 my $parentPath = join $self->delimiter, @keys;
369  
370 7 my $ptr = $self->_buildPath( $self->_href, \@keys );
371 7 ▶ if ( $self->List->isArray($ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->List->isArray($ptr)) { } 1 6 7
372 1 ▶ if ( $self->String->isInt($lastKey) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->String->isInt($lastKey)) { } 0 1 1
373 0 $ptr->[$lastKey] = $value;
374 }
375 else {
376 1 confess "can't reference array index at $parentPath by $lastKey";
377 }
378 }
379 else {
380 6 $ptr->{$lastKey} = $value;
381 }
382  
383 6 return $value; # --> Any
384 }
385  
386 =head2 toHash
387  
388 Returns a hash version of the object.
389  
390 =over
391  
392 =item usage:
393  
394 %hash = $dot->toHash;
395  
396 =back
397  
398 =cut
399  
400 7 ▶method toHash {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine toHash: not called CC=2 SCAR=10.5
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
401  
402 0 return %{ $self->_href };
403 }
404  
405 =head2 toHashRef
406  
407 Returns a hashref version of the object.
408  
409 =over
410  
411 =item usage:
412  
413 $href = $dot->toHashRef;
414  
415 =back
416  
417 =cut
418  
419 7 ▶method toHashRef {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine toHashRef: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 10 0 10
420  
421 10 return $self->_href;
422 }
423  
424 ##############################################################################
425 # PRIVATE METHODS
426 ##############################################################################
427  
428 7 ▶method _splitKey (Str $key) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _splitKey: called CC=6 SCAR=18.3
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 37 0 37
if @_ != 1 0 37 37
$#_ >= 0 ? : 37 0 37
if ($#_ >= 0) 37 0 37
Type::Tiny::XS::Str($key) or _failed_check(14, "Str", $key, "exception_class", "Error::TypeTiny::Assertion") 0 37 37
429  
430 37 my $regex = sprintf '\%s', $self->delimiter;
431 37 my @split = split( /$regex/, $key );
432  
433 37 return @split;
434 }
435  
436 method _exists (HashRef $ptr,
437 7 ▶ ArrayRef $keys) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _exists: called CC=15 SCAR=30.5
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 8 0 8
if @_ != 2 0 8 8
$#_ >= 0 ? : 8 0 8
if ($#_ >= 0) 8 0 8
Ref::Util::XS::is_plain_hashref($ptr) or _failed_check(27, "HashRef", $ptr, "exception_class", "Error::TypeTiny::Assertion") 0 8 8
$#_ >= 1 ? : 8 0 8
if ($#_ >= 1) 8 0 8
Ref::Util::XS::is_plain_arrayref($keys) or _failed_check(26, "ArrayRef", $keys, "exception_class", "Error::TypeTiny::Assertion") 0 8 8
438  
439 8 my @remKeys = @$keys; # make a copy
440 8 my $currKey = shift @remKeys;
441  
442 8 ▶ if ( $self->List->isArray($ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->List->isArray($ptr)) { } 0 8 8
443  
444 0 ▶ if ( $self->String->isInt($currKey) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->String->isInt($currKey)) 0 0 0
445 0 ▶ if (@remKeys) {
Branch: 2 branches
BranchTrueFalseTotal
if (@remKeys) { } 0 0 0
elsif (exists $ptr->[$currKey]) { } 0 0 0
446 0 return $self->_get( $ptr->[$currKey], \@remKeys );
447 }
448 elsif ( exists $ptr->[$currKey] ) {
449 0 return 1;
450 }
451 }
452 }
453 else {
454  
455 8 ▶ if ( exists $ptr->{$currKey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ptr->{$currKey}) 7 1 8
456 7 ▶ if (@remKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remKeys) { } 6 1 7
457 6 return $self->_get( $ptr->{$currKey}, \@remKeys );
458 }
459 else {
460 1 return 1;
461 }
462 }
463 }
464  
465 1 return 0;
466 }
467  
468 method _get (HashRef|ArrayRef $ptr,
469 7 ▶ ArrayRef $keys) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _get: called CC=14 SCAR=26.7
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 58 0 58
if @_ != 2 0 58 58
$#_ >= 0 ? : 58 0 58
if ($#_ >= 0) 58 0 58
Type::Tiny::XS::AUTO::TC2($ptr) or _failed_check(46, "HashRef|ArrayRef", $ptr, "exception_class", "Error::TypeTiny::Assertion") 0 58 58
$#_ >= 1 ? : 58 0 58
if ($#_ >= 1) 58 0 58
Ref::Util::XS::is_plain_arrayref($keys) or _failed_check(26, "ArrayRef", $keys, "exception_class", "Error::TypeTiny::Assertion") 0 58 58
470  
471 58 my @remKeys = @$keys; # make a copy
472 58 my $currKey = shift @remKeys;
473  
474 58 ▶ if ( $self->List->isArray($ptr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->List->isArray($ptr)) { } 10 48 58
475  
476 10 ▶ if ( $self->String->isInt($currKey) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->String->isInt($currKey)) 8 2 10
477 8 ▶ if (@remKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remKeys) { } 3 5 8
478 3 return $self->_get( $ptr->[$currKey], \@remKeys );
479 }
480 else {
481 5 return $ptr->[$currKey];
482 }
483 }
484 }
485 else {
486  
487 48 ▶ if ( exists $ptr->{$currKey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ptr->{$currKey}) 45 3 48
488 45 ▶ if (@remKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remKeys) { } 28 17 45
489 28 return $self->_get( $ptr->{$currKey}, \@remKeys );
490 }
491 else {
492 17 return $ptr->{$currKey};
493 }
494 }
495 }
496  
497 5 return; # not found (undef)
498 }
499  
500 method _buildPath (HashRef|ArrayRef $ptr,
501 7 ▶ ArrayRef $keys) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _buildPath: called CC=15 SCAR=28.0
Branch: 8 branches
BranchTrueFalseTotal
@_ ? : 18 0 18
if @_ != 2 0 18 18
$#_ >= 0 ? : 18 0 18
if ($#_ >= 0) 18 0 18
Type::Tiny::XS::AUTO::TC2($ptr) or _failed_check(47, "HashRef|ArrayRef", $ptr, "exception_class", "Error::TypeTiny::Assertion") 0 18 18
$#_ >= 1 ? : 18 0 18
if ($#_ >= 1) 18 0 18
Ref::Util::XS::is_plain_arrayref($keys) or _failed_check(26, "ArrayRef", $keys, "exception_class", "Error::TypeTiny::Assertion") 0 18 18
502  
503 18 my @remKeys = @$keys;
504  
505 18 ▶ if (@remKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remKeys) 11 7 18
506 11 my $currKey = shift @remKeys;
507  
508 11 ▶ if ( $self->List->isArray($ptr) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->List->isArray($ptr)) { } 1 10 11
elsif ($self->Hash->isHash($ptr)) { } 10 0 10
509 1 ▶ if ( $self->String->isInt($currKey) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->String->isInt($currKey)) { } 1 0 1
510 1 ▶ if ( !exists $ptr->[$currKey] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ptr->[$currKey]) 1 0 1
511 1 $ptr->[$currKey] = {};
512 }
513  
514 1 return $self->_buildPath( $ptr->[$currKey], \@remKeys );
515 }
516 else {
517 0 confess "can't reference an array index by $currKey";
518 }
519 }
520 elsif ( $self->Hash->isHash($ptr) ) {
521 10 ▶ if ( !exists $ptr->{$currKey} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ptr->{$currKey}) 6 4 10
522 6 $ptr->{$currKey} = {};
523 }
524  
525 10 return $self->_buildPath( $ptr->{$currKey}, \@remKeys );
526 }
527 }
528  
529 7 return $ptr;
530 }
531  
532 7 __PACKAGE__->meta->make_immutable;
533  
534 7 1;
↑ Summary