blib/lib/Package/JSONable.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 93.3% 28 / 30 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 94.0% 111 / 118 CC 20 SCAR 26.5
CC
20
Cov
95%
__ANON__
20.1
import
1.0
BEGIN
1.0
CRAP 14.2
↑ Summary
1 # ABSTRACT: Add TO_JSON to your packages without the boilerplate
2 package Package::JSONable;
3 {
4 4 $Package::JSONable::VERSION = '0.001';
5 }
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
15 8 my ( $class, %import_opts ) = @_;
16  
17 8 my ( $target ) = caller;
18
19 7 ▶ my $to_json = sub {
Subroutine __ANON__: called CC=20 SCAR=30.0
20 7 my ( $self, %opts ) = @_;
21  
22 7 ▶ $self = $target unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 5 2 7
23
24 7 ▶ %opts = %import_opts unless %opts;
Branch: 1 branch
BranchTrueFalseTotal
unless %opts 6 1 7
25
26 7 my @types = qw/Str Int Num Bool ArrayRef HashRef/;
27  
28 7 my %hash;
29 7 foreach my $method ( keys %opts ) {
30 25 my $type = $opts{$method};
31 25 my @value = $self->$method;
32 24 my ( $value ) = @value;
33 24 my $reftype = reftype $value;
34 24 my $typetype = reftype $type;
35  
36 24 ▶ if ($typetype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($typetype) 4 20 24
37 4 ▶ croak sprintf('Invalid type: "%s"', $typetype)
Branch: 1 branch
BranchTrueFalseTotal
if $typetype ne "CODE" 1 3 4
38 if $typetype ne 'CODE';
39
40 3 $hash{$method} = $type->($self, @value);
41 3 next;
42 }
43  
44 croak sprintf('Invalid type: "%s"', $type)
45 20 ▶ if none { /^$type$/ } @types;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { /^$type$/; } , @types) 1 19 20
46
47 19 ▶ if (!defined $value && $type ne 'Bool') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $type ne "Bool") 2 17 19
Condition100%3/3
not defined $value and $type ne "Bool"
!ll&&!rl&&r
1522
MC/DC100%2/2
not defined $value and $type ne "Bool"
not defined $value $type ne "Bool"
Truth table100%3/3
not(A) and B
A= not defined $valueB= $type ne "Bool"
ABresult
0X0
100
111
48 2 $hash{$method} = $value;
49 2 next;
50 }
51  
52 17 ▶ if ( $type eq 'Str' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'Str') { } 3 14 17
elsif ($type eq 'Int') { } 2 12 14
elsif ($type eq 'Num') { } 2 10 12
elsif ($type eq 'ArrayRef') { } 4 6 10
elsif ($type eq 'HashRef') { } 4 2 6
elsif ($type eq 'Bool') { } 2 0 2
53 3 $hash{$method} = $value . "";
54 }
55 elsif ( $type eq 'Int' ) {
56 2 $hash{$method} = int $value;
57 }
58 elsif ( $type eq 'Num' ) {
59 2 $hash{$method} = $value += 0;
60 }
61 elsif ( $type eq 'ArrayRef' ) {
62 4 ▶ if ($reftype && $reftype eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype and $reftype eq 'ARRAY') { } 2 2 4
Condition66%2/3
$reftype and $reftype eq "ARRAY"
!ll&&!rl&&r
202
MC/DC50%1/2
$reftype and $reftype eq "ARRAY"
$reftype $reftype eq "ARRAY"
Truth table66%2/3
A and B
A= $reftypeB= $reftype eq "ARRAY"
ABresult
0X0
100
111
63 2 $hash{$method} = $value;
64 }
65 else {
66 2 $hash{$method} = [ @value ];
67 }
68 }
69 elsif ( $type eq 'HashRef' ) {
70 4 ▶ if ($reftype && $reftype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype and $reftype eq 'HASH') { } 2 2 4
Condition66%2/3
$reftype and $reftype eq "HASH"
!ll&&!rl&&r
202
MC/DC50%1/2
$reftype and $reftype eq "HASH"
$reftype $reftype eq "HASH"
Truth table66%2/3
A and B
A= $reftypeB= $reftype eq "HASH"
ABresult
0X0
100
111
71 2 $hash{$method} = $value;
72 }
73 else {
74 2 $hash{$method} = { @value };
75 }
76 }
77 elsif ( $type eq 'Bool' ) {
78 2 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 2 2
79 0 $hash{$method} = JSON::true;
80 }
81 else {
82 2 $hash{$method} = JSON::false;
83 }
84 }
85 }
86  
87 4 return \%hash;
88 8 };
89
90 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 8 *{"${target}::TO_JSON"} = $to_json;
92 }
93  
94 4 1;
95  
96 __END__
↑ Summary