Indexed/JSON.pm

filter: Statementstmt 100% 27 / 27 Branchbran 75.0% 6 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 80.0% 4 / 5 totaltotal 87.5% 49 / 56 CC 8 SCAR 9.2
CC
8
Cov
90%
json
4.1
readData
3.0
open
2.0
CRAP 2.5
« Indexed/Utf8.pm ↑ Summary Indexed/Freeze.pm »
1 ##-*- Mode: CPerl -*-
2 ##
3 ## File: Tie/File/Indexed/JSON.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: tied array access to indexed data files: JSON-encoded data structures
6  
7 package Tie::File::Indexed::JSON;
8 2 ▶use Tie::File::Indexed;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ##======================================================================
13 ## Globals
14  
15 2 our @ISA = qw(Tie::File::Indexed);
16  
17 ##======================================================================
18 ## Constructors etc.
19  
20 ## $tfi = CLASS->new(%opts)
21 ## $tfi = CLASS->new($file,%opts)
22 ## + new %opts, object structure:
23 ## (
24 ## json => $json, ##-- JSON object or HASH-ref of options
25 ## )
26  
27 ## \%defaults = CLASS_OR_OBJECT->defaults()
28 ## + default attributes for constructor
29 1 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
30 return (
31 1 $_[0]->SUPER::defaults,
32 json => {utf8=>1, relaxed=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>0, canonical=>0},
33 );
34 }
35  
36 ##--------------------------------------------------------------
37 ## Utilities: JSON
38  
39 ## $json = $aj->json()
40 ## + returns json codec
41 1 ▶sub json {
Subroutine json: called CC=4 SCAR=14.1
Pod undocumented
42 1 ▶ return $_[0]{json} if (UNIVERSAL::isa($_[0]{json}, 'JSON'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0]{"json"}, "JSON") 0 1 1
43 1 my $json = JSON->new;
44 1 ▶ foreach (grep {$json->can($_)} keys %{$_[0]{json}//{}}) {
Condition50%1/2
$_[0]{"json"} // {}
l!l
10
MC/DC0%0/1
$_[0]{"json"} // {}
$_[0]{"json"}
Truth table50%1/2
A
A= $_[0]{"json"}
Aresult
11
00
45 8 $json->can($_)->($json,$_[0]{json}{$_});
46 }
47 1 return $_[0]{json} = $json;
48 }
49  
50 ##======================================================================
51 ## Object API: overrides
52  
53 ##--------------------------------------------------------------
54 ## Object API: overrides: open/close
55  
56 ## $tfi_or_undef = $tfi->open($file,$mode)
57 ## $tfi_or_undef = $tfi->open($file)
58 ## $tfi_or_undef = $tfi->open()
59 ## + opens file(s)
60 1 ▶sub open {
Subroutine open: called CC=2 SCAR=7.0
Pod documented
61 1 my $tfi = shift;
62 1 ▶ return undef if (!$tfi->SUPER::open(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless $tfi->SUPER::open(@_) 0 1 1
63  
64 ##-- ensure 'json' object is defined
65 1 $tfi->json();
66 1 return $tfi;
67 }
68  
69 ##======================================================================
70 ## Subclass API: Data I/O
71  
72 ## $bool = $tfi->writeData($utf8_string)
73 ## + override transparently encodes $data using the JSON module
74 7 ▶sub writeData {
Subroutine writeData: called CC=2 SCAR=6.9
Pod documented
75 7 ▶ return 1 if (!defined($_[1])); ##-- don't waste space on undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 1 6 7
76 6 return $_[0]{datfh}->print( $_[0]{json}->encode($_[1]) );
77 }
78  
79 ## $data_or_undef = $tfi->readData($length)
80 ## + override decodes stored data using the JSON module
81 7 ▶sub readData {
Subroutine readData: called CC=3 SCAR=11.1
Pod documented
82 7 ▶ return undef if ($_[1]==0 || !defined(my $buf=$_[0]->SUPER::readData($_[1])));
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] == 0 or not defined(my $buf = $_[0]->SUPER::readData($_[1])) 2 5 7
Condition66%2/3
$_[1] == 0 or not defined(my $buf = $_[0]->SUPER::readData($_[1]))
l!l&&r!l&&!r
205
MC/DC50%1/2
$_[1] == 0 or not defined(my $buf = $_[0]->SUPER::readData($_[1]))
$_[1] == 0 not defined(my $buf = $_[0]->SUPER::readData($_[1]))
Truth table66%2/3
A or not(B)
A= $_[1] == 0B= not defined(my $buf = $_[0]->SUPER::readData($_[1]))
ABresult
1X1
011
000
83 5 return $_[0]{json}->decode($buf);
84 }
85  
86  
87 2 1; ##-- be happpy
« Indexed/Utf8.pm ↑ Summary Indexed/Freeze.pm »