File Coverage

blib/lib/Dancer/RPCPlugin/FlattenData.pm
Criterion Covered Total %
statement 25 25 100.0
branch 12 12 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Dancer::RPCPlugin::FlattenData;
2 8     8   88230 use warnings;
  8         28  
  8         245  
3 8     8   41 use strict;
  8         17  
  8         168  
4 8     8   44 use Scalar::Util 'blessed';
  8         20  
  8         361  
5              
6 8     8   147 use Exporter 'import';
  8         21  
  8         1527  
7             our @EXPORT = qw/ flatten_data /;
8              
9             sub flatten_data {
10 17     17 1 141 my $to_flatten = shift;
11              
12 17         26 my $ref_check = ref($to_flatten);
13 17 100       39 if (blessed($to_flatten)) {
14 5 100       30 $ref_check = $to_flatten->isa('HASH')
    100          
15             ? 'HASH'
16             : $to_flatten->isa('ARRAY')
17             ? 'ARRAY'
18             : 'SCALAR';
19             }
20              
21 17 100       51 if ($ref_check eq 'HASH') {
    100          
    100          
22             my $flat = {
23             map {
24 4         12 $_ => flatten_data($to_flatten->{$_})
  6         19  
25             } keys %$to_flatten
26             };
27 4         16 return $flat;
28             }
29             elsif ($ref_check eq 'ARRAY') {
30             my $flat = [
31 3         4 map { flatten_data($_) } @$to_flatten
  8         15  
32             ];
33 3         7 return $flat;
34             }
35             elsif ($ref_check eq 'SCALAR') {
36 1         3 return $$to_flatten;
37             }
38             else {
39 9         22 return $to_flatten;
40             }
41             }
42              
43             1;
44              
45             =head1 NAME
46              
47             Dancer::RPCPlugin::DataFlatten - Simple routine to flatten (blessed) data
48              
49             =head1 SYNOPSIS
50              
51             use Dancer::RPCPlugin::DataFlatten;
52             my $data = bless({some => 'data'}, 'AnyClass');
53             my $flat = flatten_data($data); # {some => 'data'}
54              
55             =head1 DESCRIPTION
56              
57             =head2 flatten_data($any_data)
58              
59             This makes a deep-copy of the datastructure presented.
60              
61             =head3 Arguments
62              
63             Only the first argument is considered.
64              
65             =head3 Response
66              
67             A deep copy of the data structure presented.
68              
69             =head1 COPYRIGHT
70              
71             (c) MMXVII - Abe Timmerman <abeltje@cpan.org>.
72              
73             =cut