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