Review a filter by date method
Method that allows me to filter by date a data structure list of hash that
contains articles. I would like to have your opinion on the code:
=method article_by_date
$articles->articles_by_date( month => 12 );
Return a list of articles filter by date specified.
input: month (Int) : optional, month to match
year (Int) : optional, year to match
output: Hashref: Details of article
=cut
sub articles_by_date {
state $check = compile(
$invocant,
slurpy Dict[
month => Optional[Int],
year => Optional[Int],
]
);
my ($self, $arg) = $check->(@_);
my $month = $arg->{month};
my $year = $arg->{year};
if ( ! defined($year) ) {
my $dt = DateTime->now;
$year = $dt->year;
}
my $date = defined($month)
? qr/^\d\d\/$month\/$year\s\d\d:\d\d:\d\d$/
: qr/^\d\d\/\d\d\/$year\s\d\d:\d\d:\d\d$/;
my @articles;
foreach my $article ( @{$self->_get_or_create_cache('articles')} ) {
$article->{date} =~ $date
and push(@articles, $article);
}
return \@articles;
}
Thanks
No comments:
Post a Comment