Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

tokuhirom (7396)

tokuhirom
  (email not shown publicly)
http://d.hatena.ne.jp/tokuhirom/

Journal of tokuhirom (7396)

Saturday December 02, 2006
01:52 AM

Sledge::Plugin::Inflate::DateTime

[ #31785 ]

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');

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.