blib/lib/Data/Phrasebook/Loader/YAML.pm

filter: Statementstmt 100% 60 / 60 Branchbran 100% 34 / 34 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 98.4% 127 / 129 CC 24 SCAR 18.9
CC
24
Cov
99%
get
9.0
keywords
8.0
load
8.0
CRAP 6.6
↑ Summary
1 package Data::Phrasebook::Loader::YAML;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '0.13';
7  
8 #--------------------------------------------------------------------------
9  
10 9 ▶use base qw( Data::Phrasebook::Loader::Base Data::Phrasebook::Debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #--------------------------------------------------------------------------
16  
17 =head1 NAME
18  
19 Data::Phrasebook::Loader::YAML - Absract your phrases with YAML.
20  
21 =head1 SYNOPSIS
22  
23 use Data::Phrasebook;
24  
25 my $q = Data::Phrasebook->new(
26 class => 'Fnerk',
27 loader => 'YAML',
28 file => 'phrases.yaml',
29 );
30  
31 $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x );
32 my $phrase = $q->fetch($keyword);
33  
34 =head1 DESCRIPTION
35  
36 This class loader implements phrasebook patterns using YAML.
37  
38 Phrases can be contained within one or more dictionaries, with each phrase
39 accessible via a unique key. Phrases may contain placeholders, please see
40 L<Data::Phrasebook> for an explanation of how to use these. Groups of phrases
41 are kept in a dictionary. In this implementation a single file is one
42 complete dictionary.
43  
44 An example YAML file:
45  
46 ---
47 foo: >
48 Welcome to [% my %] world.
49 It is a nice [%place %].
50  
51 Within the phrase text placeholders can be used, which are then replaced with
52 the appropriate values once the get() method is called. The default style of
53 placeholders can be altered using the delimiters() method.
54  
55 =head1 INHERITANCE
56  
57 L<Data::Phrasebook::Loader::YAML> inherits from the base class
58 L<Data::Phrasebook::Loader::Base>.
59 See that module for other available methods and documentation.
60  
61 =head1 METHODS
62  
63 =head2 load
64  
65 Given a C<file>, load it. C<file> must contain a YAML map.
66  
67 $loader->load( $file, @dict );
68  
69 This method is used internally by L<Data::Phrasebook::Generic>'s
70 C<data> method, to initialise the data store.
71  
72 It must take a C<file> (be it a scalar, or something more complex)
73 and return a handle. The C<dict> is optional, should you wish to use the
74 dictionary support.
75  
76 =cut
77  
78 11 ▶sub load {
Subroutine load: called CC=8 SCAR=20.8
Pod documented
79 11 my ($class, $file, @dict) = @_;
80 11 ▶ croak "No file given as argument!" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 2 9 11
81 9 my ($d) = YAML::LoadFile( $file );
82 9 ▶ croak "Badly formatted YAML file $file" unless ref $d eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d eq "HASH" 1 8 9
83 8 $class->{yaml} = $d;
84  
85 # what sections are we using?
86 8 ▶ my $key = $class->{defaultname} || ($class->dicts)[0];
Condition100%3/3
$class->{'defaultname'} || ($class->dicts)[0]
l!l&&r!l&&!r
242
MC/DC100%2/2
$class->{'defaultname'} || ($class->dicts)[0]
$class->{'defaultname'} ($class->dicts)[0]
Truth table100%3/3
A || B
A= $class->{'defaultname'}B= ($class->dicts)[0]
ABresult
1X1
011
000
87 $class->{default} = ($key ? $class->{yaml}->{$key}
88 8 ▶ : $class->{yaml});
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 6 2 8
89  
90 8 $class->{dict} = [];
91 8 ▶ $class->{dict} = [$class->{defaultname}] if $class->{defaultname};
Branch: 1 branch
BranchTrueFalseTotal
if $class->{"defaultname"} 2 6 8
92 8 ▶ $class->{dict} = (ref $dict[0] ? $dict[0] : [@dict]) if scalar @dict;
Branch: 2 branches
BranchTrueFalseTotal
if scalar @dict 3 5 8
ref $dict[0] ? : 1 2 3
93 }
94  
95 =head2 get
96  
97 Returns the phrase stored in the phrasebook, for a given keyword.
98  
99 my $value = $loader->get( $key );
100  
101 If one or more named dictionaries have been previously selected, they will be
102 searched in order, followed by the default dictionary. The first hit on
103 C<key> will be returned, otherwise C<undef> is returned.
104  
105 =cut
106  
107 23 ▶sub get {
Subroutine get: called CC=9 SCAR=22.0
Pod documented
108 23 my ($class,$key) = @_;
109 23 ▶ return unless($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $key 5 18 23
110 18 ▶ return unless($class->{yaml});
Branch: 1 branch
BranchTrueFalseTotal
unless $class->{"yaml"} 1 17 18
111  
112 17 ▶ my @dicts = (ref $class->{dict} ? @{$class->{dict}} : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $class->{'dict'} ? : 16 1 17
113  
114 17 foreach ( @dicts ) {
115 return $class->{yaml}->{$_}->{$key}
116 if exists $class->{yaml}->{$_}
117 12 ▶ and exists $class->{yaml}->{$_}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $class->{"yaml"}{$_} and exists $class->{"yaml"}{$_}{$key} 4 8 12
Condition100%3/3
exists $class->{"yaml"}{$_} and exists $class->{"yaml"}{$_}{$key}
!ll&&!rl&&r
264
MC/DC100%2/2
exists $class->{"yaml"}{$_} and exists $class->{"yaml"}{$_}{$key}
exists $class->{"yaml"}{$_} exists $class->{"yaml"}{$_}{$key}
Truth table100%3/3
A and B
A= exists $class->{"yaml"}{$_}B= exists $class->{"yaml"}{$_}{$key}
ABresult
0X0
100
111
118 }
119  
120 return $class->{default}->{$key}
121 if ref $class->{default} eq 'HASH'
122 13 ▶ and exists $class->{default}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $class->{"default"} eq "HASH" and exists $class->{"default"}{$key} 5 8 13
Condition100%3/3
ref $class->{"default"} eq "HASH" and exists $class->{"default"}{$key}
!ll&&!rl&&r
175
MC/DC100%2/2
ref $class->{"default"} eq "HASH" and exists $class->{"default"}{$key}
ref $class->{"default"} eq "HASH" exists $class->{"default"}{$key}
Truth table100%3/3
A and B
A= ref $class->{"default"} eq "HASH"B= exists $class->{"default"}{$key}
ABresult
0X0
100
111
123  
124 8 return;
125 }
126  
127 =head2 dicts
128  
129 Returns the list of dictionaries available.
130  
131 my @dicts = $loader->dicts();
132  
133 This is the list of all dictionaries available in the source file. If multiple
134 dictionaries are not being used, then an empty list will be returned.
135  
136 =cut
137  
138 15 ▶sub dicts {
Subroutine dicts: called CC=2 SCAR=6.9
Pod documented
139 15 my $class = shift;
140  
141 15 my @keys = keys %{$class->{yaml}};
142 15 ▶ if ( scalar @keys ==
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys == scalar grep({ref $_ eq "HASH";} values %{$$class{"yaml"};})) 11 4 15
143 36 scalar grep {ref $_ eq 'HASH'} values %{$class->{yaml}} ) {
144 # data source looks like it has multiple dictionaries
145 11 return (sort @keys);
146 }
147  
148 4 return ();
149 }
150  
151 =head2 keywords
152  
153 Returns the list of keywords available. List is lexically sorted.
154  
155 my @keywords = $loader->keywords( $dict );
156  
157 If one or more named dictionaries have been previously selected, they will be
158 farmed for keywords, followed by the default dictionary.
159  
160 The C<dict> argument is optional, and may be used to override the search to a
161 single named dictionary, or a list of dictionaries if passed by reference,
162 plus the default dictionary of course.
163  
164 To find all available keywords in all available dictionaries, use the
165 following:
166  
167 $loader->keywords( [ $loader->dicts ] );
168  
169 =cut
170  
171 18 ▶sub keywords {
Subroutine keywords: called CC=8 SCAR=20.8
Pod documented
172 18 my ($class, $dict) = @_;
173 18 my (%keywords, @dicts);
174  
175 18 ▶ @dicts = ( (not $dict) ? (ref $class->{dict} ? @{$class->{dict}} : ())
Branch: 3 branches
BranchTrueFalseTotal
!$dict ? : 7 11 18
ref $class->{'dict'} ? : 6 1 7
ref $dict ? : 6 5 11
176 : (ref $dict) ? @$dict : ($dict) );
177  
178 18 foreach my $d (@dicts) {
179 next unless
180 exists $class->{yaml}->{$d}
181 23 ▶ and ref $class->{yaml}->{$d} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $class->{"yaml"}{$d} and ref $class->{"yaml"}{$d} eq "HASH" 2 21 23
Condition66%2/3
exists $class->{"yaml"}{$d} and ref $class->{"yaml"}{$d} eq "HASH"
!ll&&!rl&&r
2021
MC/DC50%1/2
exists $class->{"yaml"}{$d} and ref $class->{"yaml"}{$d} eq "HASH"
exists $class->{"yaml"}{$d} ref $class->{"yaml"}{$d} eq "HASH"
Truth table66%2/3
A and B
A= exists $class->{"yaml"}{$d}B= ref $class->{"yaml"}{$d} eq "HASH"
ABresult
0X0
100
111
182 21 map { $keywords{$_} = 1 } keys %{$class->{yaml}->{$d}};
183 }
184  
185 18 ▶ if (ref $class->{default} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class->{"default"} eq "HASH") 17 1 18
186 17 map { $keywords{$_} = 1 } keys %{$class->{default}};
187 }
188  
189 18 my @keywords = sort keys %keywords;
190 18 return @keywords;
191 }
192  
193 =head2 set_default
194  
195 If a requested phrase is not found in the named dictionary an attempt is made
196 to find it in the I<default> dictionary. L<Data::Phrasebook> loaders normally
197 use the first dictionary in the phrasebook as the default, but as mentioned in
198 L</"DICTIONARY SUPPORT"> this does not make sense because the dictionaries in
199 YAML phrasebooks are not ordered.
200  
201 To override the automatically selected default dictionary use this method, and
202 pass it a C<default_dictionary_name>. This value is only reset at phrasebook
203 load time, so you'll probably need to trigger a reload:
204  
205 $q->loader->set_default( $default_dictionary_name );
206 $q->loader->load( $file );
207  
208 To reset the loader's behaviour to automatic default dictionary selection,
209 pass this method an undefined value, and then reload.
210  
211 =cut
212  
213 2 ▶sub set_default {
Subroutine set_default: called CC=1 SCAR=0.0
Pod documented
214 2 $_[0]->{defaultname} = $_[1];
215 }
216  
217 9 1;
218  
219 __END__
↑ Summary