Sledge::Plugin::Inflate::DateTime

tokuhirom on 2006-12-02T06:52:04

I think, generate DateTime instance from CGI.pm/Apache::Request is very frequent and very painful.

Yes, I wrote the Sledge::Plugin::Inflate::DateTime.

You just write the

package Your::Pages;
use Sledge::Plguin::Inflate;
use Sledge::Plguin::Inflate::DateTime;

__PACKAGE__->add_inflate_rule_ymd('date');
then, you can inflate DateTime object easily.
sub dispatch_foo {
  my $self = shift;
  $self->stash->{date} = $self->r->inflate('date');
}


And, Sledge::Plguin::Inflate::DateTime and HTML::DateSelector go together!

package Sledge::Plugin::Inflate::DateTime;
use strict;
use warnings;
use DateTime;

sub import { my $pkg = caller(0);

no strict 'refs'; *{"$pkg\::add_inflate_rule_ymd"} = sub { my ($class, $key, $datetime) = @_; $datetime ||= 'DateTime';

$class->add_inflate_rule( $key => sub { my ( $self, ) = @_; my %args;

$args{year} = $self->r->param("$key\_year"); $args{month} = $self->r->param("$key\_month"); $args{day} = $self->r->param("$key\_day"); if ( $args{year} && $args{month} && $args{day} ) { return $datetime->new(%args); } return; } ); }; }

1; __END__

=head1 SYSNOPSIS

package Your::Pages; use Sledge::Plugin::Inflate::DateTime;

# simple way __PACKAGE__->add_inflate_rule_ymd('from');

# you can use your customized DateTime class. __PACKAGE__->add_inflate_rule_ymd('from' => 'Your::DateTime');