Phrasebook/Generic.pm

filter: Statementstmt 100% 99 / 99 Branchbran 80.0% 40 / 50 Conditioncond 71.4% 10 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 10 / 10 totaltotal 90.3% 178 / 197 CC 33 SCAR 15.6
CC
33
Cov
91%
data
8.0
dict
8.0
dicts
4.2
CRAP 4.7
« Phrasebook/Loader.pm ↑ Summary Phrasebook/Plain.pm »
1 package Data::Phrasebook::Generic;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Data::Phrasebook::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use base qw( Data::Phrasebook::Debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 $VERSION = '0.35';
10  
11 =head1 NAME
12  
13 Data::Phrasebook::Generic - Base class for Phrasebook Models
14  
15 =head1 SYNOPSIS
16  
17 use Data::Phrasebook;
18  
19 my $q = Data::Phrasebook->new(
20 class => 'Fnerk',
21 loader => 'XML',
22 file => 'phrases.xml',
23 dict => 'English',
24 );
25  
26 =head1 DESCRIPTION
27  
28 This module provides a base class for phrasebook implementations.
29  
30 =head1 CONSTRUCTOR
31  
32 =head2 new
33  
34 C<new> takes an optional hash of arguments. Each value in the hash
35 is given as an argument to a method of the same name as the key.
36  
37 This constructor should B<never> need to be called directly as
38 Phrasebook creation should go through the L<Data::Phrasebook> factory.
39  
40 Subclasses should provide at least an accessor method to retrieve values for
41 a named key. Further methods can be overloaded, but must retain a standard
42 API to the overloaded method.
43  
44 All, or at least I<most>, phrasebook implementations should inherit from
45 B<this> class.
46  
47 =cut
48  
49 21 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
50 21 my $class = shift;
51 21 my %hash = @_;
52 21 ▶ $hash{loader} ||= 'Text';
Condition100%2/2
$hash{"loader"} ||= "Text"
l!l
318
MC/DC100%1/1
$hash{"loader"} ||= "Text"
$hash{"loader"}
Truth table100%2/2
A
A= $hash{"loader"}
Aresult
11
00
53  
54 21 ▶ if($class->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->debug) 1 20 21
55 1 $class->store(3,"$class->new IN");
56 1 $class->store(4,"$class->new args=[".$class->dumper(\%hash)."]");
57 }
58  
59 21 my $self = bless {}, $class;
60  
61 # set default delimiters, in case custom delimiters
62 # are provided in the hash
63 21 $self->{delimiters} = qr{ :(\w+) }x;
64  
65 21 foreach (keys %hash) {
66 39 $self->$_($hash{$_});
67 }
68  
69 21 return $self;
70 }
71  
72 =head1 METHODS
73  
74 =head2 loader
75  
76 Set, or get, the loader class. Uses a default if none have been
77 specified. See L<Data::Phrasebook::Loader>.
78  
79 =head2 unload
80  
81 Called by the file() and dict() methods when a fresh file or dictionary is
82 specified, and reloading is required.
83  
84 =head2 loaded
85  
86 Accessor to determine whether the current dictionary has been loaded
87  
88 =head2 file
89  
90 A description of a file that is passed to the loader. In most cases,
91 this is a file. A loader that gets its data from a database could
92 conceivably have this as a hash like thus:
93  
94 $q->file( {
95 dsn => "dbi:SQLite:dbname=bookdb",
96 table => 'phrases',
97 } );
98  
99 That is, which loader you use determines what your C<file> looks like.
100  
101 The default loader takes just an ordinary filename.
102  
103 =head2 dict
104  
105 Accessor to store the dictionary to be used.
106  
107 =cut
108  
109 103 ▶sub loader {
Subroutine loader: called CC=3 SCAR=11.1
Pod documented
110 103 my $self = shift;
111 103 ▶ my $load = @_ ? shift : defined $self->{loader} ? $self->{loader} : 'Text';
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 73 30 103
defined $self->{'loader'} ? : 30 0 30
112 103 $self->{loader} = $load;
113 }
114 24 ▶sub unload {
Subroutine unload: called CC=1 SCAR=0.0
Pod documented
115 24 my $self = shift;
116 24 $self->{loaded} = undef;
117 24 $self->{'loaded-data'} = undef;
118 24 return;
119 }
120  
121 55 ▶sub loaded {
Subroutine loaded: called CC=3 SCAR=11.0
Pod documented
122 55 my $self = shift;
123 55 ▶ my $load = @_ ? $self->{loaded} = shift : $self->{loaded} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 18 37 55
124  
125 # ensure we know what loader class we are getting
126 55 ▶ $self->loader($load->class) if($load);
Branch: 1 branch
BranchTrueFalseTotal
if $load 27 28 55
127 55 return $load;
128 }
129 49 ▶sub file {
Subroutine file: called CC=4 SCAR=14.0
Pod documented
130 49 my $self = shift;
131 49 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 29 49
132 20 my $file = shift;
133 20 ▶ if(!$self->{file} || $file ne $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"file"} or $file ne $self->{"file"}) 20 0 20
Condition66%2/3
not $self->{"file"} or $file ne $self->{"file"}
l!l&&r!l&&!r
1730
MC/DC0%0/2
not $self->{"file"} or $file ne $self->{"file"}
not $self->{"file"} $file ne $self->{"file"}
Truth table66%2/3
not(A) or B
A= not $self->{"file"}B= $file ne $self->{"file"}
ABresult
1X1
011
000
134 20 $self->unload();
135 20 $self->{file} = $file;
136 }
137 }
138 49 return $self->{file};
139 }
140  
141 37 ▶sub dict {
Subroutine dict: called CC=8 SCAR=20.8
Pod documented
142 37 my $self = shift;
143  
144 37 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 33 37
145 4 my $list1 = "@_";
146 4 ▶ my $list2 = $self->{dict} ? "@{$self->{dict}}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dict'} ? : 2 2 4
147  
148 4 ▶ if($list1 ne $list2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list1 ne $list2) 4 0 4
149 4 $self->unload();
150 4 ▶ $self->{dict} = (ref $_[0] ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 4 4
151 }
152 }
153  
154 37 ▶ return($self->{dict} ? @{$self->{dict}} : () ) if(wantarray);
Branch: 2 branches
BranchTrueFalseTotal
if wantarray 30 7 37
$self->{'dict'} ? : 4 26 30
155 7 ▶ return($self->{dict} ? $self->{dict}->[0] : undef );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dict'} ? : 4 3 7
156 }
157  
158 =head2 dicts
159  
160 Having instantiated the C<Data::Phrasebook> object class, and using the C<file>
161 attribute as a directory path, the object can return a list of the current
162 dictionaries available (provided the plugin supports it) as:
163  
164 my $pb = Data::Phrasebook->new(
165 loader => 'Text',
166 file => '/tmp/phrasebooks',
167 );
168  
169 my @dicts = $pb->dicts;
170  
171 or
172  
173 my @dicts = $pb->dicts( $path );
174  
175 =cut
176  
177 3 ▶sub dicts {
Subroutine dicts: called CC=4 SCAR=14.2
Pod documented
178 3 my $self = shift;
179  
180 3 my $loader = $self->loaded;
181 3 ▶ unless($loader) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loader) 3 0 3
182 3 ▶ $self->store(4,"->dicts loader=[".($self->loader)."]") if($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 3 3
183 3 $loader = Data::Phrasebook::Loader->new(
184 'class' => $self->loader,
185 'parent' => $self,
186 );
187 3 $self->loader($loader->class); # so we know what we've got
188 }
189  
190 # just in case it doesn't use D::P::Loader::Base
191 3 ▶ croak("dicts() unsupported in plugin")
Branch: 1 branch
BranchTrueFalseTotal
unless $loader->can("dicts") 0 3 3
192 unless($loader->can("dicts"));
193  
194 3 return $loader->dicts(@_);
195 }
196  
197 =head2 keywords
198  
199 Having instantiated the C<Data::Phrasebook> object class, using the C<dict>
200 attribute as required, the object can return a list of the current keywords
201 available (provided the plugin supports it) as:
202  
203 my $pb = Data::Phrasebook->new(
204 loader => 'Text',
205 file => '/tmp/phrasebooks',
206 dict => 'TEST',
207 );
208  
209 my @keywords = $pb->keywords;
210  
211 or
212  
213 my @keywords = $pb->keywords( $dict );
214  
215 Note the list will be a combination of the default and any named dictionary.
216 However, not all Loader plugins may support the second usage.
217  
218 =cut
219  
220 7 ▶sub keywords {
Subroutine keywords: called CC=4 SCAR=14.0
Pod documented
221 7 my $self = shift;
222  
223 7 my $loader = $self->loaded;
224 7 ▶ if(!defined $loader) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loader) 5 2 7
225 5 ▶ $self->store(4,"->keywords loader=[".($self->loader)."]") if($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 5 5
226 5 $loader = Data::Phrasebook::Loader->new(
227 'class' => $self->loader,
228 'parent' => $self,
229 );
230 5 $self->loader($loader->class); # so we know what we've got
231 }
232  
233 # just in case it doesn't use D::P::Loader::Base
234 7 ▶ croak("keywords() unsupported in plugin")
Branch: 1 branch
BranchTrueFalseTotal
unless $loader->can("keywords") 0 7 7
235 unless($loader->can("keywords"));
236  
237 7 return $loader->keywords(@_);
238 }
239  
240 =head2 data
241  
242 Loads the data source, if not already loaded, and returns the data block
243 associated with the given key.
244  
245 my $data = $self->data($key);
246  
247 This is typically only used internally by implementations, not the end user.
248  
249 =cut
250  
251 25 ▶sub data {
Subroutine data: called CC=8 SCAR=20.9
Pod documented
252 25 my $self = shift;
253 25 my $id = shift;
254  
255 25 ▶ if($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 2 23 25
256 2 $self->store(3,"->data IN");
257 2 $self->store(4,"->data id=[$id]");
258 }
259  
260 25 ▶ return unless($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 25 25
261  
262 25 my $loader = $self->loaded;
263 25 ▶ if(!defined $loader) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loader) 18 7 25
264 18 ▶ if($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 1 17 18
265 1 $self->store(4,"->data loader=[".($self->loader)."]");
266 1 ▶ $self->store(4,"->data file=[".($self->file||'undef')."]");
Condition50%1/2
$self->file || 'undef'
l!l
10
MC/DC0%0/1
$self->file || 'undef'
$self->file
Truth table50%1/2
A
A= $self->file
Aresult
11
00
267 1 ▶ $self->store(4,"->data dict=[".($self->dict||'undef')."]");
Condition50%1/2
$self->dict || 'undef'
l!l
01
MC/DC0%0/1
$self->dict || 'undef'
$self->dict
Truth table50%1/2
A
A= $self->dict
Aresult
11
00
268 }
269 18 $loader = Data::Phrasebook::Loader->new(
270 'class' => $self->loader,
271 'parent' => $self,
272 );
273 17 $self->loader($loader->class); # so we know what we've got
274 17 $loader->load( $self->file, $self->dict );
275 16 $self->loaded($loader);
276 }
277  
278 23 ▶ return $self->{'loaded-data'}->{$id} ||= do { $loader->get( $id ) };
Condition100%3/3
$self->{"loaded-data"}{$id} ||= do { $loader->get($id) }
l!l&&r!l&&!r
1202
MC/DC100%2/2
$self->{"loaded-data"}{$id} ||= do { $loader->get($id) }
$self->{"loaded-data"}{$id} do { $loader->get($id) }
Truth table100%3/3
A ||= B
A= $self->{"loaded-data"}{$id}B= do { $loader->get($id) }
ABresult
1X1
011
000
279 }
280  
281 =head2 delimiters
282  
283 Returns or sets the current delimiters for substitution variables. Must be a
284 regular expression with at least one capture group.
285  
286 The example below shows the default ':variable' style regex.
287  
288 $q->delimiters( qr{ :(\w+) }x );
289  
290 The example below shows a Template Toolkit style regex.
291  
292 $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x );
293  
294 In addition to the delimiter pattern, an optional setting to suppress missing
295 value errors can be passed after the pattern. If set to a true value, will
296 turn any unmatched delimiter patterns to an empty string.
297  
298 =cut
299  
300 25 ▶sub delimiters {
Subroutine delimiters: called CC=3 SCAR=11.0
Pod documented
301 25 my $self = shift;
302 25 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 21 25
303 4 $self->{delimiters} = shift;
304 4 ▶ $self->{blank_args} = shift || 0;
Condition50%1/2
shift() || 0
l!l
04
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
305 }
306
307 25 return $self->{delimiters};
308 }
309  
310 12 1;
311  
312 __END__
« Phrasebook/Loader.pm ↑ Summary Phrasebook/Plain.pm »