X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FReaction%2FUI%2FViewPort%2FField%2FDateTime.pm;h=46fbb60ac4e1502eeb5b46d8ca1625095d71ca34;hb=7ea45b2aff36dd61953b1517a1cfb079dcea230c;hp=6d2958f1435695b0733491e3171219a4b99fe9e5;hpb=6ab43711ccd779eedce107001b300043e2056a0c;p=catagits%2FReaction.git diff --git a/lib/Reaction/UI/ViewPort/Field/DateTime.pm b/lib/Reaction/UI/ViewPort/Field/DateTime.pm index 6d2958f..46fbb60 100644 --- a/lib/Reaction/UI/ViewPort/Field/DateTime.pm +++ b/lib/Reaction/UI/ViewPort/Field/DateTime.pm @@ -1,89 +1,28 @@ package Reaction::UI::ViewPort::Field::DateTime; +use Scalar::Util 'blessed'; use Reaction::Class; -use Reaction::Types::DateTime; -use Time::ParseDate (); +use Reaction::Types::DateTime (); +use aliased 'Reaction::UI::ViewPort::Field'; -class DateTime is 'Reaction::UI::ViewPort::Field', which { +use namespace::clean -except => [ qw(meta) ]; +extends Field; - has '+value' => (isa => 'DateTime'); - #has '+layout' => (default => 'dt_textfield'); +has '+value' => (isa => Reaction::Types::DateTime::DateTime()); - has value_string => ( - isa => 'Str', is => 'rw', lazy_build => 1, - trigger_adopt('value_string') - ); - - has value_string_default_format => ( - isa => 'Str', is => 'rw', required => 1, default => sub { "%F %H:%M:%S" } - ); - - implements build_value_string => as { - my $self = shift; - - # XXX - # aha, I know why the fucker's lazy - # it's because if value's calculated - # it needs to be possible to clear it - # eval { $self->value } ... is probably the best solution atm - my $value = eval { $self->value }; - return '' unless $self->has_value; - my $format = $self->value_string_default_format; - return $value->strftime($format) if $value; - return ''; - }; - - implements adopt_value_string => as { - my ($self) = @_; - my $value = $self->value_string; - my ($epoch) = Time::ParseDate::parsedate($value, UK => 1); - if (defined $epoch) { - my $dt = 'DateTime'->from_epoch( epoch => $epoch ); - $self->value($dt); - } else { - $self->message("Could not parse date or time"); - $self->clear_value; - $self->needs_sync(1); - } - }; - - override accept_events => sub { - ('value_string', super()); - }; +has value_string_default_format => ( + isa => 'Str', is => 'rw', required => 1, default => sub { "%F %H:%M:%S" } +); +around _value_string_from_value => sub { + my $orig = shift; + my $self = shift; + my $format = $self->value_string_default_format; + return $self->$orig(@_)->strftime($format); }; -1; - -=head1 NAME - -Reaction::UI::ViewPort::Field::DateTime - -=head1 DESCRIPTION - -=head1 METHODS - -=head2 value_string +__PACKAGE__->meta->make_immutable; -Accessor for the string representation of the DateTime object. -=head2 value_string_default_format - -By default it is set to "%F %H:%M:%S". - -=head1 SEE ALSO - -=head2 L - -=head2 L - -=head1 AUTHORS - -See L for authors. - -=head1 LICENSE - -See L for the license. - -=cut +1;