File Coverage

blib/lib/Pod/CopyrightYears.pm
Criterion Covered Total %
statement 62 63 98.4
branch 12 14 85.7
condition n/a
subroutine 12 12 100.0
pod 4 4 100.0
total 90 93 96.7


line stmt bran cond sub pod time code
1             package Pod::CopyrightYears;
2              
3 5     5   138317 use strict;
  5         37  
  5         150  
4 5     5   26 use warnings;
  5         10  
  5         144  
5              
6 5     5   958 use Class::Utils qw(set_params);
  5         26731  
  5         187  
7 5     5   155 use Error::Pure qw(err);
  5         10  
  5         181  
8 5     5   2354 use Pod::Abstract;
  5         142793  
  5         178  
9 5     5   2290 use String::UpdateYears qw(update_years);
  5         3008  
  5         96  
10              
11             our $VERSION = 0.02;
12              
13             # Constructor.
14             sub new {
15 14     14 1 10888 my ($class, @params) = @_;
16              
17             # Create object.
18 14         37 my $self = bless {}, $class;
19              
20             # Pod file to update.
21 14         43 $self->{'pod_file'} = undef;
22              
23             # Section names to update.
24 14         33 $self->{'section_names'} = [
25             'LICENSE AND COPYRIGHT',
26             ];
27              
28             # Process parameters.
29 14         57 set_params($self, @params);
30              
31 13 100       199 if (! defined $self->{'pod_file'}) {
32 1         6 err "Parameter 'pod_file' is required.";
33             }
34 12         76 $self->{'pod_abstract'} = Pod::Abstract->load_file($self->{'pod_file'});
35              
36 12         46837 return $self;
37             }
38              
39             sub change_years {
40 4     4 1 33 my ($self, $year) = @_;
41              
42 4 50       17 if (! defined $year) {
43 0         0 $year = (localtime(time))[5] + 1900;
44             }
45              
46 4         11 foreach my $pod_node ($self->license_sections) {
47 2         6 $self->_iterate_node($pod_node, $year);
48             }
49              
50 4         10 return;
51             }
52              
53             sub license_sections {
54 7     7 1 25 my $self = shift;
55              
56 7         14 my @pod_nodes;
57 7         11 foreach my $section (@{$self->{'section_names'}}) {
  7         20  
58 7         36 my ($pod_node) = $self->{'pod_abstract'}->select('/head1[@heading =~ {'.$section.'}]');
59 7 100       4353 if (defined $pod_node) {
60 4         14 push @pod_nodes, $pod_node;
61             }
62             }
63              
64 7         28 return @pod_nodes;
65             }
66              
67             sub pod {
68 8     8 1 36 my $self = shift;
69              
70 8         25 my $pod = $self->{'pod_abstract'}->pod;
71 8         3396 chomp $pod;
72 8         16 my $ret = $pod;
73 8 100       41 if ($pod =~ m/=cut\s+$/ms) {
74 6         17 $ret = substr $pod, 0, -2;
75 6         15 $ret .= "\n";
76             } else {
77 2         5 $ret .= "\n";
78             }
79              
80 8         23 return $ret;
81             }
82              
83             sub _iterate_node {
84 8     8   18 my ($self, $pod_node, $year) = @_;
85              
86 8 50       15 if (defined $pod_node->children) {
87 8         90 foreach my $child ($pod_node->children) {
88 10 100       69 if ($child->type eq ':text') {
89 4         28 $self->_change_years($child, $year);
90             } else {
91 6         43 $self->_iterate_node($child, $year);
92             }
93             }
94             }
95              
96 8         33 return;
97             }
98              
99             sub _change_years {
100 4     4   6 my ($self, $pod_node, $year) = @_;
101              
102 4         10 my $text = $pod_node->pod;
103 4         229 my $updated = update_years($text, {}, $year);
104 4 100       183 if ($updated) {
105 2         6 $pod_node->body($updated);
106             }
107              
108 4         25 return;
109             }
110              
111             1;
112              
113             __END__