blib/lib/Bot/Cobalt/Lang.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 69.2% 18 / 26 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.2% 20 / 21 Podpod 0.0% 0 / 1 totaltotal 86.3% 126 / 146 CC 18 SCAR 14.0
CC
18
Cov
87%
_build_rpls
11.0
__ANON__
6.0
BUILD
4.0
CRAP 4.0
« blib/lib/Bot/Cobalt/Core/Role/IRC.pm ↑ Summary blib/lib/Bot/Cobalt/Core/ContextMeta.pm »
1 package Bot::Cobalt::Lang;
2 2 $Bot::Cobalt::Lang::VERSION = '0.021003';
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Bot::Cobalt::Common qw/:types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Bot::Cobalt::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Types::Path::Tiny -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ## Configurable:
21 2 has lang_dir => (
22 # BUILD dies without me or absolute_path (unless use_core_only => 1)
23 lazy => 1,
24 is => 'ro',
25 isa => Dir,
26 coerce => 1,
27 predicate => 'has_lang_dir',
28 writer => '_set_lang_dir',
29 );
30  
31 2 has lang => (
32 required => 1,
33 is => 'rwp',
34 isa => Str,
35 );
36  
37 2 has absolute_path => (
38 # BUILD dies without me or lang_dir (unless use_core_only => 1)
39 lazy => 1,
40 is => 'ro',
41 isa => AbsPath,
42 coerce => 1,
43 predicate => 'has_absolute_path',
44 writer => '_set_absolute_path',
45 );
46  
47 has _full_lang_path => (
48 lazy => 1,
49 is => 'ro',
50 isa => Path,
51 coerce => 1,
52 4 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine _build__full_lang_path: called CC=2 SCAR=7.0
53 4 my ($self) = @_;
54 4 ▶ return $self->absolute_path if $self->has_absolute_path;
Branch: 2 branches
BranchTrueFalseTotal
if $self->has_absolute_path 1 3 4
if $self->has_absolute_path 0 1 1
55 3 path( $self->lang_dir .'/'. $self->lang .'.yml' )
56 },
57 2 );
58  
59 has use_core => (
60 is => 'rwp',
61 isa => Bool,
62 5 ▶ builder => sub { 0 },
Subroutine _build_use_core: called CC=1 SCAR=0.0
63 2 );
64  
65 has use_core_only => (
66 is => 'rwp',
67 isa => Bool,
68 6 ▶ builder => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 1 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
70 1 my ($self, $val) = @_;
71 1 ▶ $self->_set_use_core(1) if $val
Branch: 1 branch
BranchTrueFalseTotal
if $val 1 0 1
72 },
73 2 );
74  
75 has _core_set => (
76 lazy => 1,
77 is => 'ro',
78 isa => HashObj,
79 coerce => 1,
80 3 ▶ builder => sub {
Subroutine _build__core_set: called CC=2 SCAR=7.0
81 3 my ($self) = @_;
82 3 my $cset_path = dist_dir('Bot-Cobalt') .'/etc/langs/english.yml';
83 3 my $core_set_yaml = path($cset_path)->slurp_utf8;
84 3 ▶ confess "Failed to read core set at $cset_path"
Branch: 1 branch
BranchTrueFalseTotal
unless $core_set_yaml 0 3 3
85 unless $core_set_yaml;
86 3 Bot::Cobalt::Serializer->new->thaw($core_set_yaml)
87 },
88 2 );
89  
90 has rpls => (
91 lazy => 1,
92 is => 'rwp',
93 isa => HashObj,
94 coerce => 1,
95 6 ▶ builder => sub {
Subroutine _build_rpls: called CC=9 SCAR=23.9
96 ## FIXME ? at least cleanups, certainly
97 6 my ($self) = @_;
98 6 my $rpl_hash;
99  
100 ## Core (built-in) load; shallow copy:
101 6 ▶ $rpl_hash = \%{ $self->_core_set->{RPL} }
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_core 3 3 6
102 if $self->use_core;
103  
104 6 ▶ if ( $self->use_core_only ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_core_only) 1 5 6
105 1 $self->_set_spec( $self->_core_set->{SPEC} );
106 1 return $rpl_hash
107 }
108  
109 5 my $croakable;
110 5 ▶ my $loaded_set = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 5 Bot::Cobalt::Serializer->new->readfile( $self->_full_lang_path )
112 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
113 ## croak() by default.
114 ## If this is a core set load, return empty hash.
115 0 ▶ if ( !$self->use_core ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->use_core) { } 0 0 0
116 0 $croakable = "readfile() failure for " . $self->lang .
117 "(" . $self->_full_lang_path . "): " .
118 $_ ;
119 undef
120 0 } else {
121 0 carp "Language load failure for ".$self->lang.": $_\n";
122 0 +{ RPL => +{} }
123 }
124 5 ▶ } or croak $croakable;
Branch: 1 branch
BranchTrueFalseTotal
my $loaded_set = &try(sub { "Bot::Cobalt::Serializer"->new->readfile($self->_full_lang_path); } , &catch(sub { if (not $self->use_core) { $croakable = 'readfile() failure for ' . $self->lang . '(' . $self->_full_lang_path . '): ' . $_; undef; } else { carp('Language load failure for ' . $self->lang . ": $_\n"); +{"RPL", {}}; } ; } )) or croak($croakable) 5 0 5
125  
126 5 ▶ if ( $self->use_core ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_core) 2 3 5
127 2 ▶ my $rev_for_loaded = $loaded_set->{SPEC} // 0;
Condition50%1/2
$loaded_set->{"SPEC"} // 0
l!l
20
MC/DC0%0/1
$loaded_set->{"SPEC"} // 0
$loaded_set->{"SPEC"}
Truth table50%1/2
A
A= $loaded_set->{"SPEC"}
Aresult
11
00
128 2 ▶ my $rev_for_builtin = $self->_core_set->{SPEC} // 0;
Condition50%1/2
$self->_core_set->{"SPEC"} // 0
l!l
20
MC/DC0%0/1
$self->_core_set->{"SPEC"} // 0
$self->_core_set->{"SPEC"}
Truth table50%1/2
A
A= $self->_core_set->{"SPEC"}
Aresult
11
00
129  
130 2 ▶ if ($rev_for_builtin > $rev_for_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev_for_builtin > $rev_for_loaded) 0 2 2
131 0 warn
132 "Appear to be loading a core language set, but the internal",
133 " set has a higher SPEC number than the loaded set",
134 " ($rev_for_builtin > $rev_for_loaded).\n",
135 " You may want to update language sets.\n" ;
136 }
137  
138 }
139
140 5 my $loaded_rpl_hash = $loaded_set->{RPL};
141  
142 5 ▶ confess "Language set loaded but no RPL hash found"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $loaded_rpl_hash eq "HASH" 0 5 5
143 unless ref $loaded_rpl_hash eq 'HASH';
144  
145 5 $self->_set_spec( $loaded_set->{SPEC} );
146
147 5 @{$rpl_hash}{ keys(%$loaded_rpl_hash) }
148 5 = @{$loaded_set->{RPL}}{ keys(%$loaded_rpl_hash) } ;
149  
150 5 $rpl_hash
151 },
152 2 );
153  
154 has spec => (
155 is => 'rwp',
156 isa => Int,
157 7 ▶ builder => sub { 0 },
Subroutine _build_spec: called CC=1 SCAR=0.0
158 2 );
159  
160  
161 7 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
162 7 my ($self) = @_;
163 7 ▶ unless ( $self->use_core_only ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->use_core_only) 6 1 7
164 6 ▶ die "Need either a lang_dir or an absolute path"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_absolute_path or $self->has_lang_dir 1 5 6
Condition100%3/3
$self->has_absolute_path or $self->has_lang_dir
l!l&&r!l&&!r
141
MC/DC100%2/2
$self->has_absolute_path or $self->has_lang_dir
$self->has_absolute_path $self->has_lang_dir
Truth table100%3/3
A or B
A= $self->has_absolute_pathB= $self->has_lang_dir
ABresult
1X1
011
000
165 unless $self->has_absolute_path or $self->has_lang_dir;
166 }
167 ## Load/validate rpls() at construction time.
168 6 $self->rpls;
169 }
170  
171 2 1;
172  
173 =pod
174  
175 =head1 NAME
176  
177 Bot::Cobalt::Lang - Bot::Cobalt language set loader
178  
179 =head1 SYNOPSIS
180  
181 use Bot::Cobalt::Lang;
182  
183 ## Load 'english.yml' from language dir:
184 my $english = Bot::Cobalt::Lang->new(
185 lang => 'english',
186 lang_dir => $path_to_lang_dir,
187 );
188
189 ## Access loaded RPL hash:
190 my $str = $english->rpls->{$rpl};
191  
192 ## Fall back to core set:
193 my $language = Bot::Cobalt::Lang->new(
194 use_core => 1,
195 lang => $language,
196 lang_dir => $lang_dir,
197 );
198
199 ## Use an absolute path:
200 my $language = Bot::Cobalt::Lang->new(
201 lang => "mylangset",
202 absolute_path => $path_to_my_lang_yaml,
203 );
204  
205 ## Load only the core (built-in) set:
206 my $coreset = Bot::Cobalt::Lang->new(
207 lang => 'coreset',
208 use_core_only => 1,
209 );
210  
211 =head1 DESCRIPTION
212  
213 Bot::Cobalt::Lang provides language set loading facilities to
214 L<Bot::Cobalt> and extensions.
215  
216 This is primarily used by L<Bot::Cobalt::Core> to feed the core
217 B<lang()> hash.
218  
219 B<new()> requires a 'lang' option and either a 'lang_dir' or
220 'absolute_path' -- if an absolute path is not specified, the named
221 'lang' is (attempted to be) loaded from the specified 'lang_dir' with an
222 extension of ".yml".
223  
224 The 'use_core' option will load the built-in language set.
225 'use_core_only' will not attempt to load anything except the built-in
226 set.
227  
228 If the load fails, an exception is thrown.
229  
230 =head2 rpls
231  
232 The B<rpls> attribute accesses the loaded RPL
233 L<List::Objects::WithUtils::Hash>:
234  
235 my $this_str = $language->rpls->get($rpl) // "Missing RPL $rpl";
236  
237 =head2 spec
238  
239 The B<spec> attribute returns the SPEC: definition for the loaded
240 language set.
241  
242 =head1 AUTHOR
243  
244 Jon Portnoy <avenj@cobaltirc.org>
245  
246 =cut
« blib/lib/Bot/Cobalt/Core/Role/IRC.pm ↑ Summary blib/lib/Bot/Cobalt/Core/ContextMeta.pm »