EntityModel/Definition/JSON.pm

filter: Statementstmt 50.0% 19 / 38 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 100% 5 / 5 totaltotal 46.6% 28 / 60 CC 6 SCAR 23.2
CC
6
Cov
40%
save_file
20.0
load_file
12.0
save_string
2.0
CRAP 10.2
« EntityModel/Resolver.pm ↑ Summary Test/EntityModel.pm »
1 package EntityModel::Definition::JSON;
2 {
3 5 $EntityModel::Definition::JSON::VERSION = '0.102';
4 }
5 5 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 _isa => [qw{EntityModel::Definition}],
7 5 };
8  
9 =head1 NAME
10  
11 EntityModel::Definition::JSON - definition support for L<EntityModel>
12  
13 =head1 VERSION
14  
15 version 0.102
16  
17 =head1 SYNOPSIS
18  
19 See L<EntityModel>.
20  
21 =head1 DESCRIPTION
22  
23 See L<EntityModel>.
24  
25 =head1 METHODS
26  
27 =cut
28  
29 5 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head2 load_file
32  
33 =cut
34  
35 0 ▶sub load_file {
Subroutine load_file: not called CC=3 SCAR=24.8
Pod documented
36 0 my $self = shift;
37 0 my $path = shift;
38  
39 0 ▶ open my $fh, '<:encoding(utf-8)', $path or die "Failed to open $path - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf-8)", $path or die "Failed to open $path - $!" 0 0 0
40 0 my $string = do { local $/; <$fh> };
41 0 ▶ close $fh or die "Failed to close $path - $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close $path - $!" 0 0 0
42 0 return $self->parse($string);
43 }
44  
45 =head2 load_string
46  
47 =cut
48  
49 2 ▶sub load_string {
Subroutine load_string: called CC=1 SCAR=0.0
Pod documented
50 2 my $self = shift;
51 2 my $string = shift;
52 2 logDebug("Load string [%s]", $string);
53 2 return $self->parse($string);
54 }
55  
56 =head2 save_file
57  
58 Write output to a file.
59  
60 =cut
61  
62 0 ▶sub save_file {
Subroutine save_file: not called CC=4 SCAR=30.0
Pod documented
63 0 my $self = shift;
64 0 my %args = @_;
65 0 ▶ my $path = delete $args{path} or die "No path provided";
Branch: 1 branch
BranchTrueFalseTotal
my $path = delete $args{"path"} or die "No path provided" 0 0 0
66  
67 0 my $data = JSON::XS->new->encode($args{structure});
68 0 ▶ open my $fh, '>:encoding(utf-8)', $path or die "Failed to open $path - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf-8)", $path or die "Failed to open $path - $!" 0 0 0
69 0 $fh->print($data);
70 0 ▶ close $fh or die "Failed to close $path - $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close $path - $!" 0 0 0
71 0 return $self;
72 }
73  
74 =head2 save_string
75  
76 Return output as a scalar.
77  
78 =cut
79  
80 0 ▶sub save_string {
Subroutine save_string: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82 0 my %args = @_;
83  
84 0 return JSON::XS->new->pretty->utf8->encode($self->structure_from_model($args{model}));
85 }
86  
87 =head2 parse
88  
89 Create and parse the L<JSON::XS> object.
90  
91 =cut
92  
93 2 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
94 2 my $self = shift;
95 2 my $string = shift;
96 2 my $json = JSON::XS->new;
97 2 my $def = $json->decode($string);
98 2 logDebug($def);
99 2 return $def;
100 }
101  
102 5 1;
103  
104 __END__
« EntityModel/Resolver.pm ↑ Summary Test/EntityModel.pm »