File Coverage

lib/Data/Hash/DotNotation.pm
Criterion Covered Total %
statement 57 62 91.9
branch 10 14 71.4
condition 6 11 54.5
subroutine 10 10 100.0
pod 0 3 0.0
total 83 100 83.0


line stmt bran cond sub pod time code
1             package Data::Hash::DotNotation;
2 2     2   1948 use strict; use warnings;
  2     2   4  
  2         71  
  2         8  
  2         3  
  2         98  
3              
4             our $VERSION = '1.01';
5              
6 2     2   1330 use Moose;
  2         815827  
  2         12  
7 2     2   10775 use Carp;
  2         4  
  2         978  
8              
9             =head1 NAME
10              
11             Data::Hash::DotNotation - Convenient representation for nested Hash structures
12              
13             =head1 VERSION
14              
15             1.01
16              
17             =head1 SYNOPSYS
18              
19             use Data::Hash::DotNotation;
20              
21             my $dn = Data::Hash::DotNotation->new({
22             name => 'Gurgeh',
23             planet => 'earth',
24             score => {
25             contact => 10,
26             scrabble => 20,
27             },
28             });
29              
30             print $dn->get('score.contact');
31              
32             =cut
33              
34             has 'data' => (
35             is => 'rw',
36             default => sub { {}; },
37             );
38              
39             sub get {
40 4     4 0 24 my $self = shift;
41 4 100       16 my $name = shift or croak "No name given";
42 3         4 return $self->_get($name);
43             }
44              
45             sub set {
46 5     5 0 1466 my $self = shift;
47 5 100       31 my $name = shift or croak 'No name given';
48 4         5 my $value = shift;
49              
50 4         8 $self->_set($name, $value);
51              
52 4         28 return $value;
53             }
54              
55             sub key_exists {
56 2     2 0 275 my $self = shift;
57 2         1 my $name = shift;
58 2         49 my $data = $self->data;
59              
60 2         6 my @parts = split(/\./, $name);
61 2         3 my $node = pop @parts;
62 2         2 my $parent_node;
63              
64 2   33     11 while ($data and (my $section = shift @parts)) {
65 0 0       0 if (ref $data->{$section} eq 'HASH') {
66 0         0 $data = $data->{$section};
67             } else {
68 0         0 return;
69             }
70             }
71              
72 2         8 return exists $data->{$node};
73             }
74              
75             sub _get {
76 3     3   4 my $self = shift;
77 3         3 my $name = shift;
78 3         72 my $data = $self->data;
79              
80 3         9 my @parts = split(/\./, $name);
81 3         3 my $node = pop @parts;
82 3         4 my $parent_node;
83              
84 3   66     14 while ($data and (my $section = shift @parts)) {
85 2 50       5 if (ref $data->{$section} eq 'HASH') {
86 2         8 $data = $data->{$section};
87             } else {
88 0         0 return;
89             }
90             }
91              
92 3 100 66     11 if ($data and exists $data->{$node}) {
93 2         8 return $data->{$node};
94             }
95              
96 1         5 return;
97             }
98              
99             sub _set {
100 4     4   3 my $self = shift;
101 4         5 my $name = shift;
102 4         3 my $value = shift;
103              
104 4 50       99 unless ($self->data) {
105 0         0 $self->data({});
106             }
107              
108 4         13 my @tarts = split(/\./, $name);
109 4         6 my $node = pop @tarts;
110              
111 4         68 my $current_location = $self->data;
112 4         6 foreach my $section (@tarts) {
113 2   50     9 $current_location->{$section} //= {};
114 2         4 $current_location = $current_location->{$section};
115             }
116              
117 4 100       8 if (defined($value)) {
118 3         6 $current_location->{$node} = $value;
119             } else {
120 1         2 delete $current_location->{$node};
121             }
122              
123 4         67 return $self->data;
124             }
125              
126 2     2   9 no Moose;
  2         3  
  2         11  
127             __PACKAGE__->meta->make_immutable;
128              
129             =head1 DEPENDENCIES
130              
131             =over 4
132              
133             =item L<Moose>
134              
135             =back
136              
137             =head1 SOURCE CODE
138              
139             L<GitHub|https://github.com/binary-com/perl-Data-Hash-DotNotation>
140              
141             =head1 AUTHOR
142              
143             binary.com, C<< <perl at binary.com> >>
144              
145             =head1 BUGS
146              
147             Please report any bugs or feature requests to
148             C<bug-moosex-role-registry at rt.cpan.org>, or through the web
149             interface at
150             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Hash-DotNotation>.
151             We will be notified, and then you'll automatically be notified of progress on
152             your bug as we make changes.
153              
154             =head1 SUPPORT
155              
156             You can find documentation for this module with the perldoc command.
157              
158             perldoc Data::Hash::DotNotation
159              
160             You can also look for information at:
161              
162             =over 4
163              
164             =item * RT: CPAN's request tracker (report bugs here)
165              
166             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Hash-DotNotation>
167              
168             =item * AnnoCPAN: Annotated CPAN documentation
169              
170             L<http://annocpan.org/dist/Data-Hash-DotNotation>
171              
172             =item * CPAN Ratings
173              
174             L<http://cpanratings.perl.org/d/Data-Hash-DotNotation>
175              
176             =item * Search CPAN
177              
178             L<http://search.cpan.org/dist/Data-Hash-DotNotation/>
179              
180             =back
181              
182             =head1 LICENSE AND COPYRIGHT
183              
184             Copyright (C) 2015 binary.com
185              
186             This program is free software; you can redistribute it and/or modify it
187             under the terms of the the Artistic License (2.0). You may obtain a
188             copy of the full license at:
189              
190             L<http://www.perlfoundation.org/artistic_license_2_0>
191              
192             Any use, modification, and distribution of the Standard or Modified
193             Versions is governed by this Artistic License. By using, modifying or
194             distributing the Package, you accept this license. Do not use, modify,
195             or distribute the Package, if you do not accept this license.
196              
197             If your Modified Version has been derived from a Modified Version made
198             by someone other than you, you are nevertheless required to ensure that
199             your Modified Version complies with the requirements of this license.
200              
201             This license does not grant you the right to use any trademark, service
202             mark, tradename, or logo of the Copyright Holder.
203              
204             This license includes the non-exclusive, worldwide, free-of-charge
205             patent license to make, have made, use, offer to sell, sell, import and
206             otherwise transfer the Package with respect to any patent claims
207             licensable by the Copyright Holder that are necessarily infringed by the
208             Package. If you institute patent litigation (including a cross-claim or
209             counterclaim) against any party alleging that the Package constitutes
210             direct or contributory patent infringement, then this Artistic License
211             to you shall terminate on the date that such litigation is filed.
212              
213             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
214             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
215             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
216             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
217             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
218             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
219             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
220             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
221              
222             =cut
223              
224             1;
225