File Coverage

blib/lib/Catmandu/Fix/expand_date.pm
Criterion Covered Total %
statement 19 19 100.0
branch 5 8 62.5
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 29 33 87.8


line stmt bran cond sub pod time code
1             package Catmandu::Fix::expand_date;
2              
3 1     1   101286 use Catmandu::Sane;
  1         3  
  1         7  
4              
5             our $VERSION = '1.2020';
6              
7 1     1   8 use Moo;
  1         2  
  1         6  
8 1     1   329 use namespace::clean;
  1         3  
  1         4  
9 1     1   821 use Catmandu::Fix::Has;
  1         2  
  1         6  
10              
11             my $DATE_REGEX = qr{
12             ^([0-9]{4})
13             (?: [:-] ([0-9]{1,2})
14             (?: [:-] ([0-9]{1,2}) )?
15             )?
16             }x;
17              
18             with 'Catmandu::Fix::Inlineable';
19              
20             has date_field => (fix_arg => 1, default => sub {'date'});
21              
22             sub fix {
23 2     2 0 15 my ($self, $data) = @_;
24 2 50       11 if (my $date = $data->{$self->date_field}) {
25 2 50       21 if (my ($y, $m, $d) = $date =~ $DATE_REGEX) {
26 2         8 $data->{year} = $y;
27 2 50       9 $data->{month} = 1 * $m if $m;
28 2 100       8 $data->{day} = 1 * $d if $d;
29             }
30             }
31 2         9 $data;
32             }
33              
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =head1 NAME
41              
42             Catmandu::Fix::expand_date - expand a date field into year, month and date
43              
44             =head1 NOTE
45              
46             This package is DEPRECATED and will be removed in the future.
47             Please use L<Catmandu::Fix::split_date>.
48              
49             Reasons:
50              
51             =over 4
52              
53             =item
54              
55             it writes directly in the root of the hash, which is a different
56             behaviour compared to all the other fixes (sum, count, hash, array ..)
57              
58             =item
59              
60             it adds the new keys in a different location, instead of "in place".
61              
62             =item
63              
64             its behaviour cannot be changed without breaking its current use
65              
66             =back
67              
68             =head1 SYNOPSIS
69              
70             # {date => "2001-09-11"}
71             expand_date()
72             # => {year => 2001, month => "9", day => "11", date => "2001-09-11"}
73              
74             # {datestamp => "2001:09"}
75             expand_date(datestamp)
76             # => {year => 2001, month => "9", datestamp => "2001:09"}
77              
78             =head1 DESCRIPTION
79              
80             The date field is expanded if it contains a year, optionally followed by
81             numeric month and day, each separated by C<-> or C<:>.
82              
83              
84             =cut