File Coverage

blib/lib/Dancer2/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 Dancer2::RPCPlugin::FlattenData;
2 20     20   53471 use warnings;
  20         38  
  20         577  
3 20     20   98 use strict;
  20         36  
  20         405  
4 20     20   82 use Scalar::Util 'blessed';
  20         39  
  20         785  
5              
6 20     20   91 use Exporter 'import';
  20         84  
  20         3407  
7             our @EXPORT = qw/ flatten_data /;
8              
9             sub flatten_data {
10 17     17 1 100 my $to_flatten = shift;
11              
12 17         24 my $ref_check = ref($to_flatten);
13 17 100       40 if (blessed($to_flatten)) {
14 5 100       39 $ref_check = $to_flatten->isa('HASH')
    100          
15             ? 'HASH'
16             : $to_flatten->isa('ARRAY')
17             ? 'ARRAY'
18             : 'SCALAR';
19             }
20              
21 17 100       72 if ($ref_check eq 'HASH') {
    100          
    100          
22             my $flat = {
23             map {
24 4         12 $_ => flatten_data($to_flatten->{$_})
  6         26  
25             } keys %$to_flatten
26             };
27 4         36 return $flat;
28             }
29             elsif ($ref_check eq 'ARRAY') {
30             my $flat = [
31 3         4 map { flatten_data($_) } @$to_flatten
  8         18  
32             ];
33 3         8 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             Dancer2::RPCPlugin::DataFlatten - Simple routine to flatten (blessed) data
48              
49             =head1 SYNOPSIS
50              
51             use Dancer2::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