File Coverage

blib/lib/Date/Holidays/Adapter/Local.pm
Criterion Covered Total %
statement 18 49 36.7
branch 0 14 0.0
condition 0 24 0.0
subroutine 6 10 60.0
pod 3 3 100.0
total 27 100 27.0


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