File Coverage

lib/Date/Holidays/Adapter/Local.pm
Criterion Covered Total %
statement 34 49 69.3
branch 3 14 21.4
condition 2 24 8.3
subroutine 9 10 90.0
pod 3 3 100.0
total 51 100 51.0


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   433 use warnings;
  1         2  
  1         27  
4 1     1   4 use File::Slurp qw(slurp);
  1         2  
  1         24  
5 1     1   485 use JSON; #from_json
  1         18380  
  1         51  
6 1     1   639 use Env qw($HOLIDAYS_FILE);
  1         7182  
  1         4  
7 1     1   106 use vars qw($VERSION);
  1         2  
  1         8  
8 1     1   143  
  1         3  
  1         326  
9             $VERSION = '1.33';
10              
11             my $class = shift;
12              
13 4     4 1 7 my $self = bless {}, $class;
14              
15 4         7 return $self;
16             };
17 4         9  
18             my ($self, %params) = @_;
19              
20             my $local_holiday_file = $self->_resolve_holiday_file();
21 1     1 1 4  
22             my $local_holidays;
23 1         3 if (-r $local_holiday_file) {
24              
25 1         8 my $json = slurp($local_holiday_file);
26 1 50       14 $local_holidays = from_json($json);
27             }
28 1         5  
29 1         123 my $filtered_holidays = {};
30              
31             if ($params{date} and $params{month} and $params{year}) {
32 1         29  
33             foreach my $key (keys %{$local_holidays}) {
34 1 50 33     6  
      0        
35             if ($key =~ m/^(\d{4})(\d{2})(\d{2})$/
36 0         0 and $1 == $params{year}
  0         0  
37             and $2 == $params{month}
38 0 0 0     0 and $3 == $params{day}) {
    0 0        
      0        
      0        
      0        
39              
40             $filtered_holidays->{$key} = $local_holidays->{$key};
41              
42             } elsif ($key =~ m/^(\d{2})(\d{2})$/
43 0         0 and $1 == $params{month}
44             and $2 == $params{day}) {
45              
46             $filtered_holidays->{$key} = $local_holidays->{$key};
47             }
48             }
49 0         0  
50             return $filtered_holidays;
51              
52             } else {
53 0         0 return $local_holidays;
54             }
55             }
56 1         4  
57             my ($self, %params) = @_;
58              
59             my $holidays = $self->holidays(%params);
60              
61 0     0 1 0 # First we check if a year is specified
62             my $key = $params{year}.$params{month}.$params{day};
63 0         0  
64             if (defined $holidays->{$key}) {
65             return $holidays->{$key};
66 0         0 }
67              
68 0 0       0 # Then we check if just month and day is specified
69 0         0  
70             $key = $params{month}.$params{day};
71              
72             if (defined $holidays->{$key}) {
73             return $holidays->{$key};
74 0         0 }
75              
76 0 0       0 # no holiday defined
77 0         0 return undef;
78             }
79              
80             my $self = shift;
81 0         0  
82             my $filename = '';
83              
84             if (-e $HOLIDAYS_FILE and -f _) {
85 1     1   2 $filename = $HOLIDAYS_FILE;
86             }
87 1         1 }
88              
89 1 50 33     5 1;
90 1         32  
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             Date::Holidays::Adapter::Local - a specialized adapter for local calendars
99              
100             =head1 VERSION
101              
102             This POD describes version 1.33 of Date::Holidays::Adapter::Local
103              
104             =head1 SYNOPSIS
105              
106             my $calendar = Date::Holidays->new(countrycode => 'local');
107              
108             my ($year, $month, $day) = (localtime)[ 5, 4, 3 ];
109             $year += 1900;
110             $month += 1;
111              
112             print "Woohoo" if $calendar->is_holiday(
113             year => $year,
114             month => $month,
115             day => $day
116             );
117              
118             my $holidays = $adapter->holidays(year => $year);
119              
120             printf "Jan. 15th is named '%s'\n", $holidays->{'0115'}; #my birthday I hope
121              
122             =head1 DESCRIPTION
123              
124             The is the SUPER adapter class. All of the adapters in the distribution of
125             Date::Holidays are subclasses of this class. (SEE also L<Date::Holidays>).
126              
127             The SUPER adapter class is at the same time a generic adapter. It attempts to
128             adapt to the most used API for modules in the Date::Holidays::* namespace. So
129             it should only be necessary to implement adapters to the exceptions to modules
130             not following the the defacto standard or suffering from other local
131             implementations.
132              
133             =head1 SUBROUTINES/METHODS
134              
135             The public methods in this class are all expected from the adapter, so it
136             actually corresponds with the abstract is outlined in L<Date::Holidays::Abstract>.
137              
138             Not all methods/subroutines may be implemented in the adaptee classes, the
139             adapters attempt to make the adaptee APIs adaptable where possible. This is
140             afterall the whole idea of the Adapter Pattern, but apart from making the
141             single Date::Holidays::* modules uniform towards the clients and
142             L<Date::Holidays> it is attempted to make the multitude of modules uniform in
143             the extent possible.
144              
145             =head2 new
146              
147             The constructor, takes a single named argument, B<countrycode>
148              
149             =head2 is_holiday
150              
151             The B<holidays> method, takes 3 named arguments, B<year>, B<month> and B<day>
152              
153             returns an indication of whether the day is a holiday in the calendar of the
154             country referenced by B<countrycode> in the call to the constructor B<new>.
155              
156             =head2 holidays
157              
158             The B<holidays> method, takes a single named argument, B<year>
159              
160             returns a reference to a hash holding the calendar of the country referenced by
161             B<countrycode> in the call to the constructor B<new>.
162              
163             The calendar will spand for a year and the keys consist of B<month> and B<day>
164             concatenated.
165              
166             =head1 DEFINING A LOCAL CALENDAR
167              
168             Please refer to the DEVELOPER section in L<Date::Holidays> about contributing to
169             the Date::Holidays::* namespace or attempting for adaptability with
170             L<Date::Holidays>.
171              
172             =head1 DIAGNOSTICS
173              
174             Please refer to DIAGNOSTICS in L<Date::Holidays>
175              
176             =head1 DEPENDENCIES
177              
178             =over
179              
180             =item * L<Carp>
181              
182             =item * L<Module::Load>
183              
184             =item * L<JSON>
185              
186             =item * L<File::Slurp>
187              
188             =back
189              
190             Please see the F<cpanfile> included in the distribution for a complete listing.
191              
192             =head1 INCOMPATIBILITIES
193              
194             Please refer to INCOMPATIBILITIES in L<Date::Holidays>
195              
196             =head1 BUGS AND LIMITATIONS
197              
198             Please refer to BUGS AND LIMITATIONS in L<Date::Holidays>
199              
200             =head1 BUG REPORTING
201              
202             Please refer to BUG REPORTING in L<Date::Holidays>
203              
204             =head1 AUTHOR
205              
206             Jonas Brømsø, (jonasbn) - C<< <jonasbn@cpan.org> >>
207              
208             =head1 LICENSE AND COPYRIGHT
209              
210             L<Date::Holidays> and related modules are (C) by Jonas Brømsø, (jonasbn)
211             2004-2022
212              
213             L<Date::Holidays> and related modules are released under the Artistic License 2.0
214              
215             =cut