File Coverage

blib/lib/Parse/Crontab.pm
Criterion Covered Total %
statement 73 75 97.3
branch 19 22 86.3
condition 3 3 100.0
subroutine 18 18 100.0
pod 3 5 60.0
total 116 123 94.3


line stmt bran cond sub pod time code
1             package Parse::Crontab;
2 2     2   48878 use 5.008_001;
  2         7  
  2         92  
3 2     2   11 use strict;
  2         4  
  2         84  
4 2     2   11 use warnings;
  2         8  
  2         88  
5 2     2   12 use Carp;
  2         4  
  2         223  
6              
7             our $VERSION = '0.05';
8              
9 2     2   1328 use Mouse;
  2         56635  
  2         11  
10 2     2   2197 use Path::Class;
  2         108316  
  2         139  
11              
12 2     2   854 use Parse::Crontab::Entry::Env;
  2         115  
  2         82  
13 2     2   830 use Parse::Crontab::Entry::Job;
  2         159  
  2         65  
14 2     2   689 use Parse::Crontab::Entry::Comment;
  2         93  
  2         47  
15 2     2   637 use Parse::Crontab::Entry::Empty;
  2         95  
  2         49  
16 2     2   635 use Parse::Crontab::Entry::Error;
  2         96  
  2         324  
17              
18             has file => (
19             is => 'ro',
20             );
21              
22             has content => (
23             is => 'ro',
24             isa => 'Str',
25             lazy => 1,
26             default => sub {
27             my $self = shift;
28             croak 'Attribute file or content is required!' unless defined $self->file;
29             Path::Class::file($self->file)->slurp;
30             },
31             );
32              
33             has entries => (
34             is => 'rw',
35             isa => 'ArrayRef[Parse::Crontab::Entry]',
36             default => sub {[]},
37             auto_deref => 1,
38             );
39              
40             has env => (
41             is => 'rw',
42             isa => 'HashRef',
43             default => sub {{}},
44             );
45              
46             has verbose => (
47             is => 'rw',
48             isa => 'Bool',
49             default => 1,
50             );
51              
52             has has_user_field => (
53             is => 'rw',
54             isa => 'Bool',
55             default => undef,
56             );
57              
58 2     2   9 no Mouse;
  2         3  
  2         6  
59              
60             sub BUILD {
61 3     3 1 7 my $self = shift;
62              
63 3         4 my $line_number = 1;
64 3         40 for my $line (split /\r?\n/, $self->content) {
65 10         24 my $entry_class = 'Parse::Crontab::Entry::'. $self->_decide_entry_class($line);
66 10         181 my $entry = $entry_class->new(line => $line, line_number => $line_number, has_user_field => $self->has_user_field);
67 10         83 $line_number++;
68              
69 10 100 100     79 if ($entry_class eq 'Parse::Crontab::Entry::Env' && !$entry->is_error) {
70             # overwritten if same key already exists
71 1         5 $self->env->{$entry->key} = $entry->value;
72             }
73 10         12 push @{$self->entries}, $entry;
  10         35  
74             }
75              
76 3 50       15 if ($self->verbose) {
77 3         8 my $error = $self->error_messages;
78 3 100       98 warn $error if $error;
79 3         9 my $warn = $self->warning_messages;
80 3 100       131 warn $warn if $warn;
81             }
82             }
83              
84             sub is_valid {
85 2     2 1 1147 my $self = shift;
86              
87 2         10 for my $entry ($self->entries) {
88 6 100       23 return () if $entry->is_error;
89             }
90 1         5 1;
91             }
92              
93             sub error_messages {
94 4     4 0 5 my $self = shift;
95 4         5 my @errors;
96 4         14 for my $entry ($self->entries) {
97 14 100       61 push @errors, $entry->error_message if $entry->is_error;
98             }
99 4         12 join "\n", @errors;
100             }
101              
102             sub warning_messages {
103 3     3 0 4 my $self = shift;
104 3         2 my @errors;
105 3         10 for my $entry ($self->entries) {
106 10 100       25 push @errors, $entry->warning_message if $entry->has_warnings;
107             }
108 3         7 join "\n", @errors;
109             }
110              
111             sub jobs {
112 2     2 1 595 my $self = shift;
113 2         10 grep {$_->isa('Parse::Crontab::Entry::Job')} $self->entries;
  6         28  
114             }
115              
116             sub _decide_entry_class {
117 10     10   13 my ($self, $line) = @_;
118              
119 10 100       82 if ($line =~ /^#/) {
    50          
    100          
    50          
120 2         8 'Comment';
121             }
122             elsif ($line =~ /^\s*$/) {
123 0         0 'Empty';
124             }
125             elsif ($line =~ /^(?:@|\*|[0-9])/) {
126 6         17 'Job';
127             }
128             elsif ($line =~ /=/) {
129 2         8 'Env';
130             }
131             else {
132 0           'Error';
133             }
134             }
135              
136             __PACKAGE__->meta->make_immutable;
137             __END__