Blame lib/DateTime/Format/Builder/Parser/Quick.pm

Packit 9002b2
package DateTime::Format::Builder::Parser::Quick;
Packit 9002b2
{
Packit 9002b2
  $DateTime::Format::Builder::Parser::Quick::VERSION = '0.81';
Packit 9002b2
}
Packit 9002b2
use strict;
Packit 9002b2
use warnings;
Packit 9002b2
use vars qw( %dispatch_data );
Packit 9002b2
use Params::Validate qw( SCALAR OBJECT CODEREF validate );
Packit 9002b2
use base qw( DateTime::Format::Builder::Parser );
Packit 9002b2
Packit 9002b2
Packit 9002b2
Packit 9002b2
__PACKAGE__->valid_params(
Packit 9002b2
    Quick => {
Packit 9002b2
        type      => SCALAR | OBJECT,
Packit 9002b2
        callbacks => {
Packit 9002b2
            good_classname => sub {
Packit 9002b2
                ( ref $_[0] ) or ( $_[0] =~ /^\w+[:'\w+]*\w+/ );
Packit 9002b2
            },
Packit 9002b2
        }
Packit 9002b2
    },
Packit 9002b2
    method => {
Packit 9002b2
        optional => 1,
Packit 9002b2
        type     => SCALAR | CODEREF,
Packit 9002b2
    },
Packit 9002b2
);
Packit 9002b2
Packit 9002b2
sub create_parser {
Packit 9002b2
    my ( $self, %args ) = @_;
Packit 9002b2
    my $class  = $args{Quick};
Packit 9002b2
    my $method = $args{method};
Packit 9002b2
    $method = 'parse_datetime' unless defined $method;
Packit 9002b2
    eval "use $class";
Packit 9002b2
    die $@ if $@;
Packit 9002b2
Packit 9002b2
    return sub {
Packit 9002b2
        my ( $self, $date ) = @_;
Packit 9002b2
        return unless defined $date;
Packit 9002b2
        my $rv = eval { $class->$method($date) };
Packit 9002b2
        return $rv if defined $rv;
Packit 9002b2
        return;
Packit 9002b2
    };
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
1;
Packit 9002b2
Packit 9002b2
# ABSTRACT: Use another formatter, simply
Packit 9002b2
Packit 9002b2
__END__
Packit 9002b2
Packit 9002b2
=pod
Packit 9002b2
Packit 9002b2
=head1 NAME
Packit 9002b2
Packit 9002b2
DateTime::Format::Builder::Parser::Quick - Use another formatter, simply
Packit 9002b2
Packit 9002b2
=head1 VERSION
Packit 9002b2
Packit 9002b2
version 0.81
Packit 9002b2
Packit 9002b2
=head1 SYNOPSIS
Packit 9002b2
Packit 9002b2
    use DateTime::Format::Builder (
Packit 9002b2
    parsers => { parse_datetime => [
Packit 9002b2
        { Quick => 'DateTime::Format::HTTP' },
Packit 9002b2
        { Quick => 'DateTime::Format::Mail' },
Packit 9002b2
        { Quick => 'DateTime::Format::IBeat' },
Packit 9002b2
    ]});
Packit 9002b2
Packit 9002b2
is the same as:
Packit 9002b2
Packit 9002b2
    use DateTime::Format::HTTP;
Packit 9002b2
    use DateTime::Format::Mail;
Packit 9002b2
    use DateTime::Format::IBeat;
Packit 9002b2
Packit 9002b2
    use DateTime::Format::Builder (
Packit 9002b2
    parsers => { parse_datetime => [
Packit 9002b2
        sub { eval { DateTime::Format::HTTP->parse_datetime( $_[1] ) } },
Packit 9002b2
        sub { eval { DateTime::Format::Mail->parse_datetime( $_[1] ) } },
Packit 9002b2
        sub { eval { DateTime::Format::IBeat->parse_datetime( $_[1] ) } },
Packit 9002b2
    ]});
Packit 9002b2
Packit 9002b2
(These two pieces of code can both be found in the test
Packit 9002b2
suite; one as F<quick.t>, the other as F<fall.t>.)
Packit 9002b2
Packit 9002b2
=head1 DESCRIPTION
Packit 9002b2
Packit 9002b2
C<Quick> adds a parser that allows some shortcuts when
Packit 9002b2
writing fairly standard and mundane calls to other
Packit 9002b2
formatting modules.
Packit 9002b2
Packit 9002b2
=head1 SPECIFICATION
Packit 9002b2
Packit 9002b2
C<Quick> has two keys, one optional.
Packit 9002b2
Packit 9002b2
The C<Quick> keyword should have an argument of either an
Packit 9002b2
object or a class name. If it's a class name then the class
Packit 9002b2
is C<use>d.
Packit 9002b2
Packit 9002b2
The C<method> keyword is optional with a default of
Packit 9002b2
C<parse_datetime>. It's either name of the method to invoke
Packit 9002b2
on the object, or a reference to a piece of code.
Packit 9002b2
Packit 9002b2
In any case, the resultant code ends up looking like:
Packit 9002b2
Packit 9002b2
     my $rv = $Quick->$method( $date );
Packit 9002b2
Packit 9002b2
=head1 SUPPORT
Packit 9002b2
Packit 9002b2
See L<DateTime::Format::Builder> for details.
Packit 9002b2
Packit 9002b2
=head1 SEE ALSO
Packit 9002b2
Packit 9002b2
C<datetime@perl.org> mailing list.
Packit 9002b2
Packit 9002b2
http://datetime.perl.org/
Packit 9002b2
Packit 9002b2
L<perl>, L<DateTime>,
Packit 9002b2
L<DateTime::Format::Builder>
Packit 9002b2
Packit 9002b2
=head1 AUTHORS
Packit 9002b2
Packit 9002b2
=over 4
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
Dave Rolsky <autarch@urth.org>
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
Iain Truskett
Packit 9002b2
Packit 9002b2
=back
Packit 9002b2
Packit 9002b2
=head1 COPYRIGHT AND LICENSE
Packit 9002b2
Packit 9002b2
This software is Copyright (c) 2013 by Dave Rolsky.
Packit 9002b2
Packit 9002b2
This is free software, licensed under:
Packit 9002b2
Packit 9002b2
  The Artistic License 2.0 (GPL Compatible)
Packit 9002b2
Packit 9002b2
=cut