Skeleton.pm

filter: Statementstmt 100% 79 / 79 Branchbran 94.1% 32 / 34 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 97.1% 134 / 138 CC 22 SCAR 20.1
CC
22
Cov
98%
_blank_hash
12.0
_blank_array
7.0
deflesh
5.0
CRAP 7.5
« Delete.pm ↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::Skeleton;
4 3 $Data::Skeleton::VERSION = '0.07';
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use MooX::Types::MooseLike::Base qw/Str HashRef Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Data::Skeleton - Show the keys of a deep data structure
12  
13 =head1 SYNOPSIS
14  
15 use Data::Skeleton;
16 my $ds = Data::Skeleton->new;
17 my $deep_data_structure = {
18 id => 'hablando',
19 last_modified => 1,
20 sections => [
21 {
22 content => 'h1. Ice Cream',
23 class => 'textile'
24 },
25 {
26 content => '# Chocolate',
27 class => 'markdown'
28 },
29 ],
30 };
31 use Data::Dumper::Concise;
32 print Dumper $ds->deflesh($deep_data_structure);
33  
34 # results in:
35  
36 {
37 id => "",
38 last_modified => "",
39 sections => [
40 {
41 class => "",
42 content => ""
43 },
44 {
45 class => "",
46 content => ""
47 }
48 ]
49 }
50  
51 =head1 DESCRIPTION
52  
53 Sometimes you just want to see the "schema" of a data structure.
54 This modules shows only the keys with blanks for the values.
55  
56 =cut
57  
58 has 'value_marker' => (
59 is => 'ro',
60 isa => Str,
61 lazy => 1,
62 1 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 3 );
64 3 has 'references_seen' => (
65 is => 'rw',
66 isa => HashRef,
67 );
68  
69 =head2 debug_skeleton
70  
71 Turn on/off debugging
72  
73 =cut
74  
75 3 has 'debug_skeleton' => (
76 is => 'ro',
77 isa => Bool,
78 );
79  
80 =head1 METHODS
81  
82 =head2 deflesh
83  
84 Signature: (HashRef|ArrayRef)
85 Returns: The data structure with values blanked
86  
87 =cut
88  
89 6 ▶sub deflesh {
Subroutine deflesh: called CC=5 SCAR=16.1
Pod documented
90 6 my ($self, $data) = @_;
91 6 ▶ if (ref($data) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 2 4 6
elsif (ref $data eq 'ARRAY') { } 1 3 4
elsif (builtin::blessed($data) and eval { do { keys %{$data;}; 1 } }) { } 2 1 3
92 2 return $self->_blank_hash($data);
93 } elsif (ref($data) eq 'ARRAY') {
94 1 return $self->_blank_array($data);
95 3 ▶ } elsif (blessed($data) && eval { keys %{$data}; 1; } ) {
Condition66%2/3
builtin::blessed($data) and eval { do { keys %{$data;}; 1 } }
!ll&&!rl&&r
012
MC/DC50%1/2
builtin::blessed($data) and eval { do { keys %{$data;}; 1 } }
builtin::blessed($data) eval { do { keys %{$data;}; 1 } }
Truth table66%2/3
A and B
A= builtin::blessed($data)B= eval { do { keys %{$data;}; 1 } }
ABresult
0X0
100
111
96 2 return $self->_blank_hash($data);
97 } else {
98 1 die "You must pass the deflesh method one of:
99 HashRef
100 ArrayRef
101 Object that is a blessed HashRef
102 ";
103 }
104 }
105  
106 14 ▶sub _blank_hash {
Subroutine _blank_hash: called CC=12 SCAR=24.8
107 14 my ($self, $hashref) = @_;
108 # Work on a copy
109 14 my %hashref = %{$hashref};
110 14 $hashref = \%hashref;
111  
112 14 foreach my $key (keys %{$hashref}) {
113 29 my $value = $hashref->{$key};
114 29 my $ref_value = ref($value);
115 29 my $references_seen = $self->references_seen;
116 # Skip if we've seen this ref before
117 29 ▶ if ($ref_value and $references_seen->{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_value and $references_seen->{$value}) 1 28 29
Condition100%3/3
$ref_value and $references_seen->{$value}
!ll&&!rl&&r
12161
MC/DC100%2/2
$ref_value and $references_seen->{$value}
$ref_value $references_seen->{$value}
Truth table100%3/3
A and B
A= $ref_valueB= $references_seen->{$value}
ABresult
0X0
100
111
118 1 ▶ warn "Seen referenced value: $value before" if $self->debug_skeleton;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_skeleton 0 1 1
119 1 next;
120 }
121 # If we have a reference value then note it to avoid deep recursion
122 # with circular references.
123 28 ▶ if ($ref_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_value) 16 12 28
124 16 $references_seen->{$value} = 1;
125 16 $self->references_seen($references_seen);
126 }
127 28 ▶ if (!$ref_value) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $ref_value) { } 12 16 28
elsif ($ref_value eq 'SCALAR') { } 1 15 16
elsif ($ref_value eq 'HASH') { } 7 8 15
elsif ($ref_value eq 'ARRAY') { } 5 3 8
128 # blank a value that is not a reference
129 12 $hashref->{$key} = $self->value_marker;
130 }
131 elsif ($ref_value eq 'SCALAR') {
132 1 $hashref->{$key} = $self->value_marker;
133 }
134 elsif ($ref_value eq 'HASH') {
135 # recurse when a value is a HashRef
136 7 $hashref->{$key} = $self->_blank_hash($value);
137 }
138  
139 # look inside ArrayRefs for HashRefs
140 elsif ($ref_value eq 'ARRAY') {
141 5 $hashref->{$key} = $self->_blank_array($value);
142 }
143 else {
144 3 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) { } 2 1 3
145 # Hash based objects have keys
146 2 ▶ if (eval { keys %{$value}; 1; }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { keys %{$value;}; 1 } }) { } 1 1 2
147 1 my $blanked_hash_object = $self->_blank_hash($value); #[keys %{$value}];
148 # Note that we have an object
149 # WARNING: we are altering the data structure by adding a key
150 1 $blanked_hash_object->{BLESSED_AS} = $ref_value;
151 1 $hashref->{$key} = $blanked_hash_object;
152 } else {
153 1 $hashref->{$key} = $ref_value . ' object';
154 }
155 }
156 else {
157 # To leave value or to nuke it in this case? Leave for now.
158 }
159 }
160 }
161 14 return $hashref;
162 }
163  
164 7 ▶sub _blank_array {
Subroutine _blank_array: called CC=7 SCAR=19.5
165 7 my ($self, $arrayref) = @_;
166  
167 7 my $references_seen = $self->references_seen;
168 my @ref_values =
169 7 ▶ grep { ref($_) eq 'HASH' or ref($_) eq 'ARRAY' } @{$arrayref};
Condition100%3/3
ref $_ eq 'HASH' || ref $_ eq 'ARRAY'
l!l&&r!l&&!r
224
MC/DC100%2/2
ref $_ eq 'HASH' || ref $_ eq 'ARRAY'
ref $_ eq 'HASH' ref $_ eq 'ARRAY'
Truth table100%3/3
A || B
A= ref $_ eq 'HASH'B= ref $_ eq 'ARRAY'
ABresult
1X1
011
000
170 # if no array values are a reference to either a HASH or an ARRAY then we return an empty array reference
171 7 ▶ if (!scalar @ref_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar @ref_values) { } 3 4 7
172 3 $arrayref = [];
173 }
174 else {
175 $arrayref = [
176 map {
177 5 ▶ if (ref($_) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 2 3 5
elsif (ref $_ eq 'ARRAY') { } 2 1 3
178 2 $self->_blank_hash($_);
179 }
180 elsif (ref($_) eq 'ARRAY') {
181 # Skip if we've seen this ref before
182 2 ▶ if ($references_seen->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($references_seen->{$_}) 1 1 2
183 1 ▶ warn "Seen referenced value: $_ before" if $self->debug_skeleton;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_skeleton 0 1 1
184 1 return $_;
185 }
186 1 $references_seen->{$_} = 1;
187 1 $self->references_seen($references_seen);
188 1 $self->_blank_array($_);
189 }
190 else {
191 1 $self->value_marker;
192 }
193 4 } @{$arrayref}
194 ];
195 }
196 6 return $arrayref;
197 }
198  
199 3 1;
200  
201 =head1 AUTHORS
202  
203 Mateu Hunter C<hunter@missoula.org>
204  
205 =head1 COPYRIGHT
206  
207 Copyright 2011-2012, Mateu Hunter
208  
209 =head1 LICENSE
210  
211 You may distribute this code under the same terms as Perl itself.
212  
213 =cut
« Delete.pm ↑ Summary