datetime now handles optional using a wrap on value_string
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Mutable / DateTime.pm
1 package Reaction::UI::ViewPort::Field::Mutable::DateTime;
2
3 use Reaction::Class;
4 use Time::ParseDate;
5 use DateTime;
6
7 class 'Reaction::UI::ViewPort::Field::Mutable::DateTime',
8   is 'Reaction::UI::ViewPort::Field::DateTime', which {
9
10   does 'Reaction::UI::ViewPort::Field::Role::Mutable';
11
12   has value_string =>
13     ( is => 'rw', isa => 'Str', lazy_build => 1, trigger_adopt('value_string') );
14
15   around value_string => sub {
16     my $orig = shift;
17     my $self = shift;
18     if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq ''
19         && !$self->value_is_required) {
20       $self->clear_value;
21       return undef;
22     }
23     return $self->$orig(@_);
24   };
25
26   implements adopt_value_string => as {
27     my ($self) = @_;
28     my $value = $self->value_string;
29     my ($epoch) = Time::ParseDate::parsedate($value);
30     if (defined $epoch) {
31       my $dt = 'DateTime'->from_epoch( epoch => $epoch );
32       $self->value($dt);
33     } else {
34       $self->message("Could not parse date or time");
35     }
36   };
37
38   around accept_events => sub { ('value_string', shift->(@_)) };
39
40 };
41
42 1;
43
44
45 =head1 NAME
46
47 Reaction::UI::ViewPort::Field::DateTime
48
49 =head1 DESCRIPTION
50
51 =head1 METHODS
52
53 =head2 value_string
54
55 Accessor for the string representation of the DateTime object.
56
57 =head2 value_string_default_format
58
59 By default it is set to "%F %H:%M:%S".
60
61 =head1 SEE ALSO
62
63 =head2 L<DateTime>
64
65 =head2 L<Reaction::UI::ViewPort::Field>
66
67 =head1 AUTHORS
68
69 See L<Reaction::Class> for authors.
70
71 =head1 LICENSE
72
73 See L<Reaction::Class> for the license.
74
75 =cut