TT/CDBFile/JSON.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 93 CC 9 SCAR 34.6
CC
9
Cov
0%
applySub
72.0
fetchSub
6.0
new
2.0
CRAP 31.9
1 ## -*- Mode: CPerl -*-
2 ## File: Lingua::TT::CDBFile,,1S6N.pm
3 ## Author: Bryan Jurish <TT/IO.pm>
4 ## Descript: TT I/O: CDB: tied read-only access via CDB_File, JSON values
5  
6 package Lingua::TT::CDBFile::JSON;
7 use Lingua::TT::CDBFile;
8 use Lingua::TT::Dict::JSON;
9 use JSON::XS;
10 use Carp;
11 use IO::File;
12 use strict;
13  
14 ##==============================================================================
15 ## Globals & Constants
16  
17 our @ISA = qw(Lingua::TT::CDBFile Lingua::TT::Dict::JSON);
18  
19 ##==============================================================================
20 ## Constructors etc.
21  
22 ## $dbf = CLASS_OR_OBJECT->new(%opts)
23 ## + %opts, %$doc:
24 ## ##-- new options
25 ## jxs => $jxs, ##-- underlying JSON::XS object; see jsonxs() method
26 ## ##
27 ## ##-- user options, inherited from TT::CDBFile
28 ## file => $filename, ##-- default: undef (none)
29 ## tmpfile => $tmpfilename, ##-- defualt: "$filename.$$" (not used correctly due to CDB_File bug)
30 ## mode => $mode, ##-- open mode 'r', 'w', 'rw', '<', '>', '>>': default='r'
31 ## utf8 => $bool, ##-- if true, keys/values are stored as UTF8 (default=1) -- n/a here, always utf8
32 ## ##
33 ## ##-- low-level data, inherited from TT::CDBFile
34 ## data => \%data, ##-- tied data (hash)
35 ## tied => $ref, ##-- read-only: reference returned by tie()
36 ## writer => $ref, ##-- read/write: reference returned by CDB_File::new()
37 ## fetch => \&fetch, ##-- fetch subroutine: $val = $fetch->($key)
38 ## store => \&store, ##-- store subroutine: $val = $store->($key,$val)#
39 ## jxs => $jxs, ##-- underlying JSON::XS object; see jsonxs() method
40 sub new {
41 my $that = shift;
42 return $that->Lingua::TT::CDBFile::new(@_,utf8=>1);
43 }
44  
45 ##==============================================================================
46 ## Methods: low-level utilities
47  
48 ## $jxs = $obj->jsonxs()
49 ## + INHERITED from TT::Dict::JSON
50  
51 ##==============================================================================
52 ## Methods: I/O
53  
54 ## $dbf = $dbf->open($file,%opts)
55 ## + %opts are as for new()
56 ## + $file defaults to $dbf->{file}
57 ## + INHERITED
58  
59 ##==============================================================================
60 ## Methods: Lookup
61  
62 ## \&sub = $dbf->fetchSub($key)
63 ## + subroutine to return (decoded) value
64 sub fetchSub {
65 my $tied = $_[0]{tied};
66 my $jxs = $_[0]->jsonxs;
67 my ($val);
68 return sub {
69 return undef if (!defined($val = $tied->FETCH($_[0])));
70 utf8::decode($val);
71 return $jxs->decode($val);
72 };
73 }
74  
75 ## $storeSub = $dbf->store($key,$val)
76 sub storeSub {
77 my $tied = $_[0]{tied};
78 my $jxs = $_[0]->jsonxs;
79 return sub {
80 return $tied->STORE($_[0],$jxs->encode($_[1]));
81 };
82 }
83  
84 ##==============================================================================
85 ## Methods: Apply
86  
87 ## \&apply = $dict->applySub(%opts)
88 ## + returns a CODE-ref for applying dictionary analysis to a single item
89 ## + returned sub is called without arguments
90 ## - data line to be analyzed (chomped) is in $_
91 ## - output for current data line should be stored in $_
92 sub applySub {
93 my ($dict,%opts) = @_;
94 my $jxs = $dict->jsonxs;
95 my $jxs0 = JSON::XS->new->utf8(1)->allow_nonref(1);
96 my $tied = $dict->{tied};
97 #my $include_empty = $opts{allow_empty};
98 my ($text,$a_in,$a_dict);
99 return sub {
100 ($text,$a_in) = split(/\t/,$_,2);
101 $a_dict = $tied->FETCH($text);
102  
103 $a_in = $jxs->decode($a_in) if (defined($a_in));
104 $a_dict = $jxs0->decode($a_dict) if (defined($a_dict));
105  
106 if (!defined($a_dict)) {
107 ##-- +in, -dict
108 ;
109 }
110 elsif (!defined($a_in)) {
111 ##-- -in, +dict
112 $a_in = $a_dict;
113 }
114 elsif (ref($a_in) eq 'HASH' && ref($a_dict) eq 'HASH') {
115 ##-- +in, +dict: HASH
116 @$a_in{keys %$a_dict} = values %$a_dict;
117 }
118 elsif (ref($a_in) eq 'ARRAY' && ref($a_dict) eq 'ARRAY') {
119 ##-- +in, +dict: ARRAY
120 push(@$a_in, @$a_dict);
121 }
122 else {
123 ##-- +in, +dict: OTHER
124 warn(ref($dict)."::applySub(): cannot merge values $a_in, $a_dict for key '$text'");
125 $a_in = $a_dict;
126 }
127 $_ = join("\t", $text, (defined($a_in) ? $jxs->encode($a_in) : qw()))."\n";
128 };
129 }
130  
131  
132 ##==============================================================================
133 ## Methods: TT::Persistent
134  
135 ## @keys = $dbf->noSaveKeys()
136 sub noSaveKeys {
137 return ($_[0]->SUPER::noSaveKeys(), qw(jxs));
138 }
139  
140 ##==============================================================================
141 ## Footer
142 1;
143  
144 __END__