Document/NotesFile.pm

filter: Statementstmt 35.4% 17 / 48 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 100% 2 / 2 totaltotal 31.5% 24 / 76 CC 10 SCAR 27.7
CC
10
Cov
26%
log
30.0
new
20.0
_setdirobj
12.0
CRAP 15.9
« Document/Toc.pm ↑ Summary Document/Directory.pm »
1 #=============================== NotesFile.pm ================================
2 # Filename: NotesFile.pm
3 # Description: NotesFile file manager.
4 # Original Author: Dale Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:26:17 $
7 # Version: $Revision: 1.5 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 #=============================================================================
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fault::ErrorHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DMA::ISODate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec::DatedPage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Document::NotesFile;
17 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 @ISA = qw( UNIVERSAL );
19  
20 #=============================================================================
21 # Class Methods
22 #=============================================================================
23  
24 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
25 0 my ($class,$directory) = @_;
26 0 my $self = bless {}, $class;
27  
28 0 ▶ $directory ||
Branch: 1 branch
BranchTrueFalseTotal
$directory or "Fault::ErrorHandler"->warn("NotesFile->new: Missing directory path."), (return undef) 0 0 0
29 (Fault::ErrorHandler->warn ("NotesFile->new: Missing directory path."),
30 return undef);
31  
32 0 my $dir = $self->_setdirobj ($directory);
33 0 ▶ $dir || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$dir or return undef 0 0 0
34  
35 0 $self->{'directory'} = $dir;
36  
37 # Read in our own values if the .log already exists, create it if it
38 # doesn't and failure if we can't
39 #
40 0 my $log = $dir->pathname . "/.log";
41 0 ▶ if (!-f $log) {$self->log("Initialized .notes file for " . $dir->filename);}
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $log) 0 0 0
42  
43 0 return $self;
44 }
45  
46 #=============================================================================
47 # Object Methods
48 #=============================================================================
49  
50 0 ▶sub log {
Subroutine log: not called CC=5 SCAR=34.0
Pod documented
51 0 my ($self,$note,$user) = @_;
52 0 ▶ defined $note || ($note="<Empty note>");
Branch: 1 branch
BranchTrueFalseTotal
defined $note or $note = "<Empty note>" 0 0 0
53 0 ▶ defined $user || ($user="Unknown");
Branch: 1 branch
BranchTrueFalseTotal
defined $user or $user = "Unknown" 0 0 0
54  
55 0 my $fd;
56 0 my $path = $self->{'directory'}->pathname;
57 0 my $utc = DMA::ISODate->utc;
58 0 my $msg = sprintf "%s\n[%s %s%s]", $note, $user,$utc->date,$utc->time;
59  
60 # First write the new values to a tmp file.
61 0 ▶ if (!open ($fd, ">>$path/.notes")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fd, ">>$path/.notes") 0 0 0
62 0 Fault::ErrorHandler->warn ("Failed to open .notes file at ($path)!");
63 0 return 0;
64 }
65
66 0 ▶ if (!printf $fd "$msg\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (printf $fd "$msg\n") 0 0 0
67 0 Fault::ErrorHandler->warn
68 ("Failed to write note from user $user: ($note) to ($path)!");
69 0 close $fd;
70 0 return 0;
71 }
72 0 close $fd;
73 0 return 1;
74 }
75  
76 #=============================================================================
77 # Internal Methods
78 #=============================================================================
79 # Check the type of the filename argument. If it is an object of the right
80 # Class, use it; otherwise return undef
81  
82 0 ▶sub _setdirobj {
Subroutine _setdirobj: not called CC=3 SCAR=24.8
83 0 my ($self, $dir) = @_;
84 0 my $obj = $dir;
85  
86 # If it is not a reference, assume it is a filename string and parse it.
87 0 ▶ (ref($dir)) || ($obj = File::Spec::DatedPage->new ($dir));
Branch: 1 branch
BranchTrueFalseTotal
ref $dir or $obj = "File::Spec::DatedPage"->new($dir) 0 0 0
88  
89 0 ▶ ($obj->isa ('File::Spec::DatedPage')) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
$obj->isa("File::Spec::DatedPage") or return undef 0 0 0
90 0 return $obj;
91 }
92
93 #=============================================================================
94 # Pod Documentation
95 #=============================================================================
96 # You may extract and format the documention section with the 'perldoc' cmd.
97  
98 =head1 NAME
99  
100 Document::NotesFile - NotesFile file manager.
101  
102 =head1 SYNOPSIS
103  
104 use Document::NotesFile;
105 $obj = Document::NotesFile->new ($filespecobj);
106 $obj = Document::NotesFile->new ($filename)
107 $okay = $obj->note ($note,$user);
108  
109 =head1 Inheritance
110  
111 UNIVERSAL
112  
113 =head1 Description
114  
115 This Class manages .note files contained within a Document directory. Notes
116 are special historical or other information about the document added by the
117 user of a program.
118  
119 =head1 Examples
120  
121 use Document::NotesFile;
122 my $baz = Document::NotesFile->new ($filespecobj);
123 my $baz = Document::NotesFile->new ($pathname);
124 my $waslogged = $baz->note ("Washington slept in this file", "BenF" );
125  
126 =head1 Class Variables
127  
128 None.
129  
130 =head1 Instance Variables
131  
132 None.
133  
134 =head1 Class Methods
135  
136 =over 4
137  
138 =item B<$obj = Document::NotesFile-E<gt>new ($filespecobj)>
139  
140 =item B<$obj = Document::NotesFile-E<gt>new ($filename)>
141  
142 Create an object to allow communications with a notes file object of a
143 Document directory defined by $filename or $filespecobj. If there is
144 currently no file in it named .note, create one and emit a log creation
145 message about it.
146  
147 Objects of this Class may be used as inputs to the Fault::Logger class.
148  
149 All of the path information comes either from a preparsed filename in
150 $filespecobj, a File::Spec::DatedPage object, or a string containing a
151 pathname to the target Document directory.
152  
153 [Can we get by with a File::Spec::Dated object?]
154  
155 =back 4
156  
157 =head1 Instance Methods
158  
159 =over 4
160  
161 =item B<$okay = $obj-E<gt>log ($note, $user)>
162  
163 Write a time-stamped message into the .note file of the form:
164  
165 $note\n
166 [$user 20021207223010]\n
167  
168 and return true if we succeeded in doing so.
169  
170 =back 4
171  
172 =head1 Private Class Methods
173  
174 None.
175  
176 =head1 Private Instance Methods
177  
178 None.
179  
180 =head1 KNOWN BUGS
181  
182 See TODO.
183  
184 =head1 SEE ALSO
185  
186 Fault::ErrorHandler, DMA::ISODate, File::Spec::DatedPage
187  
188 =head1 AUTHOR
189  
190 Dale Amon <amon@vnl.com>
191  
192 =cut
193
194 #=============================================================================
195 # CVS HISTORY
196 #=============================================================================
197 # $Log: NotesFile.pm,v $
198 # Revision 1.5 2008-08-28 23:26:17 amon
199 # perldoc section regularization.
200 #
201 # Revision 1.4 2008-08-13 21:04:32 amon
202 # Third phase of reformatting to current standard.
203 #
204 # Revision 1.3 2008-08-13 20:56:53 amon
205 # Second phase of reformatting to current standard.
206 #
207 # Revision 1.2 2008-08-13 14:50:02 amon
208 # First phase of reformatting to current standard.
209 #
210 # Revision 1.1.1.1 2004-09-27 15:57:01 amon
211 # Manages a Document directory
212 #
213 # 20040927 Dale Amon <amon@islandone.org>
214 # Created.
215 1 1;
« Document/Toc.pm ↑ Summary Document/Directory.pm »