| 1 | package DateTime::Format::Builder::Parser::Regex; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 24 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 24 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 24 | our $VERSION = '0.83'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 24 | ▶ | use Params::Validate qw( validate ARRAYREF SCALARREF HASHREF CODEREF ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 24 | ▶ | use parent 'DateTime::Format::Builder::Parser::generic'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | __PACKAGE__->valid_params( | ||||||||||||||
| 13 | |||||||||||||||
| 14 | # How to match | ||||||||||||||
| 15 | params => { | ||||||||||||||
| 16 | type => ARRAYREF, # mapping $1,$2,... to new args | ||||||||||||||
| 17 | }, | ||||||||||||||
| 18 | regex => { | ||||||||||||||
| 19 | type => SCALARREF, | ||||||||||||||
| 20 | callbacks => { | ||||||||||||||
| 21 | 68 | ▶ | 'is a regex' => sub { ref(shift) eq 'Regexp' } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | } | ||||||||||||||
| 23 | }, | ||||||||||||||
| 24 | |||||||||||||||
| 25 | # How to create | ||||||||||||||
| 26 | extra => { | ||||||||||||||
| 27 | type => HASHREF, | ||||||||||||||
| 28 | optional => 1, | ||||||||||||||
| 29 | }, | ||||||||||||||
| 30 | constructor => { | ||||||||||||||
| 31 | type => CODEREF | ARRAYREF, | ||||||||||||||
| 32 | optional => 1, | ||||||||||||||
| 33 | callbacks => { | ||||||||||||||
| 34 | 6 | ▶ | 'array has 2 elements' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 35 | 6 | ▶ | ref( $_[0] ) eq 'ARRAY' ? ( @{ $_[0] } == 2 ) : 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | } | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | }, | ||||||||||||||
| 39 | 24 | ); | |||||||||||||
| 40 | |||||||||||||||
| 41 | 41 | ▶ | sub do_match { | ||||||||||||
|
Subroutine
do_match: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 42 | 41 | my $self = shift; | |||||||||||||
| 43 | 41 | my $date = shift; | |||||||||||||
| 44 | 41 | my @matches = $date =~ $self->{regex}; | |||||||||||||
| 45 | 41 | ▶ | return @matches ? \@matches : undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 30 | ▶ | sub post_match { | ||||||||||||
|
Subroutine
post_match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 49 | 30 | my $self = shift; | |||||||||||||
| 50 | 30 | my ( $date, $matches, $p ) = @_; | |||||||||||||
| 51 | |||||||||||||||
| 52 | # Fill %p from match | ||||||||||||||
| 53 | 30 | @{$p}{ @{ $self->{params} } } = @$matches; | |||||||||||||
| 54 | 30 | return; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 30 | ▶ | sub make { | ||||||||||||
|
Subroutine
make: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 58 | 30 | my $self = shift; | |||||||||||||
| 59 | 30 | my ( $date, $dt, $p ) = @_; | |||||||||||||
| 60 | 30 | my @args = ( %$p, %{ $self->{extra} } ); | |||||||||||||
| 61 | 30 | ▶ | if ( my $cons = $self->{constructor} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 5 | ▶ | if ( ref $cons eq 'ARRAY' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 63 | 3 | my ( $class, $method ) = @$cons; | |||||||||||||
| 64 | 3 | return $class->$method(@args); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | elsif ( ref $cons eq 'CODE' ) { | ||||||||||||||
| 67 | 2 | return $self->$cons(@args); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | else { | ||||||||||||||
| 71 | 25 | return DateTime->new(@args); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 34 | ▶ | sub create_parser { | ||||||||||||
|
Subroutine
create_parser: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 76 | 34 | my ( $self, %args ) = @_; | |||||||||||||
| 77 | 34 | ▶ | $args{extra} ||= {}; | ||||||||||||
|
Condition100%2/2
$args{"extra"} ||= {}
MC/DC100%1/1
$args{"extra"} ||= {}
$args{"extra"}
Truth table100%2/2
A
A= $args{"extra"}
| |||||||||||||||
| 78 | 34 | ▶ | unless ( ref $self ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | 34 | $self = $self->new(%args); | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | # Create our parser | ||||||||||||||
| 83 | return $self->generic_parser( | ||||||||||||||
| 84 | ( | ||||||||||||||
| 85 | 136 | ▶ | map { exists $args{$_} ? ( $_ => $args{$_} ) : () } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | qw( | ||||||||||||||
| 87 | on_match on_fail preprocess postprocess | ||||||||||||||
| 88 | ) | ||||||||||||||
| 89 | ), | ||||||||||||||
| 90 | label => $args{label}, | ||||||||||||||
| 91 | 34 | ); | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 24 | 1; | |||||||||||||
| 95 | |||||||||||||||
| 96 | # ABSTRACT: Regex based date parsing | ||||||||||||||
| 97 | |||||||||||||||
| 98 | __END__ | ||||||||||||||