| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package HTML::FormHandlerX::Field::DateTimeNatural; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: a datetime field with natural language parsing. |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
30537
|
use version; our $VERSION = version->declare('v0.6'); |
|
|
1
|
|
|
|
|
2593
|
|
|
|
1
|
|
|
|
|
6
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
595
|
use HTML::FormHandler::Moose; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
extends 'HTML::FormHandler::Field::Text'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use MooseX::Types::DateTime; |
|
11
|
|
|
|
|
|
|
use DateTime::Format::Natural; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'datetime_format_natural' => ( |
|
14
|
|
|
|
|
|
|
is => 'ro', |
|
15
|
|
|
|
|
|
|
isa => 'DateTime::Format::Natural', |
|
16
|
|
|
|
|
|
|
lazy_build => 1, |
|
17
|
|
|
|
|
|
|
required => 1, |
|
18
|
|
|
|
|
|
|
); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'datetime' => ( |
|
21
|
|
|
|
|
|
|
is => 'rw', |
|
22
|
|
|
|
|
|
|
isa => 'DateTime', |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'lang' => ( |
|
26
|
|
|
|
|
|
|
is => 'rw', |
|
27
|
|
|
|
|
|
|
isa => 'Str', |
|
28
|
|
|
|
|
|
|
); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has 'format' => ( |
|
31
|
|
|
|
|
|
|
is => 'rw', |
|
32
|
|
|
|
|
|
|
isa => 'Str', |
|
33
|
|
|
|
|
|
|
); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has 'prefer_future' => ( |
|
36
|
|
|
|
|
|
|
is => 'rw', |
|
37
|
|
|
|
|
|
|
isa => 'Bool', |
|
38
|
|
|
|
|
|
|
); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has 'time_zone' => ( |
|
41
|
|
|
|
|
|
|
is => 'rw', |
|
42
|
|
|
|
|
|
|
isa => 'DateTime::TimeZone', |
|
43
|
|
|
|
|
|
|
coerce => 1, |
|
44
|
|
|
|
|
|
|
); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has 'daytime' => ( |
|
47
|
|
|
|
|
|
|
is => 'rw', |
|
48
|
|
|
|
|
|
|
isa => 'HashRef', |
|
49
|
|
|
|
|
|
|
); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
our $class_messages = { 'date_invalid' => 'Date is invalid.', }; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub get_class_messages { |
|
54
|
|
|
|
|
|
|
my $self = shift; |
|
55
|
|
|
|
|
|
|
return { %{ $self->next::method }, %{$class_messages}, }; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub validate { |
|
59
|
|
|
|
|
|
|
my $self = shift; |
|
60
|
|
|
|
|
|
|
my $value = $self->value; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
## validate |
|
63
|
|
|
|
|
|
|
my $parser = $self->datetime_format_natural; |
|
64
|
|
|
|
|
|
|
my $dt = $parser->parse_datetime($value); |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
## update to inflated value or set error |
|
67
|
|
|
|
|
|
|
if ($parser->success) { |
|
68
|
|
|
|
|
|
|
$self->_set_value($dt); |
|
69
|
|
|
|
|
|
|
} else { |
|
70
|
|
|
|
|
|
|
$self->add_error($self->get_message('date_invalid')); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
## return |
|
74
|
|
|
|
|
|
|
return $parser->success; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _build_datetime_format_natural { |
|
78
|
|
|
|
|
|
|
my $self = shift; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my %attributes; |
|
81
|
|
|
|
|
|
|
my $form = $self->form; |
|
82
|
|
|
|
|
|
|
foreach |
|
83
|
|
|
|
|
|
|
my $attr (qw/datetime time_zone lang format prefer_future daytime/) |
|
84
|
|
|
|
|
|
|
{ |
|
85
|
|
|
|
|
|
|
if (defined $self->$attr) { |
|
86
|
|
|
|
|
|
|
$attributes{$attr} = $self->$attr; |
|
87
|
|
|
|
|
|
|
} elsif ($form |
|
88
|
|
|
|
|
|
|
&& $form->meta->find_attribute_by_name($attr) |
|
89
|
|
|
|
|
|
|
&& defined $form->$attr) |
|
90
|
|
|
|
|
|
|
{ |
|
91
|
|
|
|
|
|
|
$attributes{$attr} = $form->$attr; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
## Fix time_zone if set, because DT::F::N can only accept time zone |
|
96
|
|
|
|
|
|
|
## names and not objects, at the time of writing this module. |
|
97
|
|
|
|
|
|
|
if ($attributes{time_zone}) { |
|
98
|
|
|
|
|
|
|
$attributes{time_zone} = $attributes{time_zone}->name; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
return DateTime::Format::Natural->new(%attributes); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
105
|
|
|
|
|
|
|
use namespace::autoclean; |
|
106
|
|
|
|
|
|
|
1; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__END__ |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=pod |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 NAME |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
HTML::FormHandlerX::Field::DateTimeNatural - a datetime field with natural language parsing. |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 VERSION |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
version v0.6 |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
This field is a simple text input field type, but it understands natural |
|
123
|
|
|
|
|
|
|
language and dates. Most of the functionality is inherited from |
|
124
|
|
|
|
|
|
|
L<DateTime::Format::Natural>. To see a list of dates it can understand see |
|
125
|
|
|
|
|
|
|
L<DateTime::Format::Natural::Lang::EN>. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
has_field 'date' => ( |
|
128
|
|
|
|
|
|
|
type => 'DateTimeNatural', |
|
129
|
|
|
|
|
|
|
time_zone => 'UTC', # optional |
|
130
|
|
|
|
|
|
|
); |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 METHODS |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
This field supports all of the methods inherited from |
|
135
|
|
|
|
|
|
|
L<HTML::FormHandler::Field::Text>, as well as all of the parameters offered by |
|
136
|
|
|
|
|
|
|
L<DateTime::Format::Natural>, all of which are optional. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
In addition to that, it will try to obtain the values for these attributes |
|
139
|
|
|
|
|
|
|
from the parent form class. E.g. you can set the C<time_zone> attribute on the |
|
140
|
|
|
|
|
|
|
form class, and all of the C<DateTimeNatural> fields will automatically have |
|
141
|
|
|
|
|
|
|
the time zone set. |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Here is the list of the methods, please refer to original module for |
|
144
|
|
|
|
|
|
|
their description: |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over 4 |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * time_zone |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * datetime |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * lang |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * format |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * prefer_future |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * daytime |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=back |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=over 4 |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item * L<HTML::FormHandler> |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * L<HTML::FormHandler::Field::Text> |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item * L<DateTime::Format::Natural> |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * L<DateTime::Format::Natural::Lang::EN> |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=back |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Roman F. <romanf@cpan.org> |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Roman F.. |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
185
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |