CalendarMonth/Locale.pm

filter: Statementstmt 65.5% 118 / 180 Branchbran 53.3% 32 / 60 Conditioncond 33.3% 5 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 71.8% 23 / 32 Podpod 100% 23 / 23 totaltotal 63.3% 202 / 319 CC 43 SCAR 22.2
CC
43
Cov
61%
minmatch_hash
42.0
daynums
20.0
_daymatch
20.0
CRAP 9.2
« CalendarMonth.pm ↑ Summary CalendarMonth/DateTool.pm »
1 package HTML::CalendarMonth::Locale;
2 {
3 10 $HTML::CalendarMonth::Locale::VERSION = '2.00';
4 }
5  
6 # Front end class around DateTime::Locale. In addition to providing
7 # access to the DT::Locale class and locale-specific instance, this
8 # class prepares some other hashes and lookups utilized by
9 # HTML::CalendarMonth.
10  
11 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 ▶use DateTime::Locale 0.45;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 19 ▶sub _locale_version { $DateTime::Locale::VERSION }
Subroutine _locale_version: called CC=1 SCAR=0.0
18  
19 10 my($CODE_METHOD, $CODES_METHOD);
20 10 ▶if (_locale_version() > 0.92) {
Branch: 1 branch
BranchTrueFalseTotal
if (_locale_version() > 0.92) { } 10 0 10
21 10 $CODE_METHOD = "code";
22 10 $CODES_METHOD = "codes";
23 }
24 else {
25 0 $CODE_METHOD = "id";
26 0 $CODES_METHOD = "ids";
27 }
28  
29 10 my %Register;
30  
31 sub new {
32 309 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=20.4
Pod documented
33 309 my $self = {};
34 309 bless $self, $class;
35 309 my %parms = @_;
36 # id is for backwards compatibility
37 my $code = $parms{code} || $parms{id}
38 309 ▶ or croak "Locale code required (eg 'en-US')\n";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $parms{"code"} || $parms{"id"} or croak("Locale code required (eg 'en-US')\n") 0 309 309
Condition33%1/3
$parms{'code'} || $parms{'id'}
l!l&&r!l&&!r
03090
MC/DC0%0/2
$parms{'code'} || $parms{'id'}
$parms{'code'} $parms{'id'}
Truth table33%1/3
A || B
A= $parms{'code'}B= $parms{'id'}
ABresult
1X1
011
000
39 309 ▶ $self->{full_days} = defined $parms{full_days} ? $parms{full_days} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $parms{'full_days'} ? : 309 0 309
40 309 ▶ $self->{full_months} = defined $parms{full_months} ? $parms{full_months} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $parms{'full_months'} ? : 309 0 309
41 # returned code might be different from given code
42 309 ▶ unless ($Register{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Register{$code}) 10 299 309
43 10 ▶ my $dtl = $self->locale->load($code)
Branch: 1 branch
BranchTrueFalseTotal
my $dtl = $self->locale->load($code) or croak("Problem loading locale '${code}'") 0 10 10
44 or croak "Problem loading locale '$code'";
45 10 $Register{$code} = $Register{$dtl->$CODE_METHOD} = { loc => $dtl };
46 }
47 309 $self->{code} = $Register{$code}{loc}->$CODE_METHOD;
48 309 $self;
49 }
50  
51 11 ▶sub locale { 'DateTime::Locale' }
Subroutine locale: called CC=1 SCAR=0.0
Pod documented
52  
53 35 ▶sub loc { $Register{shift->code}{loc} }
Subroutine loc: called CC=1 SCAR=0.0
Pod documented
54  
55 1 ▶sub locales { shift->locale->$CODES_METHOD }
Subroutine locales: called CC=1 SCAR=0.0
Pod documented
56  
57 2834 ▶sub code { shift->{code} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
58 10 *id = *code;
59  
60 10 ▶sub full_days { shift->{full_days} }
Subroutine full_days: called CC=1 SCAR=0.0
Pod documented
61 10 ▶sub full_months { shift->{full_months} }
Subroutine full_months: called CC=1 SCAR=0.0
Pod documented
62  
63 7 ▶sub first_day_of_week { shift->loc->first_day_of_week % 7 }
Subroutine first_day_of_week: called CC=1 SCAR=0.0
Pod documented
64  
65 sub days {
66 624 ▶ my $self = shift;
Subroutine days: called CC=4 SCAR=13.9
Pod documented
67 624 my $code = $self->code;
68 624 ▶ unless ($Register{$code}{days}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Register{$code}{"days"}) 10 614 624
69 10 ▶ my $method = $self->full_days ? 'day_stand_alone_wide'
Branch: 1 branch
BranchTrueFalseTotal
$self->full_days ? : 1 9 10
70 : 'day_stand_alone_abbreviated';
71 # adjust to H::CM standard expectation, 1st day Sun
72 # Sunday is first, regardless of what the calendar considers to be
73 # the first day of the week
74 10 my @days = @{$self->loc->$method};
75 10 unshift(@days, pop @days);
76 10 $Register{$code}{days} = \@days;
77 }
78 624 ▶ wantarray ? @{$Register{$code}{days}} : $Register{$code}{days};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 317 307 624
79 }
80  
81 sub narrow_days {
82 6 ▶ my $self = shift;
Subroutine narrow_days: called CC=3 SCAR=11.0
Pod documented
83 6 my $code = $self->code;
84 6 ▶ unless ($Register{$code}{narrow_days}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Register{$code}{"narrow_days"}) 1 5 6
85 # Sunday is first, regardless of what the calendar considers to be
86 # the first day of the week
87 1 my @days = @{ $self->loc->day_stand_alone_narrow };
88 1 unshift(@days, pop @days);
89 1 $Register{$code}{narrow_days} = \@days;
90 }
91 6 wantarray ? @{$Register{$code}{narrow_days}}
92 6 ▶ : $Register{$code}{narrow_days};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
93 }
94  
95 sub months {
96 327 ▶ my $self = shift;
Subroutine months: called CC=4 SCAR=13.9
Pod documented
97 327 my $code = $self->code;
98 327 ▶ unless ($Register{$code}{months}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Register{$code}{"months"}) 10 317 327
99 10 ▶ my $method = $self->full_months > 0 ? 'month_stand_alone_wide'
Branch: 1 branch
BranchTrueFalseTotal
$self->full_months > 0 ? : 10 0 10
100 : 'month_stand_alone_abbreviated';
101 10 $Register{$code}{months} = [@{$self->loc->$method}];
102 }
103 327 ▶ wantarray ? @{$Register{$code}{months}} : $Register{$code}{months};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 317 327
104 }
105  
106 sub narrow_months {
107 0 ▶ my $self = shift;
Subroutine narrow_months: not called CC=3 SCAR=24.8
Pod documented
108 0 my $code = $self->code;
109 $Register{$code}{narrow_months}
110 0 ▶ ||= [@{$self->loc->month_stand_alone_narrow}];
Condition0%0/2
$Register{$code}{"narrow_months"} ||= [@{$self->loc->month_stand_alone_narrow;}]
l!l
00
MC/DC0%0/1
$Register{$code}{"narrow_months"} ||= [@{$self->loc->month_stand_alone_narrow;}]
$Register{$code}{"narrow_months"}
Truth table0%0/2
A
A= $Register{$code}{"narrow_months"}
Aresult
11
00
111 0 wantarray ? @{$Register{$code}{narrow_months}}
112 0 ▶ : $Register{$code}{narrow_months};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
113 }
114  
115 sub days_minmatch {
116 0 ▶ my $self = shift;
Subroutine days_minmatch: not called CC=2 SCAR=17.9
Pod documented
117 $Register{$self->code}{days_mm}
118 0 ▶ ||= $self->lc_minmatch_hash($self->days);
Condition0%0/3
$Register{$self->code}{"days_mm"} ||= $self->lc_minmatch_hash($self->days)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Register{$self->code}{"days_mm"} ||= $self->lc_minmatch_hash($self->days)
$Register{$self->code}{"days_mm"} $self->lc_minmatch_hash($self->days)
Truth table0%0/3
A ||= B
A= $Register{$self->code}{"days_mm"}B= $self->lc_minmatch_hash($self->days)
ABresult
1X1
011
000
119 }
120 10 *minmatch = \&days_minmatch;
121  
122 sub _days_minmatch_pattern {
123 0 ▶ my $dmm = shift->days_minmatch;
Subroutine _days_minmatch_pattern: not called CC=1 SCAR=6.9
124 0 join('|', sort keys %$dmm);
125 }
126 10 *minmatch_pattern = \&_days_minmatch_pattern;
127  
128 sub months_minmatch {
129 1228 ▶ my $self = shift;
Subroutine months_minmatch: called CC=2 SCAR=7.1
Pod documented
130 $Register{$self->code}{months_mm}
131 1228 ▶ ||= $self->lc_minmatch_hash($self->months);
Condition66%2/3
$Register{$self->code}{"months_mm"} ||= $self->lc_minmatch_hash($self->months)
l!l&&r!l&&!r
1218100
MC/DC0%0/2
$Register{$self->code}{"months_mm"} ||= $self->lc_minmatch_hash($self->months)
$Register{$self->code}{"months_mm"} $self->lc_minmatch_hash($self->months)
Truth table66%2/3
A ||= B
A= $Register{$self->code}{"months_mm"}B= $self->lc_minmatch_hash($self->months)
ABresult
1X1
011
000
132 }
133  
134 sub _months_minmatch_pattern {
135 614 ▶ my $mmm = shift->months_minmatch;
Subroutine _months_minmatch_pattern: called CC=1 SCAR=0.0
136 614 join('|', sort keys %$mmm);
137 }
138  
139 sub daynums {
140 0 ▶ my $self = shift;
Subroutine daynums: not called CC=4 SCAR=30.0
Pod documented
141 0 my $code = $self->code;
142 0 ▶ unless ($Register{$code}{daynum}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Register{$code}{"daynum"}) 0 0 0
143 0 my %daynum;
144 0 my $days = $self->days;
145 0 $daynum{$days->[$_]} = $_ foreach 0 .. $#$days;
146 0 $Register{$code}{daynum} = \%daynum;
147 }
148 0 wantarray ? %{$Register{$code}{daynum}}
149 0 ▶ : $Register{$code}{daynum};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
150 }
151  
152 sub _daymatch {
153 0 ▶ my($self, $day) = @_;
Subroutine _daymatch: not called CC=4 SCAR=30.0
154 0 ▶ return unless defined $day;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $day 0 0 0
155 0 ▶ if ($day =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day =~ /^\d+$/) 0 0 0
156 0 $day %= 7;
157 0 return($day, $self->days->[$day]);
158 }
159 0 my $p = $self->_days_minmatch_pattern;
160 0 ▶ if ($day =~ /^($p)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day =~ /^($p)/i) 0 0 0
161 0 $day = $self->days_minmatch->{lc $1};
162 0 return($self->daynums->{$day}, $day);
163 }
164 0 return ();
165 }
166  
167 0 ▶sub daynum { (shift->_daymatch(@_))[0] }
Subroutine daynum: not called CC=1 SCAR=6.9
Pod documented
168 0 ▶sub dayname { (shift->_daymatch(@_))[1] }
Subroutine dayname: not called CC=1 SCAR=6.9
Pod documented
169  
170 sub monthnums {
171 614 ▶ my $self = shift;
Subroutine monthnums: called CC=4 SCAR=14.0
Pod documented
172 614 my $code = $self->code;
173 614 ▶ unless ($Register{$code}{monthnum}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Register{$code}{"monthnum"}) 10 604 614
174 10 my %monthnum;
175 10 my $months = $self->months;
176 10 $monthnum{$months->[$_]} = $_ foreach 0 .. $#$months;
177 10 $Register{$code}{monthnum} = \%monthnum;
178 }
179 0 wantarray ? %{$Register{$code}{monthnum}}
180 614 ▶ : $Register{$code}{monthnum};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 614 614
181 }
182  
183 sub _monthmatch {
184 921 ▶ my($self, $mon) = @_;
Subroutine _monthmatch: called CC=4 SCAR=14.1
185 921 ▶ return unless defined $mon;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mon 0 921 921
186 921 ▶ if ($mon =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon =~ /^\d+$/) 307 614 921
187 307 $mon %= 12;
188 307 return($mon, $self->months->[$mon]);
189 }
190 614 my $p = $self->_months_minmatch_pattern;
191 614 ▶ if ($mon =~ /^($p)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon =~ /^($p)/i) 614 0 614
192 614 $mon = $self->months_minmatch->{lc $1};
193 614 return($self->monthnums->{$mon}, $mon);
194 }
195 0 return ();
196 }
197  
198 614 ▶sub monthnum { (shift->_monthmatch(@_))[0] }
Subroutine monthnum: called CC=1 SCAR=0.0
Pod documented
199 307 ▶sub monthname { (shift->_monthmatch(@_))[1] }
Subroutine monthname: called CC=1 SCAR=0.0
Pod documented
200  
201 ###
202  
203 sub locale_map {
204 0 ▶ my $self = shift;
Subroutine locale_map: not called CC=3 SCAR=24.8
Pod documented
205 0 my %map;
206 0 foreach my $code ($self->locales) {
207 0 $map{$code} = $self->locale->load($code)->name;
208 }
209 0 ▶ wantarray ? %map : \%map;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
210 }
211  
212 ###
213  
214 sub lc_minmatch_hash {
215 # given a list, provide a reverse lookup of case-insensitive minimal
216 # values for each label in the list
217 10 ▶ my $whatever = shift;
Subroutine lc_minmatch_hash: called CC=6 SCAR=17.9
Pod documented
218 10 my @orig_labels = @_;
219 10 my @labels = map { lc $_ } @orig_labels;
220 10 my $cc = 1;
221 10 my %minmatch;
222 10 ▶ while (@labels) {
Branch: 1 branch
BranchTrueFalseTotal
if (@labels) 31 10 41
223 31 my %scratch;
224 31 foreach my $i (0 .. $#labels) {
225 218 my $str = $labels[$i];
226 218 my $chrs = substr($str, 0, $cc);
227 218 ▶ $scratch{$chrs} ||= [];
Condition100%2/2
$scratch{$chrs} ||= []
l!l
53165
MC/DC100%1/1
$scratch{$chrs} ||= []
$scratch{$chrs}
Truth table100%2/2
A
A= $scratch{$chrs}
Aresult
11
00
228 218 push(@{$scratch{$chrs}}, $i);
229 }
230 31 my @keep_i;
231 31 foreach (keys %scratch) {
232 165 ▶ if (@{$scratch{$_}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$scratch{$_};} == 1) { } 120 45 165
233 120 $minmatch{$_} = $orig_labels[$scratch{$_}[0]];
234 }
235 else {
236 45 push(@keep_i, @{$scratch{$_}});
237 }
238 }
239 31 @labels = @labels[@keep_i];
240 31 @orig_labels = @orig_labels[@keep_i];
241 31 ++$cc;
242 }
243 10 \%minmatch;
244 }
245  
246 sub minmatch_hash {
247 # given a list, provide a reverse lookup of minimal values for each
248 # label in the list
249 0 ▶ my $whatever = shift;
Subroutine minmatch_hash: not called CC=6 SCAR=37.4
Pod documented
250 0 my @labels = @_;
251 0 my $cc = 1;
252 0 my %minmatch;
253 0 ▶ while (@labels) {
Branch: 1 branch
BranchTrueFalseTotal
if (@labels) 0 0 0
254 0 my %scratch;
255 0 foreach my $i (0 .. $#labels) {
256 0 my $str = $labels[$i];
257 0 my $chrs = substr($str, 0, $cc);
258 0 ▶ $scratch{$chrs} ||= [];
Condition0%0/2
$scratch{$chrs} ||= []
l!l
00
MC/DC0%0/1
$scratch{$chrs} ||= []
$scratch{$chrs}
Truth table0%0/2
A
A= $scratch{$chrs}
Aresult
11
00
259 0 push(@{$scratch{$chrs}}, $i);
260 }
261 0 my @keep_i;
262 0 foreach (keys %scratch) {
263 0 ▶ if (@{$scratch{$_}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$scratch{$_};} == 1) { } 0 0 0
264 0 $minmatch{$_} = $labels[$scratch{$_}[0]];
265 }
266 else {
267 0 push(@keep_i, @{$scratch{$_}});
268 }
269 }
270 0 @labels = @labels[@keep_i];
271 0 ++$cc;
272 }
273 0 \%minmatch;
274 }
275  
276 10 1;
277  
278 __END__
279  
280 =head1 NAME
281  
282 HTML::CalendarMonth::Locale - Front end class for DateTime::Locale
283  
284 =head1 SYNOPSIS
285  
286 use HTML::CalendarMonth::Locale;
287  
288 my $loc = HTML::CalendarMonth::Locale->new( code => 'en-US' );
289  
290 # list of days of the week for locale
291 my @days = $loc->days;
292  
293 # list of months of the year for locale
294 my @months = $loc->months;
295  
296 # the name of the current locale, as supplied the code parameter to
297 # new()
298 my $locale_name = $loc->code;
299  
300 # the actual DateTime::Locale object
301 my $loc = $loc->loc;
302  
303 1;
304  
305 =head1 DESCRIPTION
306  
307 HTML::CalendarMonth utilizes the powerful locale capabilities of
308 DateTime::Locale for rendering its calendars. The default locale is
309 'en-US' but many others are available. To see this list, invoke the
310 class method HTML::CalendarMonth::Locale->locales() which in turn
311 invokes DateTime::Locale::codes().
312  
313 This module is mostly intended for internal usage within
314 HTML::CalendarMonth, but some of its functionality may be of use for
315 developers:
316  
317 =head1 METHODS
318  
319 =over
320  
321 =item new()
322  
323 Constructor. Takes the following parameters:
324  
325 =over
326  
327 =item code
328  
329 Locale code, e.g. 'en-US'.
330  
331 =item full_days
332  
333 Specifies whether full day names or their abbreviations are desired.
334 Default 0, use abbreviated days.
335  
336 =item full_months
337  
338 Specifies whether full month names or their abbreviations are desired.
339 Default 1, use full months.
340  
341 =back
342  
343 =item code()
344  
345 Returns the locale code used during object construction.
346  
347 =item locale()
348  
349 Accessor method for the DateTime::Locale class, which in turn offers
350 several class methods of specific interest. See L<DateTime::Locale>.
351  
352 =item locale_map()
353  
354 Returns a hash of all available locales, mapping their code to their
355 full name.
356  
357 =item loc()
358  
359 Accessor method for the DateTime::Locale instance as specified by C<code>.
360 See L<DateTime::Locale>.
361  
362 =item locales()
363  
364 Lists all available locale codes. Equivalent to locale()->codes(), or
365 DateTime::Locale->codes().
366  
367 =item days()
368  
369 Returns a list of days of the week, Sunday first. These are the actual
370 unique day strings used for rendering calendars, so depending on which
371 attributes were provided to C<new()>, this list will either be
372 abbreviations or full names. The default uses abbreviated day names.
373 Returns a list in list context or an array ref in scalar context.
374  
375 =item narrow_days()
376  
377 Returns a list of short day abbreviations, beginning with Sunday. The
378 narrow abbreviations are not guaranteed to be unique (i.e. 'S' for both
379 Sat and Sun).
380  
381 =item days_minmatch()
382  
383 Provides a hash reference containing minimal case-insensitive match
384 strings for each day of the week, e.g., 'sa' for Saturday, 'm' for
385 Monday, etc.
386  
387 =item months()
388  
389 Returns a list of months of the year, beginning with January. Depending
390 on which attributes were provided to C<new()>, this list will either be
391 full names or abbreviations. The default uses full names. Returns a list
392 in list context or an array ref in scalar context.
393  
394 =item narrow_months()
395  
396 Returns a list of short month abbreviations, beginning with January. The
397 narrow abbreviations are not guaranteed to be unique.
398  
399 =item months_minmatch()
400  
401 Provides a hash reference containing minimal case-insensitive match
402 strings for each month of the year, e.g., 'n' for November, 'ja' for
403 January, 'jul' for July, 'jun' for June, etc.
404  
405 =item daynums()
406  
407 Provides a hash reference containing day of week indices for each fully
408 qualified day name as returned by days().
409  
410 =item daynum($day)
411  
412 Provides the day of week index for a particular day name.
413  
414 =item dayname($day)
415  
416 Provides the fully qualified day name for a given string or day index.
417  
418 =item monthnums()
419  
420 Provides a hash reference containing month of year indices for each
421 fully qualified month name as returned by months().
422  
423 =item monthnum($month)
424  
425 Provides the month of year index for a particular month name.
426  
427 =item monthname($month)
428  
429 Provides the month name for a given string or month index.
430  
431 =item minmatch_hash(@list)
432  
433 This is the method used to generate the case-insensitive minimal match
434 hash referenced above. Given an arbitrary list, a hash reference will
435 be returned with minimal match strings as keys and the original strings
436 as values.
437  
438 =item lc_minmatch_hash(@list)
439  
440 Same as minmatch_hash, except keys are forced to lower case.
441  
442 =item first_day_of_week()
443  
444 Returns a number from 0 to 6 representing the first day of the week for
445 this locale, where 0 represents Sunday.
446  
447 =back
448  
449 =head1 AUTHOR
450  
451 Matthew P. Sisk, E<lt>F<sisk@mojotoad.com>E<gt>
452  
453 =head1 COPYRIGHT
454  
455 Copyright (c) 2010-2015 Matthew P. Sisk. All rights reserved. All wrongs
456 revenged. This program is free software; you can redistribute it and/or
457 modify it under the same terms as Perl itself.
458  
459 =head1 SEE ALSO
460  
461 HTML::CalendarMonth(3), DateTime::Locale(3)
462  
463 =for Pod::Coverage minmatch minmatch_pattern id
« CalendarMonth.pm ↑ Summary CalendarMonth/DateTool.pm »