blib/lib/CLDR/Number/Role/Base.pm

filter: Statementstmt 98.9% 90 / 91 Branchbran 86.6% 26 / 30 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 95.3% 144 / 151 CC 22 SCAR 11.2
CC
22
Cov
95%
_get_data
5.0
_trigger_locale
4.0
_split_locale
4.0
CRAP 3.1
« blib/lib/CLDR/Number/Format/Currency.pm ↑ Summary blib/lib/CLDR/Number/Format/Percent.pm »
1 package CLDR::Number::Role::Base;
2  
3 14 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use CLDR::Number::Data::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # This role does not have a publicly supported interface and may change in
9 # backward incompatible ways in the future. Please use one of the documented
10 # classes instead.
11  
12 14 our $VERSION = '0.00_03';
13  
14 14 requires qw( BUILD );
15  
16 14 has version => (
17 is => 'ro',
18 default => $VERSION,
19 );
20  
21 14 has cldr_version => (
22 is => 'ro',
23 default => 24,
24 );
25  
26 14 has locale => (
27 is => 'rw',
28 trigger => 1,
29 );
30  
31 has default_locale => (
32 is => 'ro',
33 coerce => sub {
34 2 ▶ my ($locale) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.2
35  
36 2 ▶ if (!defined $locale) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $locale) { } 0 2 2
elsif (not exists $CLDR::Number::Data::Base::DATA->{$locale}) { } 1 1 2
37 0 carp 'default_locale is not defined';
38 }
39 elsif (!exists $CLDR::Number::Data::Base::DATA->{$locale}) {
40 1 carp "default_locale '$locale' is unknown";
41 }
42 else {
43 1 return $locale;
44 }
45  
46 1 return;
47 },
48 14 );
49  
50 # TODO: length NYI
51 14 has length => (
52 is => 'rw',
53 );
54  
55 14 has decimal_sign => (
56 is => 'rw',
57 );
58  
59 14 has group_sign => (
60 is => 'rw',
61 );
62  
63 14 has plus_sign => (
64 is => 'rw',
65 );
66  
67 14 has minus_sign => (
68 is => 'rw',
69 );
70  
71 has _locale_inheritance => (
72 is => 'rw',
73 54 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 14 );
75  
76 14 has _init_args => (
77 is => 'rw',
78 );
79  
80 around BUILDARGS => sub {
81 54 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
82  
83 54 ▶ return $class->$orig(@args) if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 54 54
84 54 return $class->$orig(@args, _init_args => {@args});
85 14 };
86  
87 before BUILD => sub {
88 54 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
89  
90 54 ▶ return if $self->_has_init_arg('locale');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg("locale") 38 16 54
91  
92 16 $self->_trigger_locale;
93 14 };
94  
95 after BUILD => sub {
96 54 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
97  
98 54 $self->_init_args({});
99 14 };
100  
101 sub _has_init_arg {
102 1356 ▶ my ($self, $arg) = @_;
Subroutine _has_init_arg: called CC=2 SCAR=7.1
103  
104 1356 ▶ return unless $self->_init_args;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_init_args 0 1356 1356
105 1356 return exists $self->_init_args->{$arg};
106 }
107  
108 sub _set_unless_init_arg {
109 572 ▶ my ($self, $attribute, $value) = @_;
Subroutine _set_unless_init_arg: called CC=2 SCAR=6.9
110  
111 572 ▶ return if $self->_has_init_arg($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg($attribute) 10 562 572
112  
113 562 $self->$attribute($value);
114 }
115  
116 sub _build_signs {
117 116 ▶ my ($self, @signs) = @_;
Subroutine _build_signs: called CC=3 SCAR=11.0
118  
119 116 for my $sign (@signs) {
120 404 my $attribute = $sign;
121  
122 404 ▶ next if $self->_has_init_arg($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_init_arg($attribute) 0 404 404
123  
124 404 $sign =~ s{ _sign $ }{}x;
125  
126 404 $self->$attribute($self->_get_data(symbols => $sign));
127 }
128 }
129  
130 sub _trigger_locale {
131 86 ▶ my ($self, $locale) = @_;
Subroutine _trigger_locale: called CC=4 SCAR=13.9
132 86 my ($lang, $script, $region, $ext) = _split_locale($locale);
133  
134 86 ▶ if ($lang && exists $CLDR::Number::Data::Base::DATA->{$lang}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lang and exists $CLDR::Number::Data::Base::DATA->{$lang}) { } 59 27 86
elsif ($self->default_locale) { } 2 25 27
Condition100%3/3
$lang and exists $CLDR::Number::Data::Base::DATA->{$lang}
!ll&&!rl&&r
24359
MC/DC100%2/2
$lang and exists $CLDR::Number::Data::Base::DATA->{$lang}
$lang exists $CLDR::Number::Data::Base::DATA->{$lang}
Truth table100%3/3
A and B
A= $langB= exists $CLDR::Number::Data::Base::DATA->{$lang}
ABresult
0X0
100
111
135 59 $self->_locale_inheritance(
136 _build_inheritance($lang, $script, $region, $ext)
137 );
138 59 $locale = $self->_locale_inheritance->[0];
139 }
140 elsif ($self->default_locale) {
141 2 $locale = $self->default_locale;
142 2 $self->_locale_inheritance(
143 _build_inheritance( _split_locale($locale) )
144 );
145 }
146 else {
147 25 $locale = 'root';
148 25 $self->_locale_inheritance( [$locale] );
149 }
150  
151 86 $self->{locale} = $locale;
152  
153 86 $self->_build_signs(qw{ decimal_sign group_sign plus_sign minus_sign });
154 }
155  
156 sub _split_locale {
157 88 ▶ my ($locale) = @_;
Subroutine _split_locale: called CC=4 SCAR=13.9
158  
159 88 ▶ return unless defined $locale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $locale 16 72 88
160  
161 72 $locale = lc $locale;
162 72 $locale =~ tr{_}{-};
163  
164 72 my ($lang, $script, $region, $ext) = $locale =~ m{ ^
165 ( [a-z]{2,3} ) # language
166 (?: - ( [a-z]{4} ) )? # script
167 (?: - ( [a-z]{2} | [0-9]{3} ) )? # country or region
168 (?: - ( u- .+ ) )? # extension
169 -? # trailing separator
170 $ }xi;
171  
172 72 ▶ $script = ucfirst $script if $script;
Branch: 1 branch
BranchTrueFalseTotal
if $script 9 63 72
173 72 ▶ $region = uc $region if $region;
Branch: 1 branch
BranchTrueFalseTotal
if $region 17 55 72
174  
175 72 return $lang, $script, $region, $ext;
176 }
177  
178 sub _build_inheritance {
179 61 ▶ my ($lang, $script, $region, $ext) = @_;
Subroutine _build_inheritance: called CC=4 SCAR=13.9
180 61 my @tree;
181  
182 61 for my $subtags (
183 [$lang, $region, $ext],
184 [$lang, $script, $region],
185 [$lang, $script],
186 [$lang, $region],
187 [$lang],
188 ) {
189 305 ▶ next if grep { !$_ } @$subtags;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_;} @$subtags 210 95 305
190 95 my $locale = join '-', @$subtags;
191 95 ▶ next if !exists $CLDR::Number::Data::Base::DATA->{$locale};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CLDR::Number::Data::Base::DATA->{$locale} 13 82 95
192 82 push @tree, $locale;
193 }
194 61 push @tree, 'root';
195  
196 61 return \@tree;
197 }
198  
199 sub _get_data {
200 464 ▶ my ($self, $type, $key) = @_;
Subroutine _get_data: called CC=5 SCAR=16.1
201 464 my $data = $CLDR::Number::Data::Base::DATA;
202  
203 464 for my $locale (@{$self->_locale_inheritance}) {
204 return $data->{$locale}{$type}{$key}
205 if exists $data->{$locale}
206 && exists $data->{$locale}{$type}
207 539 ▶ && exists $data->{$locale}{$type}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$locale} and exists $data->{$locale}{$type} and exists $data->{$locale}{$type}{$key} 452 87 539
Condition83%5/6
exists $data->{$locale} and exists $data->{$locale}{$type} and exists $data->{$locale}{$type}{$key}
!ll&&!rl&&r
4740452
exists $data->{$locale} and exists $data->{$locale}{$type}
!ll&&!rl&&r
047492
MC/DC66%2/3
exists $data->{$locale} and exists $data->{$locale}{$type} and exists $data->{$locale}{$type}{$key}
exists $data->{$locale} exists $data->{$locale}{$type} exists $data->{$locale}{$type}{$key}
Truth table75%3/4
A and B and C
A= exists $data->{$locale}B= exists $data->{$locale}{$type}C= exists $data->{$locale}{$type}{$key}
ABCresult
0XX0
10X0
1100
1111
208 }
209  
210 12 return undef;
211 }
212  
213 14 1;
« blib/lib/CLDR/Number/Format/Currency.pm ↑ Summary blib/lib/CLDR/Number/Format/Percent.pm »