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   135276 use warnings;
  8         32  
  8         284  
3 8     8   47 use strict;
  8         18  
  8         179  
4 8     8   42 use Scalar::Util 'blessed';
  8         14  
  8         392  
5              
6 8     8   139 use Exporter 'import';
  8         18  
  8         1465  
7             our @EXPORT = qw/ flatten_data /;
8              
9             sub flatten_data {
10 17     17 1 196 my $to_flatten = shift;
11              
12 17         38 my $ref_check = ref($to_flatten);
13 17 100       50 if (blessed($to_flatten)) {
14 5 100       46 $ref_check = $to_flatten->isa('HASH')
    100          
15             ? 'HASH'
16             : $to_flatten->isa('ARRAY')
17             ? 'ARRAY'
18             : 'SCALAR';
19             }
20              
21 17 100       64 if ($ref_check eq 'HASH') {
    100          
    100          
22             my $flat = {
23             map {
24 4         15 $_ => flatten_data($to_flatten->{$_})
  6         31  
25             } keys %$to_flatten
26             };
27 4         18 return $flat;
28             }
29             elsif ($ref_check eq 'ARRAY') {
30             my $flat = [
31 3         8 map { flatten_data($_) } @$to_flatten
  8         21  
32             ];
33 3         12 return $flat;
34             }
35             elsif ($ref_check eq 'SCALAR') {
36 1         4 return $$to_flatten;
37             }
38             else {
39 9         32 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