File Coverage

blib/lib/MooseX/Types/Locale/Language.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Locale::Language;
2              
3              
4             # ****************************************************************
5             # general dependency(-ies)
6             # ****************************************************************
7              
8 1     1   804472 use 5.008_001;
  1         4  
  1         39  
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     1   5 use strict;
  1         2  
  1         30  
13 1     1   5 use warnings;
  1         6  
  1         27  
14              
15 1     1   862 use Locale::Language;
  1         301153  
  1         91  
16 1         12 use MooseX::Types::Moose qw(
17             Str
18 1     1   891 );
  1         70054  
19             use MooseX::Types (
20 1         7 -declare => [qw(
21             LanguageCode
22             Alpha2Language
23             BibliographicLanguage
24             Alpha3Language
25             TerminologicLanguage
26             LanguageName
27             )],
28 1     1   5924 );
  1         1  
29              
30              
31             # ****************************************************************
32             # namespace clearer
33             # ****************************************************************
34              
35 1     1   7190 use namespace::clean;
  1         3  
  1         9  
36              
37              
38             # ****************************************************************
39             # public class variable(s)
40             # ****************************************************************
41              
42             our $VERSION = "0.07";
43              
44              
45             # ****************************************************************
46             # private class variable(s)
47             # ****************************************************************
48              
49             # Because language2code($_) cannot coerce 'japanese' to 'Japanese'.
50             my %alpha2;
51             @alpha2{ all_language_codes() } = ();
52              
53             my %bibliographic;
54             @bibliographic{ all_language_codes(LOCALE_LANG_ALPHA_3) } = ();
55              
56             my %terminologic;
57             @terminologic{ all_language_codes(LOCALE_LANG_TERM) } = ();
58              
59             # Because code2language($_) cannot coerce 'JA' to 'ja'.
60             my %name;
61             @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             foreach my $subtype (LanguageCode, Alpha2Language) {
72             subtype $subtype,
73             as Str,
74             where {
75             exists $alpha2{$_};
76             },
77             message {
78             sprintf 'Validation failed for code failed with value (%s) '
79             .'because specified language code does not exist '
80             . 'in ISO 639-1',
81             defined $_ ? $_ : q{};
82             };
83              
84             coerce $subtype,
85             from Str,
86             via {
87             # - Converts 'EN' into 'en'.
88             # - ISO 639 recommended lower-case.
89             return lc $_;
90             };
91             }
92              
93             # ----------------------------------------------------------------
94             # language code as defined in ISO 639-2 (alpha-3 bibliographic)
95             # ----------------------------------------------------------------
96             foreach my $subtype (Alpha3Language, BibliographicLanguage) {
97             subtype $subtype,
98             as Str,
99             where {
100             exists $bibliographic{$_};
101             },
102             message {
103             sprintf 'Validation failed for code failed with value (%s) '
104             . 'because specified language code does not exist '
105             . 'in ISO 639-2 (bibliographic)',
106             defined $_ ? $_ : q{};
107             };
108              
109             coerce $subtype,
110             from Str,
111             via {
112             # Converts 'ENG' into 'eng'.
113             return lc $_;
114             };
115             }
116              
117             # ----------------------------------------------------------------
118             # language code as defined in ISO 639-2 (alpha-3 terminologic)
119             # ----------------------------------------------------------------
120             subtype TerminologicLanguage,
121             as Str,
122             where {
123             exists $terminologic{$_};
124             },
125             message {
126             sprintf 'Validation failed for code failed with value (%s) '
127             . 'because specified language code does not exist '
128             . 'in ISO 639-2 (terminologic)',
129             defined $_ ? $_ : q{};
130             };
131              
132             coerce TerminologicLanguage,
133             from Str,
134             via {
135             # Converts 'ENG' into 'eng'.
136             return lc $_;
137             };
138              
139             # ----------------------------------------------------------------
140             # language name as defined in ISO 639
141             # ----------------------------------------------------------------
142             subtype LanguageName,
143             as Str,
144             where {
145             exists $name{$_};
146             },
147             message {
148             sprintf 'Validation failed for name failed with value (%s) '
149             . 'because specified language name does not exist '
150             . 'in ISO 639',
151             defined $_ ? $_ : q{};
152             };
153              
154             coerce LanguageName,
155             from Str,
156             via {
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             return code2language( language2code($_) ) || $_;
162             };
163              
164              
165             # ****************************************************************
166             # optionally add Getopt option type
167             # ****************************************************************
168              
169             eval { require MooseX::Getopt; };
170             if (!$@) {
171             MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
172             for (LanguageCode, Alpha2Language, LanguageName);
173             }
174              
175              
176             # ****************************************************************
177             # return true
178             # ****************************************************************
179              
180             1;
181             __END__
182              
183              
184             # ****************************************************************
185             # POD
186             # ****************************************************************
187              
188             =pod
189              
190             =head1 NAME
191              
192             MooseX::Types::Locale::Language - Locale::Language related constraints and coercions for Moose
193              
194             =head1 VERSION
195              
196             This document describes
197             L<MooseX::Types::Locale::Language|MooseX::Types::Locale::Language>
198             version C<0.07>.
199              
200             =head1 SYNOPSIS
201              
202             {
203             package Foo;
204              
205             use Moose;
206             use MooseX::Types::Locale::Language qw(
207             LanguageCode
208             LanguageName
209             );
210              
211             has 'code'
212             => ( isa => LanguageCode, is => 'rw', coerce => 1 );
213             has 'name'
214             => ( isa => LanguageName, is => 'rw', coerce => 1 );
215              
216             __PACKAGE__->meta->make_immutable;
217             }
218              
219             my $foo = Foo->new(
220             code => 'JA',
221             name => 'JAPANESE',
222             );
223             print $foo->code; # 'ja'
224             print $foo->name; # 'Japanese'
225              
226             =head1 DESCRIPTION
227              
228             This module packages several
229             L<Moose::Util::TypeConstraints|Moose::Util::TypeConstraints> with coercions,
230             designed to work with the values of L<Locale::Language|Locale::Language>.
231              
232             =head1 CONSTRAINTS AND COERCIONS
233              
234             =over 4
235              
236             =item C<Alpha2Language>
237              
238             A subtype of C<Str>, which should be defined in language code of ISO 639-1
239             alpha-2.
240             If you turned C<coerce> on, C<Str> will be lower-case.
241             For example, C<'JA'> will convert to C<'ja'>.
242              
243             =item C<LanguageCode>
244              
245             Alias of C<Alpha2Language>.
246              
247             =item C<BibliographicLanguage>
248              
249             A subtype of C<Str>, which should be defined in language code of ISO 639-2/B
250             alpha-3.
251             If you turned C<coerce> on, C<Str> will be lower-case.
252             For example, C<'CHI'> will convert to C<'chi'>.
253              
254             =item C<Alpha3Language>
255              
256             Alias of C<BibliographicLanguage>.
257              
258             =item C<TerminologicLanguage>
259              
260             A subtype of C<Str>, which should be defined in language code of ISO 639-2/T
261             alpha-3.
262             If you turned C<coerce> on, C<Str> will be lower-case.
263             For example, C<'ZHO'> will convert to C<'zho'>.
264              
265             =item C<LanguageName>
266              
267             A subtype of C<Str>, which should be defined in ISO 639-1 language name.
268             If you turned C<coerce> on, C<Str> will be same case as canonical name.
269             For example, C<'JAPANESE'> will convert to C<'Japanese'>.
270              
271             =back
272              
273             =head1 NOTE
274              
275             =head2 Code conversion is not supported
276              
277             These coercions is not support code conversion.
278             For example, from C<Alpha2Language> to C<LanguageName>.
279              
280             has language
281             => ( is => 'rw', isa => LanguageName, coerce => 1 );
282              
283             ...
284              
285             $foo->language('en'); # does not convert to 'English'
286              
287             If you want conversion, could you implement an individual language class
288             with several attributes?
289              
290             See C</examples/complex.pl> in the distribution for more details.
291              
292             =head2 The type mapping of L<MooseX::Getopt|MooseX::Getopt>
293              
294             This module provides you the optional type mapping of
295             L<MooseX::Getopt|MooseX::Getopt>
296             when L<MooseX::Getopt|MooseX::Getopt> was installed.
297              
298             C<LanguageCode>, C<Alpha2Language> and C<LanguageName> are
299             C<String> (C<"=s">) type.
300              
301             =head1 SEE ALSO
302              
303             =over 4
304              
305             =item * L<Locale::Language|Locale::Language>
306              
307             =item * L<MooseX::Types::Locale::Language::Fast|MooseX::Types::Locale::Language::Fast>
308              
309             =item * L<MooseX::Types::Locale::Country|MooseX::Types::Locale::Country>
310              
311             =back
312              
313             =head1 INCOMPATIBILITIES
314              
315             None reported.
316              
317             =head1 TO DO
318              
319             =over 4
320              
321             =item * I may add grammatical aliases of constraints/coercions.
322             For example, C<LanguageAsAlpha2> as existent C<Alpha2Language>.
323              
324             =item * I may add namespased types.
325             For example, C<'Locale::Language::Alpha2'> as export type
326             C<Alpha2Language>.
327              
328             =back
329              
330             =head1 BUGS AND LIMITATIONS
331              
332             No bugs have been reported.
333              
334             =head2 Making suggestions and reporting bugs
335              
336             Please report any found bugs, feature requests, and ideas for improvements
337             to C<bug-moosex-types-locale-language at rt.cpan.org>,
338             or through the web interface
339             at L<http://rt.cpan.org/Public/Bug/Report.html?Queue=MooseX-Types-Locale-Language>.
340             I will be notified, and then you'll automatically be notified of progress
341             on your bugs/requests as I make changes.
342              
343             When reporting bugs, if possible,
344             please add as small a sample as you can make of the code
345             that produces the bug.
346             And of course, suggestions and patches are welcome.
347              
348             =head1 SUPPORT
349              
350             You can find documentation for this module with the C<perldoc> command.
351              
352             perldoc MooseX::Types::Locale::Language
353              
354             You can also look for information at:
355              
356             =over 4
357              
358             =item RT: CPAN's request tracker
359              
360             L<http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Locale-Language>
361              
362             =item AnnoCPAN: Annotated CPAN documentation
363              
364             L<http://annocpan.org/dist/MooseX-Types-Locale-Language>
365              
366             =item Search CPAN
367              
368             L<http://search.cpan.org/dist/MooseX-Types-Locale-Language>
369              
370             =item CPAN Ratings
371              
372             L<http://cpanratings.perl.org/dist/MooseX-Types-Locale-Language>
373              
374             =back
375              
376             =head1 VERSION CONTROL
377              
378             This module is maintained using I<git>.
379             You can get the latest version from
380             L<git://github.com/gardejo/p5-moosex-types-locale-language.git>.
381              
382             =head1 AUTHOR
383              
384             =over 4
385              
386             =item MORIYA Masaki, alias Gardejo
387              
388             C<< <moriya at cpan dot org> >>,
389             L<http://gardejo.org/>
390              
391             =back
392              
393             =head1 COPYRIGHT AND LICENSE
394              
395             Copyright (c) 2009-2010 MORIYA Masaki, alias Gardejo
396              
397             This library is free software;
398             you can redistribute it and/or modify it under the same terms as Perl itself.
399             See L<perlgpl|perlgpl> and L<perlartistic|perlartistic>.
400              
401             The full text of the license can be found in the F<LICENSE> file
402             included with this distribution.
403              
404             =cut