lib/Date/Holidays/Adapter.pm

filter: Statementstmt 42.7% 47 / 110 Branchbran 12.5% 5 / 40 Conditioncond 16.6% 3 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 70.5% 12 / 17 Podpod 100% 3 / 3 totaltotal 35.0% 70 / 200 CC 27 SCAR 34.6
CC
27
Cov
33%
holidays
72.0
is_holiday
72.0
_fetch
21.5
CRAP 31.8
« blib/lib/Date/Holidays/Adapter/Local.pm ↑ Summary lib/Date/Holidays.pm »
1 package Date::Holidays::Adapter;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Carp; # croak
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Locale::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 33 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 33 $VERSION = '1.35';
14  
15 2 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod documented
16 2 my ($class, %params) = @_;
17  
18 my $self = bless {
19 _countrycode => $params{countrycode},
20 2 ▶ _adaptee => undef,
Condition33%1/3
$class || ref $class
l!l&&r!l&&!r
200
MC/DC0%0/2
$class || ref $class
$class ref $class
Truth table33%1/3
A || B
A= $classB= ref $class
ABresult
1X1
011
000
21 }, $class || ref $class;
22  
23 2 my $adaptee = $self->_fetch(\%params);
24  
25 2 ▶ if ($adaptee) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptee) { } 2 0 2
26 2 $self->{_adaptee} = $adaptee;
27 } else {
28 0 die 'Unable to initialize adaptee class';
29 }
30  
31 2 return $self;
32 }
33  
34 0 ▶sub holidays {
Subroutine holidays: not called CC=8 SCAR=42.8
Pod documented
35 0 my ($self, %params) = @_;
36  
37 0 my $r;
38 my $adaptee;
39  
40 # Adaptee has a constructor
41 0 ▶ if ( $self->{_adaptee}->can('new')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_adaptee'}->can('new') and $self->isa('Date::Holidays::Adapter')) { } 0 0 0
Condition0%0/3
$self->{"_adaptee"}->can("new") and $self->isa("Date::Holidays::Adapter")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_adaptee"}->can("new") and $self->isa("Date::Holidays::Adapter")
$self->{"_adaptee"}->can("new") $self->isa("Date::Holidays::Adapter")
Truth table0%0/3
A and B
A= $self->{"_adaptee"}->can("new")B= $self->isa("Date::Holidays::Adapter")
ABresult
0X0
100
111
42 and $self->isa('Date::Holidays::Adapter')) {
43  
44 0 $adaptee = $self->{_adaptee}->new();
45  
46 # Adaptee has no constructor
47 } else {
48 0 $adaptee = $self->{'_adaptee'};
49 }
50  
51 0 ▶ if (blessed $adaptee) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($adaptee)) { } 0 0 0
52  
53 # Adapting non-polymorphic interface
54 0 my $method = "$self->{'_countrycode'}_holidays";
55 0 my $sub = $adaptee->can($method);
56  
57 # Adapting polymorphic interface
58 0 ▶ if (! $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 0 0 0
59 0 $method = 'holidays';
60 0 $sub = $adaptee->can($method);
61 }
62  
63 0 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 0 0 0
64 0 $r = $adaptee->$method($params{'year'});
65 }
66  
67 0 return $r;
68  
69 } else {
70  
71 # Adapting non-polymorphic interface
72 0 my $method = "$self->{'_countrycode'}_holidays";
73 0 my $sub = $adaptee->can($method);
74  
75 # Adapting polymorphic interface
76 0 ▶ if (! $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 0 0 0
77 0 $sub = $adaptee->can('holidays');
78 }
79  
80 0 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 0 0 0
81 0 $r = &{$sub}($params{'year'});
82 }
83  
84 0 return $r;
85 }
86 }
87  
88 0 ▶sub is_holiday {
Subroutine is_holiday: not called CC=8 SCAR=42.8
Pod documented
89 0 my ($self, %params) = @_;
90  
91 0 my $r;
92 my $adaptee;
93  
94 0 ▶ if ( $self->{'_adaptee'}->can('new')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_adaptee'}->can('new') and $self->isa('Date::Holidays::Adapter')) { } 0 0 0
Condition0%0/3
$self->{"_adaptee"}->can("new") and $self->isa("Date::Holidays::Adapter")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_adaptee"}->can("new") and $self->isa("Date::Holidays::Adapter")
$self->{"_adaptee"}->can("new") $self->isa("Date::Holidays::Adapter")
Truth table0%0/3
A and B
A= $self->{"_adaptee"}->can("new")B= $self->isa("Date::Holidays::Adapter")
ABresult
0X0
100
111
95 and $self->isa('Date::Holidays::Adapter')) {
96  
97 0 $adaptee = $self->{'_adaptee'}->new();
98  
99 } else {
100 0 $adaptee = $self->{'_adaptee'};
101 }
102  
103 0 ▶ if (blessed $adaptee) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($adaptee)) { } 0 0 0
104  
105 # Adapting non-polymorphic interface
106 0 my $method = "is_$self->{'_countrycode'}_holiday";
107  
108 0 ▶ if ($adaptee->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptee->can($method)) { } 0 0 0
109  
110 $r = $adaptee->$method(
111 $params{'year'},
112 $params{'month'},
113 0 $params{'day'}
114 );
115  
116 0 return $r;
117  
118 # Adapting polymorphic interface
119 } else {
120  
121 0 ▶ if ($adaptee->can('is_holiday')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adaptee->can("is_holiday")) 0 0 0
122 $r = $adaptee->is_holiday(
123 $params{'year'},
124 $params{'month'},
125 0 $params{'day'}
126 );
127 }
128  
129 0 return $r;
130 }
131  
132 } else {
133 # Adapting non-polymorphic interface
134 0 my $method = "is_$self->{_countrycode}_holiday";
135 0 my $sub = $adaptee->can($method);
136  
137 # We have an interface
138 0 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 0 0 0
139  
140 0 $r = &{$sub}(
141 $params{'year'},
142 $params{'month'},
143 0 $params{'day'}
144 );
145  
146 0 return $r;
147 }
148  
149 # Adapting polymorphic interface
150 0 $sub = $adaptee->can('is_holiday');
151  
152 0 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 0 0 0
153 0 $r = &{$sub}(
154 $params{'year'},
155 $params{'month'},
156 0 $params{'day'}
157 );
158  
159 0 return $r;
160 }
161 }
162  
163 0 return $r;
164 }
165  
166 5 ▶sub _load {
Subroutine _load: called CC=2 SCAR=7.2
167 5 my ( $self, $module ) = @_;
168  
169 # Trying to load module
170 5 eval { load $module; }; # From Module::Load
171  
172 # Asserting success of load
173 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
174 0 die "Unable to load: $module - $@\n";
175 }
176  
177 # Returning name of loaded module upon success
178 5 return $module;
179 }
180  
181 2 ▶sub _fetch {
Subroutine _fetch: called CC=7 SCAR=30.7
182 2 my ( $self, $params ) = @_;
183  
184 # Do we have a country code?
185 2 ▶ if ( not $self->{'_countrycode'} and not $params->{countrycode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_countrycode"} or $params->{"countrycode"}) 0 2 2
Condition33%1/3
$self->{"_countrycode"} or $params->{"countrycode"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{"_countrycode"} or $params->{"countrycode"}
$self->{"_countrycode"} $params->{"countrycode"}
Truth table33%1/3
A or B
A= $self->{"_countrycode"}B= $params->{"countrycode"}
ABresult
1X1
011
000
186 0 croak 'No country code specified';
187 }
188  
189 2 ▶ my $countrycode = $params->{countrycode} || $self->{'_countrycode'};
Condition33%1/3
$params->{'countrycode'} || $self->{'_countrycode'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$params->{'countrycode'} || $self->{'_countrycode'}
$params->{'countrycode'} $self->{'_countrycode'}
Truth table33%1/3
A || B
A= $params->{'countrycode'}B= $self->{'_countrycode'}
ABresult
1X1
011
000
190  
191 # Do we do country code assertion?
192 2 ▶ if ( !$params->{'nocheck'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"nocheck"}) 0 2 2
193  
194 # Is our country code valid or local?
195 0 ▶ if ( $countrycode !~ m/local/i and !code2country( $countrycode ) ) { #from Locale::Country
Branch: 1 branch
BranchTrueFalseTotal
unless ($countrycode =~ /local/i or code2country($countrycode)) 0 0 0
Condition0%0/3
$countrycode =~ /local/i or code2country($countrycode)
l!l&&r!l&&!r
000
MC/DC0%0/2
$countrycode =~ /local/i or code2country($countrycode)
$countrycode =~ /local/i code2country($countrycode)
Truth table0%0/3
A or B
A= $countrycode =~ /local/iB= code2country($countrycode)
ABresult
1X1
011
000
196 0 die "$countrycode is not a valid country code";
197 }
198 }
199  
200 # Trying to load adapter module for country code
201 2 my $module;
202  
203 2 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.6
204 # We load an adapter implementation
205 2 ▶ if ( code2country( $countrycode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code2country($countrycode)) { } 0 2 2
206 0 $module = 'Date::Holidays::' . uc $countrycode;
207 } else {
208 2 $module = 'Date::Holidays::' . $countrycode;
209 }
210  
211 2 $module = $self->_load($module);
212  
213 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 warn "Unable to load module: $module - $_";
215  
216 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
217 #$countrycode = uc $countrycode;
218  
219 0 ▶ if ($countrycode =~ m/local/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($countrycode =~ /local/i) { } 0 0 0
220 0 $module = 'Date::Holidays::Local';
221 } else {
222 0 $module = 'Date::Holidays::' . $countrycode;
223 }
224  
225 # We load an adapter implementation
226  
227 0 ▶ if ($module = $self->_load($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module = $self->_load($module)) 0 0 0
228 0 warn "we got a module and we return\n";
229 }
230  
231 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 0 warn "Unable to load module: $module - $_";
233  
234 0 $module = 'Date::Holidays::Adapter';
235 0 $module = $self->_load($module);
236 0 };
237 2 };
238  
239 # Returning name of loaded module upon success
240 2 return $module;
241 }
242  
243 33 1;
244  
245 __END__
« blib/lib/Date/Holidays/Adapter/Local.pm ↑ Summary lib/Date/Holidays.pm »