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   83671  
  1         3  
  1         6  
4             our $VERSION = '1.2018';
5              
6             use Moo;
7 1     1   7 use namespace::clean;
  1         1  
  1         4  
8 1     1   269 use Catmandu::Fix::Has;
  1         2  
  1         4  
9 1     1   612  
  1         2  
  1         5  
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 11 if (my ($y, $m, $d) = $date =~ $DATE_REGEX) {
24 2 50       9 $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       7 }
28 2 100       6 }
29             $data;
30             }
31 2         7  
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