LocaleObject.pm

filter: Statementstmt 96.8% 91 / 94 Branchbran 78.9% 30 / 38 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 95.0% 19 / 20 Podpod 80.0% 4 / 5 totaltotal 90.2% 158 / 175 CC 25 SCAR 12.0
CC
25
Cov
91%
module
6.0
setlocale
5.2
_module_to_language
4.1
CRAP 3.3
↑ Summary Filter.pm »
1 # Copyright 2009, 2010, 2011, 2013, 2016, 2017 Kevin Ryde
2  
3 # This file is part of Time-Duration-Locale.
4 #
5 # Time-Duration-Locale is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option) any
8 # later version.
9 #
10 # Time-Duration-Locale is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Time-Duration-Locale. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Time::Duration::LocaleObject;
19 4 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use vars qw($VERSION @ISA $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use Class::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 @ISA = ('Class::Singleton');
27 4 *_new_instance = \&new;
28  
29 4 $VERSION = 12;
30  
31 # uncomment this to run the ### lines
32 #use Smart::Comments;
33  
34 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
35 ### LocaleObject new(): @_
36 8 my ($class, %self) = @_;
37 8 my $self = bless \%self, $class;
38  
39 # Load language module now, if given. You're not supposed to pass both
40 # 'module' and 'language', but for now the latter has precedence.
41 #
42 8 ▶ if (my $module = delete $self{'module'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = delete $self{"module"}) 2 6 8
43 2 $self->module ($module);
44 }
45 6 ▶ if (my $lang = delete $self{'language'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang = delete $self{"language"}) 0 6 6
46 0 $self->language ($lang);
47 }
48  
49 6 return $self;
50 }
51  
52 # don't go through AUTOLOAD
53 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
54  
55 65 ▶sub module {
Subroutine module: called CC=6 SCAR=17.9
Pod documented
56 65 my $self = shift;
57 65 ▶ ref $self or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 11 54 65
58 65 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 54 65
59 # set
60 11 my ($module) = @_;
61 11 ▶ if (defined $module) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $module) 11 0 11
62 # guard against infinite recursion on Time::Duration::Locale
63 # maybe should restrict to lower-case module names
64 11 ▶ if ($module eq 'Time::Duration::Locale'
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq "Time::Duration::Locale" or $module eq "Time::Duration::LocaleObject") 4 7 11
Condition100%3/3
$module eq "Time::Duration::Locale" or $module eq "Time::Duration::LocaleObject"
l!l&&r!l&&!r
227
MC/DC100%2/2
$module eq "Time::Duration::Locale" or $module eq "Time::Duration::LocaleObject"
$module eq "Time::Duration::Locale" $module eq "Time::Duration::LocaleObject"
Truth table100%3/3
A or B
A= $module eq "Time::Duration::Locale"B= $module eq "Time::Duration::LocaleObject"
ABresult
1X1
011
000
65 || $module eq 'Time::Duration::LocaleObject') {
66 4 croak 'Don\'t set module to Locale or LocaleObject';
67 }
68 7 Module::Load::load ($module);
69 }
70 6 $self->{'module'} = $module;
71 }
72 # get
73 60 return $self->{'module'};
74 }
75  
76 8 ▶sub language {
Subroutine language: called CC=4 SCAR=13.9
Pod documented
77 8 my $self = shift;
78 8 ▶ ref $self or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 7 1 8
79 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 2 8
80 # set
81 6 my ($lang) = @_;
82 6 $self->module (_language_to_module ($lang));
83 }
84 # get
85 8 my $module = $self->{'module'};
86 8 ▶ return (defined $module ? _module_to_language($module) : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $module ? : 7 1 8
87 }
88  
89 # maybe it'd be easier to create a Time::Duration::en than mangle the names
90 6 ▶sub _language_to_module {
Subroutine _language_to_module: called CC=2 SCAR=7.2
91 6 my ($lang) = @_;
92 6 ▶ return ($lang eq 'en' ? 'Time::Duration' : "Time::Duration::$lang");
Branch: 1 branch
BranchTrueFalseTotal
$lang eq 'en' ? : 6 0 6
93 }
94 7 ▶sub _module_to_language {
Subroutine _module_to_language: called CC=3 SCAR=14.2
95 7 my ($module) = @_;
96 7 ▶ return ($module eq 'Time::Duration' ? 'en'
Branch: 2 branches
BranchTrueFalseTotal
$module eq 'Time::Duration' ? : 7 0 7
$module =~ /^Time::Duration::(.*)/ ? : 0 0 0
97 : $module =~ /^Time::Duration::(.*)/ ? $1
98 : $module);
99 }
100  
101 #------------------------------------------------------------------------------
102 # setlocale
103  
104 4 ▶sub setlocale {
Subroutine setlocale: called CC=5 SCAR=16.5
Pod documented
105 4 my ($self) = @_;
106 4 ▶ ref $self or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 1 3 4
107 ### TDLObj setlocale()
108  
109 # I18N::LangTags version 0.30 for implicate_supers_strictly(), don't worry
110 # about a I18N::LangTags->VERSION(0.30), it'll bomb
111 #
112 4 require I18N::LangTags;
113 4 require I18N::LangTags::Detect;
114  
115 # Prefer implicate_supers_strictly() over implicate_supers() since the
116 # latter loses territory preferences when it converts
117 #
118 # en-au, en-gb -> en-au, en, en-gb
119 #
120 # whereas implicate_supers_strictly() keeps gb ahead of generic en
121 #
122 # en-au, en-gb -> en-au, en-gb, en
123 #
124 # Not that it makes a difference as of July 2010 since there's no
125 # territory flavours (only the joke en_PIGLATIN).
126 #
127 # Chances are though that if you put in territory preferences in $LANGUAGE
128 # you'll want to include generics explicitly at the desired points, and in
129 # that case implicate_supers() and implicate_supers_strictly() come out
130 # the same.
131 #
132 4 my %seen;
133 my $error;
134 4 foreach my $dashlang (I18N::LangTags::implicate_supers_strictly
135 (I18N::LangTags::Detect::detect()),
136 'en') {
137 4 ▶ next if $seen{$dashlang}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$dashlang}++ 0 4 4
138  
139 4 (my $lang = $dashlang) =~ s/-(.*)/_\U$1/g;
140 ### $dashlang
141 ### attempt lang: $lang
142  
143 4 ▶ if (eval { $self->language($lang); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->language($lang); 1 } }) 4 0 4
144 # return value not documented ... don't use it yet
145 4 return $lang;
146 }
147 0 $error = $@;
148 ### $error
149 }
150 0 croak "Time::Duration not available -- $error";
151 }
152  
153 #------------------------------------------------------------------------------
154 # call-through
155 #
156 # ENHANCE-ME: Umm, like all AUTOLOAD for class methods this is slightly
157 # dangerous. If the base Class::Singleton already has a method the same
158 # name as the Time::Duration function/method which is supposed to be created
159 # here then the AUTOLOAD here doesn't run. Example in
160 # devel/autoload-singleton.pl.
161 #
162 # Should be ok in practice. The trick would be to stub up funcs for the
163 # possible methods in the target module, except that's not done immediately
164 # in new(), and later is too late. Maybe it'd be worth explicit stubs for
165 # the normal Time::Duration funcs at least ...
166 #
167  
168 28 ▶sub can {
Subroutine can: called CC=2 SCAR=6.9
Pod undocumented
169 28 my ($self, $name) = @_;
170 ### print "TDLObj can(): $name
171 28 ▶ return $self->SUPER::can($name) || _make_dispatcher($self,$name);
Condition100%3/3
$self->SUPER::can($name) || _make_dispatcher($self, $name)
l!l&&r!l&&!r
3223
MC/DC100%2/2
$self->SUPER::can($name) || _make_dispatcher($self, $name)
$self->SUPER::can($name) _make_dispatcher($self, $name)
Truth table100%3/3
A || B
A= $self->SUPER::can($name)B= _make_dispatcher($self, $name)
ABresult
1X1
011
000
172 }
173 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
174 1 my $name = $AUTOLOAD;
175 ### TDLObj AUTOLOAD(): $name
176 1 $name =~ s/.*://;
177 1 ▶ my $code = _make_dispatcher($_[0],$name)
Condition50%1/2
_make_dispatcher($_[0], $name) || croak("No such function $name()")
l!l
10
MC/DC0%0/1
_make_dispatcher($_[0], $name) || croak("No such function $name()")
_make_dispatcher($_[0], $name)
Truth table50%1/2
A
A= _make_dispatcher($_[0], $name)
Aresult
11
00
178 || croak "No such function $name()";
179 1 goto $code;
180 }
181  
182 4 ▶use vars '$_make_dispatcher';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 26 ▶sub _make_dispatcher {
Subroutine _make_dispatcher: called CC=4 SCAR=13.9
184 26 my ($class_or_self, $name) = @_;
185 ### TDLObj _make_dispatcher(): $class_or_self, $name
186  
187 # $_make_dispatcher is recursion protection against bad
188 # language_preferences method, or any other undefined method module() or
189 # setlocale() might accidentally call here.
190 26 ▶ if ($_make_dispatcher
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::Duration::LocaleObject::_make_dispatcher or do { local $Time::Duration::LocaleObject::_make_dispatcher = 1; $class_or_self->module or $class_or_self->setlocale; my $module = $class_or_self->module; not $module->can($name) }) 3 23 26
Condition66%2/3
$Time::Duration::LocaleObject::_make_dispatcher or do { local $Time::Duration::LocaleObject::_make_dispatcher = 1; $class_or_self->module or $class_or_self->setlocale; my $module = $class_or_self->module; not $module->can($name) }
l!l&&r!l&&!r
0323
MC/DC50%1/2
$Time::Duration::LocaleObject::_make_dispatcher or do { local $Time::Duration::LocaleObject::_make_dispatcher = 1; $class_or_self->module or $class_or_self->setlocale; my $module = $class_or_self->module; not $module->can($name) }
$Time::Duration::LocaleObject::_make_dispatcher do { local $Time::Duration::LocaleObject::_make_dispatcher = 1; $class_or_self->module or $class_or_self->setlocale; my $module = $class_or_self->module; not $module->can($name) }
Truth table66%2/3
A or B
A= $Time::Duration::LocaleObject::_make_dispatcherB= do { local $Time::Duration::LocaleObject::_make_dispatcher = 1; $class_or_self->module or $class_or_self->setlocale; my $module = $class_or_self->module; not $module->can($name) }
ABresult
1X1
011
000
191 || do {
192 26 local $_make_dispatcher = 1;
193 26 ▶ $class_or_self->module || $class_or_self->setlocale;
Branch: 1 branch
BranchTrueFalseTotal
$class_or_self->module or $class_or_self->setlocale 23 3 26
194 26 my $module = $class_or_self->module;
195 ### module exists: $module
196 ### check can(): $name
197 26 ! $module->can($name) }) {
198 3 return undef;
199 }
200  
201 39 ▶ my $subr = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
202 #### TDLObj dispatch: $name
203  
204 39 my $self = shift;
205 39 ▶ ref $self or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 3 36 39
206 39 ▶ $self->{'module'} || $self->setlocale;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"module"} or $self->setlocale 38 1 39
207  
208 39 my $target = "$self->{'module'}::$name";
209 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
210 39 return &$target(@_);
211 23 };
212 4 ▶ { no strict 'refs'; *$name = $subr }
Subroutine BEGIN: called CC=1 SCAR=0.0
213 23 return $subr;
214 }
215  
216 4 1;
217 __END__
↑ Summary Filter.pm »