lib/File/DataClass/Storage/JSON.pm

filter: Statementstmt 93.3% 70 / 75 Branchbran 77.7% 14 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 100% 2 / 2 totaltotal 92.1% 106 / 115 CC 11 SCAR 9.8
CC
11
Cov
90%
__ANON__
7.3
read_from_file
3.0
__ANON__
2.0
CRAP 2.7
« lib/File/DataClass/Storage/Any.pm ↑ Summary lib/File/DataClass/Schema.pm »
1 package File::DataClass::Storage::JSON;
2  
3 4 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use File::DataClass::Functions qw( extension_map throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::DataClass::Types qw( Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON::MaybeXS qw( JSON );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 extends q(File::DataClass::Storage);
13  
14 4 extension_map 'JSON' => '.json';
15  
16 # Private functions
17 21 ▶my $_build_transcoder = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
18 21 my $options = shift; my $json = JSON->new;
19  
20 21 for (grep { $_ ne 'reboolify' } keys %{ $options }) {
21 51 $json = $json->$_( $options->{ $_ } );
22 }
23  
24 21 return $json;
25 4 };
26  
27 4 ▶my $_reboolify; $_reboolify = sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
28 6 my $in = shift; my $ref = ref $in;
29  
30 0 ▶ if (not $ref) { return $in }
Branch: 4 branches
BranchTrueFalseTotal
if (not $ref) { } 0 6 6
elsif ($ref eq 'HASH') { } 4 2 6
elsif ($ref eq 'ARRAY') { } 0 2 2
elsif ($ref =~ / ::Boolean \z /mx) { } 2 0 2
31 elsif ($ref eq 'HASH') {
32 4 return { map { $_ => $_reboolify->( $in->{ $_ } ) } keys %{ $in } };
33 }
34 0 elsif ($ref eq 'ARRAY') { return [ map { $_reboolify->( $_ ) } @{ $in } ] }
35 2 ▶ elsif ($ref =~ m{ ::Boolean \z }mx) { return ${ $in } ? true : false }
Branch: 1 branch
BranchTrueFalseTotal
${$in;} ? : 1 1 2
36  
37 0 return $in;
38 4 };
39  
40 # Public attributes
41 4 has '+extn' => default => '.json';
42  
43 7 ▶has '+read_options' => builder => sub { { utf8 => false, } };
Subroutine _build_read_options: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
44  
45 7 ▶has '+write_options' => builder => sub { {
Subroutine _build_write_options: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
46 7 canonical => true, convert_blessed => true,
47 4 pretty => true, utf8 => false, } };
48  
49 # Private attributes
50 has '_decoder' => is => 'lazy', isa => Object,
51 1 ▶ builder => sub { $_build_transcoder->( $_[ 0 ]->read_options ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build__decoder: called CC=1 SCAR=0.0
52  
53 has '_encoder' => is => 'lazy', isa => Object,
54 10 ▶ builder => sub { $_build_transcoder->( $_[ 0 ]->write_options ) };
Subroutine _build__encoder: called CC=1 SCAR=0.0
55  
56 # Public methods
57 36 ▶sub read_from_file {
Subroutine read_from_file: called CC=3 SCAR=11.0
Pod documented
58 36 my ($self, $rdr) = @_; my $json = $self->_decoder; my $data;
59  
60 36 ▶ $self->encoding and $rdr->encoding( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
$self->encoding and $rdr->encoding($self->encoding) 1 35 36
61 36 ▶ $rdr->is_empty and return {};
Branch: 1 branch
BranchTrueFalseTotal
$rdr->is_empty and return {} 0 34 34
62  
63 34 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
64 34 $data = $json->decode( $rdr->all );
65 33 ▶ $self->read_options->{reboolify} and $data = $_reboolify->( $data );
Branch: 1 branch
BranchTrueFalseTotal
$self->read_options->{"reboolify"} and $data = &$_reboolify($data) 1 32 33
66 }
67 34 ▶ catch { s{ at \s [^ ]+ \s line \s\d+\. }{}mx; throw "${_} in file ${rdr}" };
Subroutine __ANON__: called CC=1 SCAR=0.0
68  
69 33 return $data;
70 }
71  
72 28 ▶sub write_to_file {
Subroutine write_to_file: called CC=2 SCAR=6.9
Pod documented
73 28 my ($self, $wtr, $data) = @_; my $json = $self->_encoder;
74  
75 28 ▶ $self->encoding and $wtr->encoding( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
$self->encoding and $wtr->encoding($self->encoding) 1 27 28
76 28 $wtr->print( $json->encode( $data ) );
77 27 return $data;
78 }
79  
80 4 1;
81  
82 __END__
« lib/File/DataClass/Storage/Any.pm ↑ Summary lib/File/DataClass/Schema.pm »