1package DateTime::Format::Natural::Duration; 2 3use strict; 4use warnings; 5 6use DateTime::Format::Natural::Duration::Checks; 7use List::MoreUtils qw(all); 8 9our $VERSION = '0.06'; 10 11sub _pre_duration 12{ 13 my $self = shift; 14 my ($date_strings) = @_; 15 16 my $check_if = sub 17 { 18 my $sub = shift; 19 my $class = join '::', (__PACKAGE__, 'Checks'); 20 my $check = $class->can($sub) or die "$sub() not found in $class"; 21 22 return $check->($self->{data}->{duration}, $date_strings, @_); 23 }; 24 25 my ($present, $extract, $adjust, @indexes); 26 27 if ($check_if->('for', \$present)) { 28 @{$self->{insert}}{qw(datetime trace)} = do { 29 my $dt = $self->parse_datetime($present); 30 ($dt, $self->{traces}[0]); 31 }; 32 } 33 elsif ($check_if->('first_to_last', \$extract)) { 34 if (my ($complete) = $date_strings->[1] =~ $extract) { 35 $date_strings->[0] .= " $complete"; 36 } 37 } 38 elsif ($check_if->('from_count_to_count', \$extract, \$adjust, \@indexes)) { 39 if (my ($complete) = $date_strings->[$indexes[0]] =~ $extract) { 40 $adjust->($date_strings, $indexes[1], $complete); 41 } 42 } 43} 44 45sub _post_duration 46{ 47 my $self = shift; 48 my ($queue, $traces) = @_; 49 50 my %assign = ( 51 datetime => $queue, 52 trace => $traces, 53 ); 54 if (all { exists $self->{insert}{$_} } keys %assign) { 55 unshift @{$assign{$_}}, $self->{insert}{$_} foreach keys %assign; 56 } 57} 58 59sub _save_state 60{ 61 my $self = shift; 62 my %args = @_; 63 64 return if %{$self->{state}}; 65 66 unless ($args{valid_expression}) { 67 %{$self->{state}} = %args; 68 } 69} 70 71sub _restore_state 72{ 73 my $self = shift; 74 75 my %state = %{$self->{state}}; 76 77 if (%state) { 78 $state{valid_expression} 79 ? $self->_set_valid_exp 80 : $self->_unset_valid_exp; 81 82 $state{failure} 83 ? $self->_set_failure 84 : $self->_unset_failure; 85 86 defined $state{error} 87 ? $self->_set_error($state{error}) 88 : $self->_unset_error; 89 } 90} 91 921; 93__END__ 94 95=head1 NAME 96 97DateTime::Format::Natural::Duration - Duration hooks and state handling 98 99=head1 SYNOPSIS 100 101 Please see the DateTime::Format::Natural documentation. 102 103=head1 DESCRIPTION 104 105The C<DateTime::Format::Natural::Duration> class contains code to alter 106tokens before parsing and to insert DateTime objects in the resulting 107queue. Furthermore, there's code to save the state of the first failing 108parse and restore it after the duration has been processed. 109 110=head1 SEE ALSO 111 112L<DateTime::Format::Natural> 113 114=head1 AUTHOR 115 116Steven Schubiger <schubiger@cpan.org> 117 118=head1 LICENSE 119 120This program is free software; you may redistribute it and/or 121modify it under the same terms as Perl itself. 122 123See L<http://dev.perl.org/licenses/> 124 125=cut 126