Language.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 94.4% 85 / 90 CC 6 SCAR 3.5
CC
6
Cov
96%
__ANON__
2.1
__ANON__
2.0
__ANON__
2.0
CRAP 1.4
↑ Summary Language/Fast.pm »
1 package MooseX::Types::Locale::Language;
2  
3  
4 # ****************************************************************
5 # general dependency(-ies)
6 # ****************************************************************
7  
8 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # MooseX::Types turns strict/warnings pragmas on,
10 # however, kwalitee scorer can not detect such mechanism.
11 # (Perl::Critic can it, with equivalent_modules parameter)
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Locale::Language;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use MooseX::Types::Moose qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 Str
18 1 );
19 1 ▶use MooseX::Types (
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 -declare => [qw(
21 LanguageCode
22 Alpha2Language
23 BibliographicLanguage
24 Alpha3Language
25 TerminologicLanguage
26 LanguageName
27 )],
28 1 );
29  
30  
31 # ****************************************************************
32 # namespace clearer
33 # ****************************************************************
34  
35 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 # ****************************************************************
39 # public class variable(s)
40 # ****************************************************************
41  
42 1 our $VERSION = "0.07";
43  
44  
45 # ****************************************************************
46 # private class variable(s)
47 # ****************************************************************
48  
49 # Because language2code($_) cannot coerce 'japanese' to 'Japanese'.
50 1 my %alpha2;
51 1 @alpha2{ all_language_codes() } = ();
52  
53 1 my %bibliographic;
54 1 @bibliographic{ all_language_codes(LOCALE_LANG_ALPHA_3) } = ();
55  
56 1 my %terminologic;
57 1 @terminologic{ all_language_codes(LOCALE_LANG_TERM) } = ();
58  
59 # Because code2language($_) cannot coerce 'JA' to 'ja'.
60 1 my %name;
61 1 @name{ all_language_names() } = ();
62  
63  
64 # ****************************************************************
65 # subtype(s) and coercion(s)
66 # ****************************************************************
67  
68 # ----------------------------------------------------------------
69 # language code as defined in ISO 639-1 (alpha-2)
70 # ----------------------------------------------------------------
71 1 foreach my $subtype (LanguageCode, Alpha2Language) {
72 subtype $subtype,
73 as Str,
74 12 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 12 exists $alpha2{$_};
76 },
77 4 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
78 4 ▶ sprintf 'Validation failed for code failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 2 4
79 .'because specified language code does not exist '
80 . 'in ISO 639-1',
81 defined $_ ? $_ : q{};
82 2 };
83  
84 coerce $subtype,
85 from Str,
86 4 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 # - Converts 'EN' into 'en'.
88 # - ISO 639 recommended lower-case.
89 4 return lc $_;
90 2 };
91 }
92  
93 # ----------------------------------------------------------------
94 # language code as defined in ISO 639-2 (alpha-3 bibliographic)
95 # ----------------------------------------------------------------
96 1 foreach my $subtype (Alpha3Language, BibliographicLanguage) {
97 subtype $subtype,
98 as Str,
99 12 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 12 exists $bibliographic{$_};
101 },
102 4 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
103 4 ▶ sprintf 'Validation failed for code failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 2 4
104 . 'because specified language code does not exist '
105 . 'in ISO 639-2 (bibliographic)',
106 defined $_ ? $_ : q{};
107 2 };
108  
109 coerce $subtype,
110 from Str,
111 4 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 # Converts 'ENG' into 'eng'.
113 4 return lc $_;
114 2 };
115 }
116  
117 # ----------------------------------------------------------------
118 # language code as defined in ISO 639-2 (alpha-3 terminologic)
119 # ----------------------------------------------------------------
120 subtype TerminologicLanguage,
121 as Str,
122 6 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 6 exists $terminologic{$_};
124 },
125 2 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
126 2 ▶ sprintf 'Validation failed for code failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
127 . 'because specified language code does not exist '
128 . 'in ISO 639-2 (terminologic)',
129 defined $_ ? $_ : q{};
130 1 };
131  
132 coerce TerminologicLanguage,
133 from Str,
134 2 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 # Converts 'ENG' into 'eng'.
136 2 return lc $_;
137 1 };
138  
139 # ----------------------------------------------------------------
140 # language name as defined in ISO 639
141 # ----------------------------------------------------------------
142 subtype LanguageName,
143 as Str,
144 6 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 6 exists $name{$_};
146 },
147 2 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
148 2 ▶ sprintf 'Validation failed for name failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
149 . 'because specified language name does not exist '
150 . 'in ISO 639',
151 defined $_ ? $_ : q{};
152 1 };
153  
154 coerce LanguageName,
155 from Str,
156 2 ▶ via {
Subroutine __ANON__: called CC=2 SCAR=7.2
157 # - Converts 'eNgLiSh' into 'English'.
158 # - Cannot use 'ucfirst lc', because there is 'Rhaeto-Romance'.
159 # - In case of invalid name, returns original $_
160 # to use it in exception message.
161 2 ▶ return code2language( language2code($_) ) || $_;
Condition66%2/3
code2language(language2code($_)) || $_
l!l&&r!l&&!r
110
MC/DC0%0/2
code2language(language2code($_)) || $_
code2language(language2code($_)) $_
Truth table66%2/3
A || B
A= code2language(language2code($_))B= $_
ABresult
1X1
011
000
162 1 };
163  
164  
165 # ****************************************************************
166 # optionally add Getopt option type
167 # ****************************************************************
168  
169 1 eval { require MooseX::Getopt; };
170 1 ▶if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 1 1
171 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
172 0 for (LanguageCode, Alpha2Language, LanguageName);
173 }
174  
175  
176 # ****************************************************************
177 # return true
178 # ****************************************************************
179  
180 1 1;
181 __END__
↑ Summary Language/Fast.pm »