CatalystX/I18N/Role/Collate.pm

filter: Statementstmt 97.3% 37 / 38 Branchbran 50.0% 2 / 4 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 85.2% 52 / 61 CC 5 SCAR 8.5
CC
5
Cov
87%
i18n_sort
5.2
_build_i18n_collator
2.0
BEGIN
1.0
CRAP 2.3
« CatalystX/I18N/Model/DataLocalize.pm ↑ Summary CatalystX/I18N/Role/DateTime.pm »
1 # ============================================================================
2 package CatalystX::I18N::Role::Collate;
3 # ============================================================================
4  
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use CatalystX::I18N::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Unicode::Collate::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 has 'i18n_collator' => (
15 is => 'rw',
16 isa => 'Unicode::Collate',
17 lazy_build => 1,
18 builder => '_build_i18n_collator',
19 clearer => '_clear_i18n_collator',
20 );
21  
22 2 ▶sub _build_i18n_collator {
Subroutine _build_i18n_collator: called CC=2 SCAR=7.0
23 2 my ($c) = @_;
24  
25 2 my $language = $c->language;
26 2 my $config = $c->i18n_config;
27 2 ▶ $config->{collate} ||= {};
Condition50%1/2
$config->{"collate"} ||= {}
l!l
02
MC/DC0%0/1
$config->{"collate"} ||= {}
$config->{"collate"}
Truth table50%1/2
A
A= $config->{"collate"}
Aresult
11
00
28  
29 2 my $collator = Unicode::Collate::Locale->new( locale => $language, %{$config->{collate}} );
30  
31 2 return $collator;
32 }
33  
34 1 ▶sub i18n_sort {
Subroutine i18n_sort: called CC=4 SCAR=16.4
Pod documented
35 1 my ($c,@arguments) = @_;
36  
37 0 ▶ @arguments = @{$arguments[0]}
Branch: 1 branch
BranchTrueFalseTotal
if scalar @arguments == 0 and ref $arguments[0] eq "ARRAY" 0 1 1
Condition33%1/3
scalar @arguments == 0 and ref $arguments[0] eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
scalar @arguments == 0 and ref $arguments[0] eq "ARRAY"
scalar @arguments == 0 ref $arguments[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @arguments == 0B= ref $arguments[0] eq "ARRAY"
ABresult
0X0
100
111
38 if scalar(@arguments) == 0 && ref($arguments[0]) eq 'ARRAY';
39  
40 1 my @results = $c->i18n_collator->sort(@arguments);
41  
42 1 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
43 }
44  
45 34 ▶after 'set_locale' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 34 my ($c,$locale) = @_;
47 34 $c->_clear_i18n_collator();
48 4 };
49  
50 4 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 1;
52  
53 =encoding utf8
54  
55 =head1 NAME
56  
57 CatalystX::I18N::Role::Collate - Support for localised collation
58  
59 =head1 SYNOPSIS
60  
61 package MyApp::Catalyst;
62  
63 use Catalyst qw/MyPlugins
64 CatalystX::I18N::Role::Base
65 CatalystX::I18N::Role::Collate/;
66
67 package MyApp::Catalyst::Controller::Main;
68 use strict;
69 use warnings;
70 use parent qw/Catalyst::Controller/;
71
72 sub action : Local {
73 my ($self,$c) = @_;
74
75 $c->locale('de');
76 @sorted_names = $c->i18n_sort(qw/Algerien Ägypten Armenien Argentinien Äthiopien Afganistan Aserbaidschan/);
77
78 $c->stash->{names} = \@sorted_names;
79 }
80  
81 =head1 DESCRIPTION
82  
83 This role adds support for localised collation your Catalyst application.
84  
85 my @german_alphabeth = (A..Z,a..z,'ä','Ä','ü','Ü','ö','Ö','ß');
86
87 $sort = join(',',sort @german_alphabeth);
88 # $sort_no_collate is 'A,B,C,[...],Z,a,b,c,[...],z,Ä,Ö,Ü,ß,ä,ö,ü'
89
90 $sort_localised = join(',',$c->i18n_sort(@german_alphabeth));
91 # $sort_no_collate is 'a,A,ä,Ä,b,B,c,C,[...],s,S,ß,t,T,u,U,ü,Ü,v,V,w,[...],z,Z'
92  
93 All methods are lazy. This means that the values will be only calculated
94 upon the first call of the method.
95  
96 =head1 METHODS
97  
98 =head3 i18n_sort
99  
100 my @sorted = $c->i18n_sort(@list);
101 OR
102 my $sorted = $c->i18n_sort(\@list);
103  
104 Sorts the given list or arrayref with the current locale and returns a
105 list or arrayref.
106  
107 =head3 i18n_collator
108  
109 my $collator = $c->i18n_collator();
110  
111 Returns a L<Unicode::Collate::Locale> object with the current language used
112 as the locale. The collator settings can be configured in your Catalyst I18N
113 configuration:
114  
115 # Add some I18N configuration
116 __PACKAGE__->config(
117 name => 'MyApp',
118 I18N => {
119 default_locale => 'de_AT',
120 locales => {
121 'de_AT' => {
122 collate => {
123 level => 3,
124 variable => 'Non-Ignorable',
125 ...
126 },
127 },
128 }
129 },
130 );
131  
132 The following configuration options are available (see L<Unicode::Collate> for
133 detailed documentation):
134  
135 =over
136  
137 =item * UCA_Version
138  
139 =item * alternate
140  
141 =item * backwards
142  
143 =item * entry
144  
145 =item * hangul_terminator
146  
147 =item * ignoreChar
148  
149 =item * katakana_before_hiragana
150  
151 =item * level
152  
153 =item * normalisation
154  
155 =item * overrideCJK
156  
157 =item * overrideHangul
158  
159 =item * preprocess
160  
161 =item * rearrange
162  
163 =item * suppress
164  
165 =item * table
166  
167 =item * undefName
168  
169 =item * undefChar
170  
171 =item * upper_before_lower
172  
173 =item * variable
174  
175 =back
176  
177 =head1 SEE ALSO
178  
179 L<Unicode::Collate>
180  
181 =head1 AUTHOR
182  
183 Maroš Kollár
184 CPAN ID: MAROS
185 maros [at] k-1.com
186
187 L<http://www.k-1.com>
« CatalystX/I18N/Model/DataLocalize.pm ↑ Summary CatalystX/I18N/Role/DateTime.pm »