blib/lib/Data/Properties/JSON.pm

filter: Statementstmt 91.4% 32 / 35 Branchbran 62.5% 10 / 16 Conditioncond 75.0% 6 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 2 totaltotal 73.9% 54 / 73 CC 13 SCAR 18.0
CC
13
Cov
81%
new
9.2
as_hash
6.0
AUTOLOAD
4.2
CRAP 6.1
↑ Summary
1  
2 package Data::Properties::JSON;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = '0.004';
8  
9  
10 sub new
11 6 ▶{
Subroutine new: called CC=9 SCAR=22.2
Pod undocumented
12 6 my ($class, %args) = @_;
13
14 6 ▶ if( $args{properties_file} )
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'properties_file'}) { } 1 5 6
elsif ($args{'json'}) { } 1 4 5
elsif ($args{'data'}) { } 4 0 4
15 {
16 1 $args{__name} = 'root';
17 open my $ifh, '<', $args{properties_file}
18 1 ▶ or die "Cannot open '$args{properties_file}' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ifh, "<", $args{"properties_file"} or die qq[Cannot open '$args{"properties_file"}' for reading: $!] 1 0 1
19 1 local $/;
20 1 $args{data} = decode_json(scalar(<$ifh>));
21 }
22 elsif( $args{json} )
23 {
24 1 $args{data} = decode_json($args{json});
25 1 $args{__name} = 'root';
26 }
27 elsif( $args{data} )
28 {
29 4 ▶ $args{__name} ||= 'root';
Condition100%2/2
$args{"__name"} ||= "root"
l!l
31
MC/DC100%1/1
$args{"__name"} ||= "root"
$args{"__name"}
Truth table100%2/2
A
A= $args{"__name"}
Aresult
11
00
30 }
31 else
32 {
33 0 die "Neither properties_file nor json nor data were provided.";
34 }# end if()
35
36 6 my $s = bless \%args, $class;
37 6 ▶ if( ref($s->{data}) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->{"data"} eq "HASH") 6 0 6
38 {
39 6 foreach my $key ( keys(%{ $s->{data} }) )
40 {
41 15 ▶ next unless ref($s->{data}->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $s->{"data"}{$key} 12 3 15
42 $s->{$key} = ref($s)->new(
43 __name => "$s->{__name}.$key",
44 3 data => $s->{data}->{$key},
45 );
46 }# end foreach()
47 }# end if()
48
49 # Finally:
50 6 return $s;
51 }# end new()
52  
53  
54 sub as_hash
55 0 ▶{
Subroutine as_hash: not called CC=2 SCAR=17.9
Pod undocumented
56 0 ▶ wantarray ? %{ $_[0]->{data} } : $_[0]->{data};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
57 }# end as_hash()
58  
59  
60 sub AUTOLOAD
61 24 ▶{
Subroutine AUTOLOAD: called CC=4 SCAR=14.3
62 24 my $s = shift;
63 24 our $AUTOLOAD;
64
65 24 my ($name) = $AUTOLOAD =~ m/::([^:]+)$/;
66 die "Node $s->{__name} has no property named '$name'"
67 24 ▶ unless exists( $s->{$name} ) || exists( $s->{data}->{$name} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s->{$name} or exists $s->{"data"}{$name} 0 24 24
Condition66%2/3
exists $s->{$name} or exists $s->{"data"}{$name}
l!l&&r!l&&!r
12120
MC/DC0%0/2
exists $s->{$name} or exists $s->{"data"}{$name}
exists $s->{$name} exists $s->{"data"}{$name}
Truth table66%2/3
A or B
A= exists $s->{$name}B= exists $s->{"data"}{$name}
ABresult
1X1
011
000
68
69 24 ▶ return $s->{$name} || $s->{data}->{$name};
Condition66%2/3
$s->{$name} || $s->{'data'}{$name}
l!l&&r!l&&!r
12120
MC/DC0%0/2
$s->{$name} || $s->{'data'}{$name}
$s->{$name} $s->{'data'}{$name}
Truth table66%2/3
A || B
A= $s->{$name}B= $s->{'data'}{$name}
ABresult
1X1
011
000
70 }# end AUTOLOAD()
71  
72 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
73  
74 1 1;# return true:
75  
76 =pod
77  
78 =head1 NAME
79  
80 Data::Properties::JSON - JSON test fixtures and properties.
81  
82 =head1 DEPRECATED
83  
84 This module is now deprecated and should no longer be used.
85  
86 =head1 SYNOPSIS
87  
88 File C</path/to/file.json>
89  
90 {
91 "contact_form": {
92 "first_name": "John",
93 "last_name": "Doe",
94 "email": "john.doe@test.com",
95 "message": "This is a test message...just a test."
96 }
97 }
98  
99 Your Perl code:
100  
101 use Data::Properties::JSON;
102
103 my $props = Data::Properties::JSON->new(
104 properties_file => "/path/to/file.json"
105 );
106
107 -- or --
108 my $props = Data::Properties::JSON->new( json => $json_string );
109 -- or --
110 my $props = Data::Properties::JSON->new( data => { foo => "bar" } );
111
112 $props->contact_form->first_name; # John
113 $props->contact_form->last_name; # Doe
114 $props->contact_form->email; # john.doe@test.com
115
116 # Works differently depending on the calling context:
117 my %hash = $props->contact_form->as_hash;
118 my $hashref = $props->contact_form->as_hash;
119  
120 =head1 AUTHOR and Copyright
121  
122 Copyright 2011 John Drago <jdrago_999@yahoo.com> all rights reserved.
123  
124 =head1 LICENSE
125  
126 This software is Free software and may be used and redistributed under the same
127 terms as any version of perl itself.
128  
129 =head1 SEE ALSO
130  
131 L<JSON> and L<JSON::XS>
132  
133 L<ASP4>
134  
135 =cut
136  
↑ Summary