Schedule/Weekly.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Schedule.pm ↑ Summary
1 package DateTime::Schedule::Weekly 0.03;
2 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Augment DateTime::Schedule with a weekly recurrrence pattern
5  
6 0 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 class DateTime::Schedule::Weekly {
9 inherit DateTime::Schedule;
10  
11 use Readonly;
12 Readonly::Array my @DAY_NUMS => (undef, qw(monday tuesday wednesday thursday friday saturday sunday));
13  
14 use constant true => !!1;
15 use constant false => !true;
16  
17 field $sunday : param : reader = true;
18 field $monday : param : reader = true;
19 field $tuesday : param : reader = true;
20 field $wednesday : param : reader = true;
21 field $thursday : param : reader = true;
22 field $friday : param : reader = true;
23 field $saturday : param : reader = true;
24  
25 field $include : param : reader = [];
26  
27 ADJUST {
28 die("At least one day must be scheduled")
29 unless ($self->sunday
30 || $self->monday
31 || $self->tuesday
32 || $self->wednesday
33 || $self->thursday
34 || $self->friday
35 || $self->saturday);
36  
37 $include = {map {$_->strftime('%F') => true} ($include // [])->@*};
38 }
39  
40 sub weekdays($class, @params) {
41 __PACKAGE__->new(sunday => false, saturday => false, @params);
42 }
43  
44 sub weekends($class, @params) {
45 __PACKAGE__->new(monday => false, tuesday => false, wednesday => false, thursday => false, friday => false, @params);
46 }
47  
48 method is_day_scheduled($d) {
49 my $day_name = $DAY_NUMS[$d->day_of_week];
50 my $str = $d->strftime('%F');
51 return false if ($self->exclude->{$str});
52 return $self->include->{$str} || $self->$day_name;
53 }
54 }
55  
56 =head1 NAME
57  
58 DateTime::Schedule::Weekly - Determine scheduled days in range based on weekly recurrence and inclusions/exclusions
59  
60 =head1 SYNOPSIS
61  
62 use DateTime::Schedule::Weekly;
63  
64 my $dts = DateTime::Schedule::Weekly->weekdays(exclude => [...list of school holidays...], portion => 0.5);
65  
66 my $school_days_elapsed = $dts->days_in_range($first_day_of_school, $now)->count;
67  
68 =head1 DESCRIPTION
69  
70 This subclass of L<DateTime::Schedule> augments its capabilities to support a
71 regular weekly schedule of on/off days.
72  
73 =head1 CONSTRUCTORS
74  
75 =head2 new
76  
77 Returns a new instance. Permits the following construction parameters, in
78 addition to those supported by L<DateTime::Schedule>:
79  
80 =head4 sunday, monday, tuesday, wednesday, thursday, friday, saturday
81  
82 I<Optional>. Default C<true>
83  
84 These seven constructor parameters indicate which days of the week are "scheduled"
85 By default, all are "on", making the behavior identical to L<DateTime::Schedule>'s.
86  
87 =head4 include
88  
89 I<Optional>. Default C<[]>
90  
91 An arrayref of L<DateTime>s. These days are included over and above the normal
92 schedule.
93  
94 E.g., a school schedule could be regularly M-F, but due to extensive weather-related
95 closures, the school added in one or more saturdays class days. These would be
96 C<include>d.
97  
98 B<N.B. Exclusions have priority over inclusions, so a date in both lists will be excluded!>
99  
100 =head2 weekdays
101  
102 Identical to calling L</new> except that by default C<saturday> and C<sunday> are
103 "off".
104  
105 =head2 weekends
106  
107 Identical to calling L</new> except that by default C<monday>, C<tuesday>,
108 C<wednesday>, C<thursday>, and C<friday> are "off".
109  
110 =head1 METHODS
111  
112 Implements all methods from L<DateTime::Schedule>
113  
114 =head2 is_day_scheduled($datetime)
115  
116 Given a L<DateTime>, returns false if date is in the exclusion list. Otherwise
117 returns true if date is in the inclusion list or if date is in a scheduled day
118 of week. Returns false otherwise.
119  
120 =head1 AUTHOR
121  
122 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
123  
124 =head1 LICENSE
125  
126 Copyright (c) 2024 Mark Tyrrell
127  
128 Permission is hereby granted, free of charge, to any person obtaining a copy
129 of this software and associated documentation files (the "Software"), to deal
130 in the Software without restriction, including without limitation the rights
131 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
132 copies of the Software, and to permit persons to whom the Software is
133 furnished to do so, subject to the following conditions:
134  
135 The above copyright notice and this permission notice shall be included in all
136 copies or substantial portions of the Software.
137  
138 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
139 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
140 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
141 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
142 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
143 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
144 SOFTWARE.
145  
146 =cut
147  
148 1;
149  
150 __END__
« Schedule.pm ↑ Summary