blib/lib/DateTime/Locale/FromData.pm

filter: Statementstmt 96.0% 120 / 125 Branchbran 83.3% 5 / 6 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.8% 33 / 38 Podpod 33.3% 8 / 24 totaltotal 82.2% 171 / 208 CC 13 SCAR 4.9
CC
13
Cov
92%
BEGIN
3.0
language_code
2.1
script_code
2.1
CRAP 1.6
« blib/lib/DateTime/Locale/Catalog.pm ↑ Summary blib/lib/DateTime/Locale/Conflicts.pm »
1 package DateTime::Locale::FromData;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use DateTime::Locale::Util qw( parse_locale_code );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Params::ValidationCompiler 0.13 qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Specio::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 our $VERSION = '1.47';
13  
14 17 my @FormatLengths;
15  
16 17 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
17 17 my @methods = qw(
18 code
19 name
20 language
21 script
22 territory
23 variant
24 native_name
25 native_language
26 native_script
27 native_territory
28 native_variant
29 am_pm_abbreviated
30 date_format_full
31 date_format_long
32 date_format_medium
33 date_format_short
34 time_format_full
35 time_format_long
36 time_format_medium
37 time_format_short
38 day_format_abbreviated
39 day_format_narrow
40 day_format_wide
41 day_stand_alone_abbreviated
42 day_stand_alone_narrow
43 day_stand_alone_wide
44 day_of_month_format_abbreviated
45 month_format_abbreviated
46 month_format_narrow
47 month_format_wide
48 month_stand_alone_abbreviated
49 month_stand_alone_narrow
50 month_stand_alone_wide
51 quarter_format_abbreviated
52 quarter_format_narrow
53 quarter_format_wide
54 quarter_stand_alone_abbreviated
55 quarter_stand_alone_narrow
56 quarter_stand_alone_wide
57 era_abbreviated
58 era_narrow
59 era_wide
60 default_date_format_length
61 default_time_format_length
62 first_day_of_week
63 version
64 glibc_datetime_format
65 glibc_date_format
66 glibc_date_1_format
67 glibc_time_format
68 glibc_time_12_format
69 );
70  
71 17 for my $meth (@methods) {
72 867 ▶ my $sub = sub { $_[0]->{$meth} };
Subroutine __ANON__: called CC=1 SCAR=0.0
73 ## no critic (TestingAndDebugging::ProhibitNoStrict)
74 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 867 *{$meth} = $sub;
76 }
77  
78 17 @FormatLengths = qw( short medium long full );
79  
80 17 for my $length (@FormatLengths) {
81 68 my $meth = 'datetime_format_' . $length;
82 68 my $key = 'computed_' . $meth;
83  
84 6 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
85 6 my $self = shift;
86  
87 6 ▶ return $self->{$key} if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 1 5 6
88  
89 5 return $self->{$key} = $self->_make_datetime_format($length);
90 68 };
91  
92 ## no critic (TestingAndDebugging::ProhibitNoStrict)
93 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 68 *{$meth} = $sub;
95 }
96 }
97  
98 972 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
99 972 my $class = shift;
100 972 my $data = shift;
101  
102 return bless {
103 972 %{$data},
104 default_date_format_length => 'medium',
105 default_time_format_length => 'medium',
106 locale_data => $data
107 }, $class;
108 }
109  
110 2 ▶sub date_format_default {
Subroutine date_format_default: called CC=1 SCAR=0.0
Pod undocumented
111 2 return $_[0]->date_format_medium;
112 }
113  
114 2 ▶sub time_format_default {
Subroutine time_format_default: called CC=1 SCAR=0.0
Pod undocumented
115 2 return $_[0]->time_format_medium;
116 }
117  
118 0 ▶sub datetime_format {
Subroutine datetime_format: not called CC=1 SCAR=6.9
Pod undocumented
119 0 return $_[0]->{datetime_format_medium};
120 }
121  
122 2 ▶sub datetime_format_default {
Subroutine datetime_format_default: called CC=1 SCAR=0.0
Pod undocumented
123 2 return $_[0]->datetime_format_medium;
124 }
125  
126 5 ▶sub _make_datetime_format {
Subroutine _make_datetime_format: called CC=1 SCAR=0.0
127 5 my $self = shift;
128 5 my $length = shift;
129  
130 5 my $dt_key = 'datetime_format_' . $length;
131 5 my $date_meth = 'date_format_' . $length;
132 5 my $time_meth = 'time_format_' . $length;
133  
134 5 my $dt_format = $self->{$dt_key};
135 5 $dt_format =~ s/\{0\}/$self->$time_meth/eg;
136 5 $dt_format =~ s/\{1\}/$self->$date_meth/eg;
137  
138 5 return $dt_format;
139 }
140  
141 17 my $length = enum( values => [qw( full long medium short )] );
142 17 my $validator = validation_for(
143 name => '_check_length_parameter',
144 name_is_optional => 1,
145 params => [ { type => $length } ],
146 );
147  
148 1 ▶sub set_default_date_format_length {
Subroutine set_default_date_format_length: called CC=1 SCAR=0.0
Pod undocumented
149 1 my $self = shift;
150 1 my ($l) = $validator->(@_);
151  
152 1 $self->{default_date_format_length} = lc $l;
153 }
154  
155 1 ▶sub set_default_time_format_length {
Subroutine set_default_time_format_length: called CC=1 SCAR=0.0
Pod undocumented
156 1 my $self = shift;
157 1 my ($l) = $validator->(@_);
158  
159 1 $self->{default_time_format_length} = lc $l;
160 }
161  
162 927 ▶sub date_formats {
Subroutine date_formats: called CC=2 SCAR=6.9
Pod undocumented
163 927 my %formats;
164 927 for my $length (@FormatLengths) {
165 3708 my $meth = 'date_format_' . $length;
166 3708 $formats{$length} = $_[0]->$meth;
167 }
168 927 return \%formats;
169 }
170  
171 927 ▶sub time_formats {
Subroutine time_formats: called CC=2 SCAR=6.9
Pod undocumented
172 927 my %formats;
173 927 for my $length (@FormatLengths) {
174 3708 my $meth = 'time_format_' . $length;
175 3708 $formats{$length} = $_[0]->$meth;
176 }
177 927 return \%formats;
178 }
179  
180 2 ▶sub available_formats {
Subroutine available_formats: called CC=2 SCAR=7.0
Pod documented
181 2 my $self = shift;
182  
183 $self->{computed_available_formats}
184 2 ▶ ||= [ sort keys %{ $self->_available_formats } ];
Condition50%1/2
$self->{"computed_available_formats"} ||= [sort(keys %{$self->_available_formats;})]
l!l
02
MC/DC0%0/1
$self->{"computed_available_formats"} ||= [sort(keys %{$self->_available_formats;})]
$self->{"computed_available_formats"}
Truth table50%1/2
A
A= $self->{"computed_available_formats"}
Aresult
11
00
185  
186 2 return @{ $self->{computed_available_formats} };
187 }
188  
189 148 ▶sub format_for {
Subroutine format_for: called CC=1 SCAR=0.0
Pod documented
190 148 my $self = shift;
191 148 my $for = shift;
192  
193 148 return $self->_available_formats->{$for};
194 }
195  
196 150 ▶sub _available_formats { $_[0]->{available_formats} }
Subroutine _available_formats: called CC=1 SCAR=0.0
197  
198 8 ▶sub prefers_24_hour_time {
Subroutine prefers_24_hour_time: called CC=2 SCAR=7.0
Pod documented
199 8 my $self = shift;
200  
201 return $self->{prefers_24_hour_time}
202 8 ▶ if exists $self->{prefers_24_hour_time};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"prefers_24_hour_time"} 0 8 8
203  
204 # This regex splits the pattern into parts, but only keeps the parts that aren't quoted. This
205 # lets us ignore literal strings in the pattern when looking for `h|K`. Without this we could
206 # match on a literal `'h'` in the pattern (which fr-CA has at the time of this writing), giving
207 # us a false positive.
208 8 my @parts = split /(?:'(?:(?:[^']|'')*)')/, $self->time_format_short;
209 8 return $self->{prefers_24_hour_time} = !( grep {/h|K/} @parts );
210 }
211  
212 2 ▶sub language_code {
Subroutine language_code: called CC=2 SCAR=7.2
Pod documented
213 2 my $self = shift;
214 return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
215 2 ▶ ->{language};
Condition50%1/2
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
l!l
02
MC/DC0%0/1
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
$self->{"parsed_code"}
Truth table50%1/2
A
A= $self->{"parsed_code"}
Aresult
11
00
216 }
217  
218 2 ▶sub script_code {
Subroutine script_code: called CC=2 SCAR=7.2
Pod documented
219 2 my $self = shift;
220 return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
221 2 ▶ ->{script};
Condition50%1/2
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
l!l
02
MC/DC0%0/1
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
$self->{"parsed_code"}
Truth table50%1/2
A
A= $self->{"parsed_code"}
Aresult
11
00
222 }
223  
224 3 ▶sub territory_code {
Subroutine territory_code: called CC=2 SCAR=7.2
Pod documented
225 3 my $self = shift;
226 return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
227 3 ▶ ->{territory};
Condition50%1/2
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
l!l
30
MC/DC0%0/1
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
$self->{"parsed_code"}
Truth table50%1/2
A
A= $self->{"parsed_code"}
Aresult
11
00
228 }
229  
230 2 ▶sub variant_code {
Subroutine variant_code: called CC=2 SCAR=7.2
Pod documented
231 2 my $self = shift;
232 return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
233 2 ▶ ->{variant};
Condition50%1/2
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
l!l
20
MC/DC0%0/1
$self->{"parsed_code"} ||= {parse_locale_code($self->code)}
$self->{"parsed_code"}
Truth table50%1/2
A
A= $self->{"parsed_code"}
Aresult
11
00
234 }
235  
236 1 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
237 1 $_[0]->code;
238 }
239  
240 0 ▶sub language_id {
Subroutine language_id: not called CC=1 SCAR=6.9
Pod undocumented
241 0 $_[0]->language_code;
242 }
243  
244 0 ▶sub script_id {
Subroutine script_id: not called CC=1 SCAR=6.9
Pod undocumented
245 0 $_[0]->script_code;
246 }
247  
248 0 ▶sub territory_id {
Subroutine territory_id: not called CC=1 SCAR=6.9
Pod undocumented
249 0 $_[0]->territory_code;
250 }
251  
252 0 ▶sub variant_id {
Subroutine variant_id: not called CC=1 SCAR=6.9
Pod undocumented
253 0 $_[0]->variant_code;
254 }
255  
256 3 ▶sub locale_data {
Subroutine locale_data: called CC=1 SCAR=0.0
Pod documented
257 3 return %{ dclone( $_[0]->{locale_data} ) };
258 }
259  
260 2 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=2 SCAR=6.9
Pod undocumented
261 2 my $self = shift;
262 2 my $cloning = shift;
263  
264 2 ▶ return if $cloning;
Branch: 1 branch
BranchTrueFalseTotal
if $cloning 1 1 2
265  
266 1 return $self->code;
267 }
268  
269 2 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
270 2 my $self = shift;
271 2 shift;
272 2 my $serialized = shift;
273  
274 2 require DateTime::Locale;
275 2 my $obj = DateTime::Locale->load($serialized);
276  
277 2 %{$self} = %{$obj};
278  
279 2 return $self;
280 }
281  
282 17 1;
283  
284 # ABSTRACT: Class for locale objects instantiated from pre-defined data
285  
286 __END__
« blib/lib/DateTime/Locale/Catalog.pm ↑ Summary blib/lib/DateTime/Locale/Conflicts.pm »