JobLog/Log/Line.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 343 CC 37 SCAR 45.9
CC
37
Cov
0%
new
306.0
clone
56.0
parse
42.0
CRAP 98.6
1 package App::JobLog::Log::Line;
2 $App::JobLog::Log::Line::VERSION = '1.042';
3 # ABSTRACT: encapsulates one line of log text
4  
5  
6 use Modern::Perl;
7 use Class::Autouse qw{DateTime};
8 use autouse 'App::JobLog::Time' => qw(now tz);
9  
10 # represents a single non-comment line in the log
11 # not using Moose to keep CLI snappy
12  
13 # to_string method for convenience
14 use overload '""' => \&to_string;
15 use overload 'bool' => sub { 1 };
16  
17 # some global variables for use in BNF regex
18 our ( $date, @tags, @description, $is_beginning, $is_note );
19  
20 # log line parser
21 our $re = qr{
22 ^ (?&ts) (?&non_ts) $
23 (?(DEFINE)
24 (?<ts> (\d{4}\s++\d++\s++\d++\s++\d++\s++\d++\s++\d++) (?{$date = $^N}) )
25 (?<non_ts> (?&note) | (?&duration_mark) )
26 (?<duration_mark> : (?: (?&done) | (?&event) ) )
27 (?<done> DONE )
28 (?<note> <NOTE> (?&event) (?{$is_note = 1}) )
29 (?<event> (?&tags) : (?&descriptions) (?{$is_beginning = 1}) )
30 (?<tags> (?:(?&tag)(\s++(?&tag))*+)?)
31 (?<tag> ((?:[^\s:\\]|(?&escaped))++) (?{push @tags, $^N}))
32 (?<escaped> \\.)
33 (?<descriptions> (?: (?&description) (?: ; \s*+ (?&description) )*+ )? )
34 (?<description> ((?:[^;\\]|(?&escaped))++) (?{push @description, $^N}))
35 )
36 }xi;
37  
38  
39 sub new {
40 my ( $class, @args ) = @_;
41 $class = ref $class || $class;
42 my %opts = @args;
43  
44 # validate %opts
45 my $self = bless {}, $class;
46 if ( exists $opts{comment} ) {
47 $self->{comment} = $opts{comment};
48 delete $opts{comment};
49 die 'inconsistent arguments: ' . join( ', ', @args ) if keys %opts;
50 }
51 elsif ( exists $opts{done} ) {
52 my $time = $opts{time};
53 die "invalid value for time: $time"
54 if $time && ref $time ne 'DateTime';
55 $self->{time} = $time || now;
56 $self->{done} = 1;
57 delete $opts{done};
58 delete $opts{time};
59 die 'inconsistent arguments: ' . join( ', ', @args ) if keys %opts;
60 }
61 elsif ( exists $opts{time} ) {
62 my $time = $opts{time};
63 die "invalid value for time: $time"
64 if $time && ref $time ne 'DateTime';
65 $self->{time} = $time;
66 my $tags = $opts{tags};
67 die 'invalid value for tags: ' . $tags
68 if defined $tags && ref $tags ne 'ARRAY';
69 unless ($tags) {
70 $tags = [];
71 $self->{tags_unspecified} = 1;
72 }
73 $self->{tags} = $tags;
74 my $description = $opts{description};
75 if ( my $type = ref $description ) {
76 die 'invalid type for description: ' . $type
77 unless $type eq 'ARRAY';
78 $self->{description} = $description;
79 }
80 elsif ( defined $description ) {
81  
82 # normalize whitespace; this is useful for testing
83 $description =~ s/^\s++|\s++$//g;
84 $description =~ s/\s++/ /g;
85
86 $description = [$description];
87 }
88 else {
89 $description = [];
90 }
91 $self->{description} = $description;
92 delete @opts{qw(time tags description)};
93 die 'inconsistent arguments: ' . join( ', ', @args ) if keys %opts;
94 }
95 elsif ( exists $opts{text} ) {
96 die 'text lines in log must be blank' if $opts{text} =~ /\S/;
97 $self->{text} = $opts{text} . '';
98 delete $opts{text};
99 die 'inconsistent arguments: ' . join( ', ', @args ) if keys %opts;
100 }
101 return $self;
102 }
103  
104  
105 sub parse {
106 my ( $class, $text ) = @_;
107 my $obj = bless { text => $text }, $class;
108 if ( $text =~ /^\s*(?:#\s*+(.*?)\s*)?$/ ) {
109 if ( defined $1 ) {
110 $obj->{comment} = $1;
111 delete $obj->{text};
112 }
113 return $obj;
114 }
115 local ( $date, @tags, @description, $is_beginning, $is_note );
116 if ( $text =~ $re ) {
117  
118 # must use to_string to obtain text
119 delete $obj->{text};
120 my @time = split /\s++/, $date;
121 $date = DateTime->new(
122 year => $time[0],
123 month => $time[1],
124 day => $time[2],
125 hour => $time[3],
126 minute => $time[4],
127 second => $time[5],
128 time_zone => tz,
129 );
130 $obj->{time} = $date;
131 if ($is_beginning) {
132 my %tags = map { $_ => 1 } @tags;
133 $obj->{tags} =
134 [ map { ( my $v = $_ ) =~ s/\\(.)/$1/g; $v } sort keys %tags ];
135 $obj->{description} = [
136 map {
137 ( my $v = $_ ) =~ s/\\(.)/$1/g;
138 $v =~ s/^\s++|\s++$//g;
139 $v =~ s/\s++/ /g;
140 $v
141 } @description
142 ];
143 $obj->{note} = 1 if $is_note;
144 }
145 else {
146 $obj->{done} = 1;
147 }
148 return $obj;
149 }
150 else {
151 $obj->{malformed} = 1;
152 }
153 return $obj;
154 }
155  
156  
157 sub clone {
158 my ($self) = @_;
159 my $clone = bless {}, ref $self;
160 if ( $self->is_malformed ) {
161 $clone->{malformed} = 1;
162 $clone->text = $self->text;
163 }
164 elsif ( $self->is_event ) {
165 $clone->time = $self->time->clone;
166 if ( $self->is_beginning ) {
167 $clone->{note} = 1 if $self->is_note;
168 $clone->tags = [ @{ $self->tags } ];
169 $clone->description = [ @{ $self->description } ];
170 }
171 }
172 else {
173 $clone->comment = $self->comment if $self->is_comment;
174 $clone->text = $self->text if exists $self->{text};
175 }
176 return $clone;
177 }
178  
179  
180 sub to_string {
181 my ($self) = @_;
182 return $self->{text} if exists $self->{text};
183 if ( $self->is_event ) {
184 my $text = $self->time_stamp;
185 $text .= $self->is_note ? '<NOTE>' : ':';
186 if ( $self->is_beginning ) {
187 $self->tags ||= [];
188 my %tags = map { $_ => 1 } @{ $self->tags };
189 $text .= join ' ', map { s/([:\\\s])/\\$1/g; $_ } sort keys %tags;
190 $text .= ':';
191 $self->description ||= [];
192 $text .= join ';',
193 map { ( my $d = $_ ) =~ s/([;\\])/\\$1/g; $d }
194 @{ $self->description };
195 }
196 else {
197 $text .= 'DONE';
198 }
199 return $text;
200 }
201 elsif ( $self->is_comment ) {
202 return '# ' . $self->comment;
203 }
204 }
205  
206  
207 sub time_stamp {
208 my ( $self, $time ) = @_;
209 $time ||= $self->time;
210 return sprintf '%d %2s %2s %2s %2s %2s', $time->year, $time->month,
211 $time->day, $time->hour,
212 $time->minute, $time->second;
213 }
214  
215 # a bunch of attributes, here for convenience
216  
217  
218 sub text : lvalue {
219 $_[0]->{text};
220 }
221  
222  
223 sub tags : lvalue {
224 $_[0]->{tags};
225 }
226  
227  
228 sub comment : lvalue {
229 $_[0]->{comment};
230 }
231  
232  
233 sub time : lvalue {
234 $_[0]->{time};
235 }
236  
237  
238 sub description : lvalue {
239 $_[0]->{description};
240 }
241  
242 # a bunch of tests
243  
244  
245 sub is_malformed { exists $_[0]->{malformed} }
246  
247  
248 sub is_beginning { exists $_[0]->{tags} }
249  
250  
251 sub is_end { $_[0]->{done} }
252  
253  
254 sub is_note { $_[0]->{note} }
255  
256  
257 sub is_event { $_[0]->{time} }
258  
259  
260 sub is_endpoint { $_[0]->{time} && !$_[0]->{note} }
261  
262  
263 sub is_comment { exists $_[0]->{comment} }
264  
265  
266 sub tags_unspecified { $_[0]->{tags_unspecified} }
267  
268  
269 sub is_blank {
270 !( $_[0]->is_malformed || $_[0]->is_comment || $_[0]->is_event );
271 }
272  
273 # some useful methods
274  
275  
276 sub comment_out {
277 my ($self) = @_;
278 my $text = $self->to_string;
279 delete $self->{$_} for keys %$self;
280 $self->{comment} = $text;
281 return $self;
282 }
283  
284  
285 sub all_tags {
286 my ( $self, @tags ) = @_;
287 return unless $self->tags;
288 my %tags = map { $_ => 1 } @{ $self->{tags} };
289 for my $tag (@tags) {
290 return unless $tags{$tag};
291 }
292 return 1;
293 }
294  
295  
296 sub exists_tag {
297 my ( $self, @tags ) = @_;
298 return unless $self->tags;
299 my %tags = map { $_ => 1 } @{ $self->{tags} };
300 for my $tag (@tags) {
301 return 1 if $tags{$tag};
302 }
303 return;
304 }
305  
306 1;
307  
308 __END__