blib/lib/HTML/Calendar/Monthly.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 75.0% 15 / 20 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 81.2% 13 / 16 Podpod 83.3% 5 / 6 totaltotal 87.2% 109 / 125 CC 14 SCAR 11.5
CC
14
Cov
90%
_init
6.6
add_link
4.0
_days_list
3.0
CRAP 3.2
↑ Summary
1 package HTML::Calendar::Monthly;
2  
3 # Monthly.pm -- A very simple HTML calendar
4 # RCS Info : $Id: Monthly.pm,v 1.4 2009/06/25 09:18:25 jv Exp $
5 # Author : Johan Vromans
6 # Created On : Thu Apr 30 22:13:00 2009
7 # Last Modified By: Johan Vromans
8 # Last Modified On: Thu Jun 25 11:18:16 2009
9 # Update Count : 4
10 # Status : Unknown, Use with caution!
11  
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = "0.03";
16  
17 =head1 NAME
18  
19 HTML::Calendar::Monthly - A very simple HTML calendar
20  
21 =head1 SYNOPSIS
22  
23 use HTML::Calendar::Monthly;
24  
25 my $cal = HTML::Calendar::Monthly->new; # This month, this year
26 $cal = HTML::Calendar::Monthly->new({ 'month' => $month }); # This year
27 $cal = HTML::Calendar::Monthly->new({ 'month' => $month,
28 'year' => $year});
29  
30 my $month = $cal->month;
31 my $year = $cal->year;
32  
33 # Add a link for a day.
34 $cal->add_link( $day, $link );
35  
36 # Get HTML representation.
37 my $html = $cal->calendar_month;
38  
39 =head1 DESCRIPTION
40  
41 This is a very simple module which will make an HTML representation of
42 a given month. You can add links to individual days.
43  
44 Yes, the inspiration for this came out of me looking at
45 HTML::Calendar::Simple, and thinking 'Hmmm. A bit too complicated for
46 what I want. I know, I will write a simplified version.' So I did.
47  
48 =cut
49  
50 3 ▶use Date::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 3 my @days = qw( Ma Di Wo Do Vr Za Zo );
53 3 my @months = qw( Januari Februari Maart April Mei Juni Juli
54 Augustus September Oktober November December );
55  
56 3 ▶use constant DAYS_IN_WEEK => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use constant DAYS_IN_MONTH => 31; # max
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 =head2 new
60  
61 my $cal = HTML::Calendar::Monthly->new;
62 my $cal = HTML::Calendar::Monthly->new({ 'month' => $month });
63 my $cal = HTML::Calendar::Monthly->new({ 'month' => $month,
64 'year' => $year });
65  
66 This will make a new HTML::Calendar::Monthly object.
67  
68 =cut
69  
70 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
71 2 my $self = {};
72 2 bless( $self, shift );
73 2 $self->_init(@_);
74 2 return $self;
75 }
76  
77 2 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.9
78 2 my $self = shift;
79 # Validate the args passed to new, if there were any.
80 2 my $valid_day = Date::Simple->new;
81 2 my $ref = shift;
82 2 ▶ if ( defined $ref && ref $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref and ref $ref eq "HASH") 2 0 2
Condition33%1/3
defined $ref and ref $ref eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $ref and ref $ref eq "HASH"
defined $ref ref $ref eq "HASH"
Truth table33%1/3
A and B
A= defined $refB= ref $ref eq "HASH"
ABresult
0X0
100
111
83 2 ▶ my $month = exists $ref->{month} ? $ref->{month} : $valid_day->month;
Branch: 1 branch
BranchTrueFalseTotal
exists $ref->{'month'} ? : 2 0 2
84 2 ▶ my $year = exists $ref->{year} ? $ref->{year} : $valid_day->year;
Branch: 1 branch
BranchTrueFalseTotal
exists $ref->{'year'} ? : 2 0 2
85 2 $valid_day = $self->_date_obj($year, $month, 1);
86 2 ▶ $valid_day = defined $valid_day ? $valid_day : Date::Simple->new;
Branch: 1 branch
BranchTrueFalseTotal
defined $valid_day ? : 2 0 2
87 }
88 2 $self->{month} = $valid_day->month;
89 2 $self->{year} = $valid_day->year;
90 2 $self->{the_month} = $self->_days_list($self->{month}, $self->{year});
91 2 return $self;
92 }
93  
94 =head2 month
95  
96 my $month = $cal->month;
97  
98 This will return the numerical value of the month.
99  
100 my $month = $cal->month_name;
101  
102 This will return the name of the month.
103  
104 =head2 year
105  
106 my $year = $cal->year;
107  
108 This will return the four-digit year of the calendar
109  
110 =cut
111  
112 0 ▶sub month { $_[0]->{month} } # month in numerical format
Subroutine month: not called CC=1 SCAR=6.9
Pod documented
113 0 ▶sub month_name { $months[$_[0]->{month}-1 ]} # month name
Subroutine month_name: not called CC=1 SCAR=6.9
Pod undocumented
114 0 ▶sub year { $_[0]->{year} } # year in YYYY form
Subroutine year: not called CC=1 SCAR=6.9
Pod documented
115 4 ▶sub _the_month { @{ $_[0]->{the_month} } } # this is the list of hashrefs.
Subroutine _the_month: called CC=1 SCAR=0.0
116  
117 =head2 add_link
118  
119 $cal->add_link( $day, $link ); # puts an href on the day
120  
121 =cut
122  
123 2 ▶sub add_link {
Subroutine add_link: called CC=4 SCAR=13.9
Pod documented
124 2 my ($self, $day, $link) = @_;
125 2 foreach my $day_ref ( $self->_the_month ) {
126 24 ▶ next unless $day_ref && $day_ref->{date}->day == $day;
Branch: 1 branch
BranchTrueFalseTotal
unless $day_ref and $day_ref->{"date"}->day == $day 22 2 24
Condition100%3/3
$day_ref and $day_ref->{"date"}->day == $day
!ll&&!rl&&r
4182
MC/DC100%2/2
$day_ref and $day_ref->{"date"}->day == $day
$day_ref $day_ref->{"date"}->day == $day
Truth table100%3/3
A and B
A= $day_refB= $day_ref->{"date"}->day == $day
ABresult
0X0
100
111
127 2 $day_ref->{day_link} = $link;
128 2 last;
129 }
130 }
131  
132 70 ▶sub _cell {
Subroutine _cell: called CC=3 SCAR=11.0
133 70 my ( $self, $ref ) = @_;
134 70 ▶ return "<td class='hc_empty'></td>" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 10 60 70
135  
136 60 ▶ if ( exists $ref->{day_link} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{'day_link'}) { } 2 58 60
137 return
138 "<td class='hc_date_linked'>"
139 2 . "<a href='" . $ref->{day_link} . "'>" . $ref->{date}->day . "</a>"
140 . "</td>";
141 }
142 else {
143 return
144 "<td class='hc_date'>"
145 . $ref->{date}->day
146 58 . "</td>";
147 }
148 }
149  
150 =head2 calendar_month
151  
152 my $html = $cal->calendar_month;
153  
154 This will return an html string of the calendar month.
155  
156 =cut
157  
158 2 ▶sub calendar_month {
Subroutine calendar_month: called CC=2 SCAR=6.9
Pod documented
159 2 my $self = shift;
160 2 my @seq = $self->_the_month;
161 my $cal = "<table class='hc_month'>\n"
162 . " <tr>\n"
163 2 . join("\n", map { " <th>$_</th>" } @days )
164 . " </tr>\n";
165 2 ▶ while ( @seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@seq) 10 2 12
166 10 my @week_row = splice( @seq, 0, DAYS_IN_WEEK );
167 10 $#week_row = DAYS_IN_WEEK - 1;
168 $cal .= " <tr>\n"
169 10 . join("\n", map { " " . $self->_cell($_) } @week_row )
170 . "\n </tr>\n";
171 }
172 2 $cal .= "</table>\n";
173 2 return $cal;
174 }
175  
176  
177 6 ▶sub _date_obj { Date::Simple->new($_[1], $_[2], $_[3]) }
Subroutine _date_obj: called CC=1 SCAR=0.0
178  
179 2 ▶sub _days_list {
Subroutine _days_list: called CC=3 SCAR=11.0
180 2 my $self = shift;
181 # Fill in a Date::Simple object for every day, Why not Date::Range object?
182 # Because I haven't installed it yet, and not sure it would be appropriate
183 # for the way I have set this up.
184 2 my ($month, $year) = @_;
185 2 my $start = $self->_date_obj($year, $month, 1);
186 2 my $end = $start + DAYS_IN_MONTH;
187 2 $end = $self->_date_obj($end->year, $end->month, 1);
188  
189 2 my $st = $start->day_of_week;
190  
191 # We start weeks on monday.
192 2 $st--;
193 2 ▶ $st += DAYS_IN_WEEK if $st < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $st < 0 0 2 2
194  
195 2 my @seq = ( undef ) x $st;
196 2 ▶ push @seq, { 'date' => $start++ } while ($start < $end);
Branch: 1 branch
BranchTrueFalseTotal
$start < $end and do { push @seq, {"date", $start++} } 60 2 62
197 2 return \@seq;
198 }
199  
200 =head1 AUTHOR
201  
202 Johan Vromans E<lt>jvromans@squirrel.nlE<gt>
203  
204 Parts of this module are copied from HTML::Calendar::Simple, written by Stray Toaster E<lt>coder@stray-toaster.co.ukE<gt>.
205  
206 =cut
207  
208 3 1;
↑ Summary