File Coverage

blib/lib/Data/Object/Role/Hash.pm
Criterion Covered Total %
statement 146 149 97.9
branch 40 56 71.4
condition 25 37 67.5
subroutine 35 35 100.0
pod 0 29 0.0
total 246 306 80.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Hash Object Role for Perl 5
2             package Data::Object::Role::Hash;
3              
4 44     44   317013 use 5.010;
  44         142  
5 44     44   15032 use Data::Object::Role;
  44         90  
  44         396  
6              
7 44     44   13797 use Data::Object 'codify';
  44         82  
  44         2824  
8 44     44   244 use Scalar::Util 'blessed';
  44         94  
  44         2379  
9 44     44   32770 use Storable 'dclone';
  44         141796  
  44         86103  
10              
11             map with($_), our @ROLES = qw(
12             Data::Object::Role::Defined
13             Data::Object::Role::Collection
14             Data::Object::Role::Detract
15             Data::Object::Role::Keyed
16             Data::Object::Role::Output
17             Data::Object::Role::Ref
18             Data::Object::Role::Values
19             Data::Object::Role::Type
20             );
21              
22             our $VERSION = '0.41'; # VERSION
23              
24             sub aslice {
25 1     1 0 3 goto &array_slice;
26             }
27              
28             sub array_slice {
29 2     2 0 4 my ($hash, @arguments) = @_;
30 2         4 return [@{$hash}{@arguments}];
  2         19  
31             }
32              
33             sub clear {
34 1     1 0 7 goto ∅
35             }
36              
37             sub defined {
38 1     1 0 2 my ($hash, $argument) = @_;
39 1         8 return CORE::defined $hash->{$argument};
40             }
41              
42             sub delete {
43 1     1 0 2 my ($hash, $argument) = @_;
44 1         10 return CORE::delete $hash->{$argument};
45             }
46              
47             sub each {
48 1     1 0 2 my ($hash, $code, @arguments) = @_;
49 1 50       5 $code = codify $code if !ref $code;
50              
51 1         9 for my $key (CORE::keys %$hash) {
52 4         16 $code->($key, $hash->{$key}, @arguments);
53             }
54              
55 1         5 return $hash;
56             }
57              
58             sub each_key {
59 1     1 0 2 my ($hash, $code, @arguments) = @_;
60              
61 1 50       5 $code = codify $code if !ref $code;
62 1         11 $code->($_, @arguments) for CORE::keys %$hash;
63              
64 1         10 return $hash;
65             }
66              
67             sub each_n_values {
68 1     1 0 3 my ($hash, $number, $code, @arguments) = @_;
69              
70 1 50       5 $code = codify $code if !ref $code;
71 1         7 my @values = CORE::values %$hash;
72 1         8 $code->(CORE::splice(@values, 0, $number), @arguments) while @values;
73              
74 1         10 return $hash;
75             }
76              
77             sub each_value {
78 1     1 0 2 my ($hash, $code, @arguments) = @_;
79              
80 1 50       4 $code = codify $code if !ref $code;
81 1         11 $code->($_, @arguments) for CORE::values %$hash;
82              
83 1         10 return $hash;
84             }
85              
86             sub empty {
87 2     2 0 3 my ($hash) = @_;
88 2         21 CORE::delete @$hash{CORE::keys %$hash};
89 2         7 return $hash;
90             }
91              
92             sub exists {
93 1     1 0 2 my ($hash, $key) = @_;
94 1         6 return CORE::exists $hash->{$key};
95             }
96              
97             sub filter_exclude {
98 1     1 0 2 my ($hash, @arguments) = @_;
99 1         2 my %i = map { $_ => $_ } @arguments;
  2         5  
100              
101             return {
102 2 50       7 CORE::map { CORE::exists $hash->{$_} ? ($_ => $hash->{$_}) : () }
103 1         7 CORE::grep { not CORE::exists $i{$_} } CORE::keys %$hash
  4         5  
104             };
105             }
106              
107             sub filter_include {
108 1     1 0 2 my ($hash, @arguments) = @_;
109              
110             return {
111 1 50       2 CORE::map { CORE::exists $hash->{$_} ? ($_ => $hash->{$_}) : () }
  2         13  
112             @arguments
113             };
114             }
115              
116             sub fold {
117 18     18 0 22 my ($data, $path, $store, $cache) = @_;
118              
119 18   100     38 $store ||= {};
120 18   100     29 $cache ||= {};
121              
122 18         23 my $ref = CORE::ref($data);
123 18         54 my $obj = Scalar::Util::blessed($data);
124 18         27 my $adr = Scalar::Util::refaddr($data);
125 18         38 my $tmp = { %$cache };
126              
127 18 50 66     178 if ($adr && $tmp->{$adr}) {
    100 100        
    100 66        
      66        
      66        
128 0         0 $store->{$path} = $data;
129             } elsif ($ref eq 'HASH' || ($obj and $obj->isa('Data::Object::Hash'))) {
130 5         10 $tmp->{$adr} = 1;
131 5 50       16 if (%$data) {
132 5         23 for my $key (CORE::sort(CORE::keys %$data)) {
133 11 100       24 my $place = $path ? CORE::join('.', $path, $key) : $key;
134 11         15 my $value = $data->{$key};
135 11         24 fold($value, $place, $store, $tmp);
136             }
137             } else {
138 0         0 $store->{$path} = {};
139             }
140             } elsif ($ref eq 'ARRAY' || ($obj and $obj->isa('Data::Object::Array'))) {
141 2         5 $tmp->{$adr} = 1;
142 2 50       8 if (@$data) {
143 2         8 for my $idx (0 .. $#$data) {
144 5 50       12 my $place = $path ? CORE::join(':', $path, $idx) : $idx;
145 5         9 my $value = $data->[$idx];
146 5         10 fold($value, $place, $store, $tmp);
147             }
148             } else {
149 0         0 $store->{$path} = [];
150             }
151             } else {
152 11         18 $store->{$path} = $data;
153             }
154              
155 18         39 return $store;
156             }
157              
158             sub get {
159 4     4 0 7 my ($hash, $argument) = @_;
160 4         22 return $hash->{$argument};
161             }
162              
163             sub hash_slice {
164 2     2 0 4 my ($hash, @arguments) = @_;
165 2         4 return {CORE::map { $_ => $hash->{$_} } @arguments};
  4         23  
166             }
167              
168             sub hslice {
169 1     1 0 3 goto &hash_slice;
170             }
171              
172             sub invert {
173 1     1 0 2 my ($hash) = @_;
174              
175 1         3 my $temp = {};
176 1         9 for (CORE::keys %$hash) {
177             CORE::defined $hash->{$_} ?
178             $temp->{CORE::delete $hash->{$_}} = $_ :
179 6 100       15 CORE::delete $hash->{$_};
180             }
181              
182 1         5 for (CORE::keys %$temp) {
183 5         6 $hash->{$_} = CORE::delete $temp->{$_};
184             }
185              
186 1         3 return $hash;
187             }
188              
189             sub iterator {
190 1     1 0 3 my ($hash) = @_;
191 1         3 my @keys = CORE::keys %{$hash};
  1         13  
192              
193 1         2 my $i = 0;
194             return sub {
195 5 100   5   32 return undef if $i > $#keys;
196 4         14 return $hash->{$keys[$i++]};
197             }
198 1         11 }
199              
200             sub keys {
201 2     2 0 6 my ($hash) = @_;
202 2         15 return [CORE::keys %$hash];
203             }
204              
205             sub lookup {
206 16     16 0 14 my ($hash, $path) = @_;
207              
208 16 50 33     137 return undef unless ($hash and $path) and (
      66        
      33        
209             ('HASH' eq ref($hash)) or blessed($hash) and $hash->isa('HASH')
210             );
211              
212 16 100       70 return $hash->{$path} if $hash->{$path};
213              
214 11         9 my $next;
215             my $rest;
216              
217 11         34 ($next, $rest) = $path =~ /(.*)\.([^\.]+)$/;
218 11 100 66     34 return lookup($hash->{$next}, $rest) if $next and $hash->{$next};
219              
220 8         23 ($next, $rest) = $path =~ /([^\.]+)\.(.*)$/;
221 8 50 66     52 return lookup($hash->{$next}, $rest) if $next and $hash->{$next};
222              
223 1         4 return undef;
224             }
225              
226             sub pairs {
227 1     1 0 6 goto &pairs_array;
228             }
229              
230             sub pairs_array {
231 2     2 0 3 my ($hash) = @_;
232 2         15 return [CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash];
  8         16  
233             }
234              
235             sub merge {
236 4     4 0 8 my ($left, @arguments) = @_;
237              
238 4 50       11 return dclone $left if ! @arguments;
239 4 50       10 return dclone merge($left, merge(@arguments)) if @arguments > 1;
240              
241 4         6 my ($right) = @arguments;
242 4         18 my (%merge) = %$left;
243              
244 4         11 for my $key (CORE::keys %$right) {
245 5         7 my $lprop = $$left{$key};
246 5         5 my $rprop = $$right{$key};
247              
248             $merge{$key} = ((ref($rprop) eq 'HASH') and (ref($lprop) eq 'HASH'))
249 5 50 66     23 ? merge($$left{$key}, $$right{$key}) : $$right{$key};
250             }
251              
252 4         155 return dclone \%merge;
253             }
254              
255             sub reset {
256 1     1 0 1 my ($hash) = @_;
257 1         7 @$hash{CORE::keys %$hash} = ();
258 1         3 return $hash;
259             }
260              
261             sub reverse {
262 1     1 0 2 my ($hash) = @_;
263              
264 1         3 my $temp = {};
265 1         11 for (CORE::keys %$hash) {
266 5 100       15 $temp->{$_} = $hash->{$_} if CORE::defined $hash->{$_};
267             }
268              
269 1         13 return {CORE::reverse %$temp};
270             }
271              
272             sub set {
273 1     1 0 2 my ($hash, $key, $argument) = @_;
274 1         8 return $hash->{$key} = $argument;
275             }
276              
277             sub unfold {
278 2     2 0 2 my ($hash) = @_;
279              
280 2         3 my $store = {};
281 2         15 for my $key (CORE::sort(CORE::keys(%$hash))) {
282 11         8 my $node = $store;
283 11         16 my @steps = CORE::split(/\./, $key);
284 11         20 for (my $i=0; $i < @steps; $i++) {
285 18         12 my $last = $i == $#steps;
286 18         16 my $step = $steps[$i];
287 18 100       35 if (my @parts = $step =~ /^(\w*):(0|[1-9]\d*)$/) {
288             $node = $node->{$parts[0]}[$parts[1]] = $last
289             ? $hash->{$key}
290             : exists $node->{$parts[0]}[$parts[1]]
291 5 50       27 ? $node->{$parts[0]}[$parts[1]]
    100          
292             : {};
293             } else {
294             $node = $node->{$step} = $last
295             ? $hash->{$key}
296             : exists $node->{$step}
297 13 100       42 ? $node->{$step}
    100          
298             : {};
299             }
300             }
301             }
302              
303 2         4 return $store;
304             }
305              
306             sub values {
307 3     3 0 5 my ($hash) = @_;
308 3         30 return [CORE::values %$hash];
309             }
310              
311             1;
312              
313             __END__