[Bps-public-commit] r9679 - in Date-Extract: . lib/Date

sartak at bestpractical.com sartak at bestpractical.com
Thu Nov 15 19:39:44 EST 2007


Author: sartak
Date: Thu Nov 15 19:39:44 2007
New Revision: 9679

Modified:
   Date-Extract/   (props changed)
   Date-Extract/lib/Date/Extract.pm
   Date-Extract/t/01-new.t

Log:
 r45229 at onn:  sartak | 2007-11-15 18:02:27 -0500
 Change the API a bit more


Modified: Date-Extract/lib/Date/Extract.pm
==============================================================================
--- Date-Extract/lib/Date/Extract.pm	(original)
+++ Date-Extract/lib/Date/Extract.pm	Thu Nov 15 19:39:44 2007
@@ -3,6 +3,11 @@
 use warnings;
 use DateTime;
 
+sub _croak {
+    require Carp;
+    Carp::croak @_;
+}
+
 =head1 NAME
 
 Date::Extract - simple date extraction
@@ -18,7 +23,7 @@
 =head1 SYNOPSIS
 
     my $parser = Date::Extract->new();
-    my $dt = $parser->extract_date($arbitrary_text)
+    my $dt = $parser->extract($arbitrary_text)
         or die "No date found.";
     return $dt->ymd;
 
@@ -52,22 +57,29 @@
 By default it will use the "floating" time zone. See the documentation for
 L<DateTime>.
 
-=item prefer_future
+=item prefers
+
+This argument decides what happens when an ambiguous date appears in the
+input. For example, "Friday" may refer to any number of Fridays. The valid
+options for this argument are:
+
+=over 4
+
+=item nearest
+
+Prefer the nearest date. This is the default.
 
-If (for example) a bare weekday appears, setting this option will interpret it
-as the nearest such weekday in the future. This is the default.
+=item future
 
-Note that this means that, on a Friday, "Friday" will return the next Friday.
+Prefer the closest future date.
 
-=item prefer_past
+=item past
 
-If (for example) a bare weekday appears, setting this option will interpret it
-as the nearest such weekday in the past.
+Prefer the closest past date.
 
-Note that this means that, on a Friday, "Friday" will return the previous
-Friday.
+=back
 
-=item conflict
+=item returns
 
 If the text has multiple possible dates, then this argument determines which
 date will be returned. By default it's 'first'.
@@ -108,18 +120,24 @@
 sub new {
     my $class = shift;
     my %args = (
-        conflict => 'first',
+        returns => 'first',
+        prefers => 'nearest',
         @_,
     );
 
-    if ($args{conflict} ne 'first'
-     && $args{conflict} ne 'last'
-     && $args{conflict} ne 'earliest'
-     && $args{conflict} ne 'latest'
-     && $args{conflict} ne 'all'
-     && $args{conflict} ne 'all_cron') {
-        require Carp;
-        Carp::croak "Invalid `conflict` passed to constructor: expected 'first', 'last', earliest', 'latest', 'all', or 'all_cron'.";
+    if ($args{returns} ne 'first'
+     && $args{returns} ne 'last'
+     && $args{returns} ne 'earliest'
+     && $args{returns} ne 'latest'
+     && $args{returns} ne 'all'
+     && $args{returns} ne 'all_cron') {
+        _croak "Invalid `returns` passed to constructor: expected 'first', 'last', earliest', 'latest', 'all', or 'all_cron'.";
+    }
+
+    if ($args{prefers} ne 'nearest'
+     && $args{prefers} ne 'past'
+     && $args{prefers} ne 'future') {
+        _croak "Invalid `prefers` passed to constructor: expected 'first', 'last', earliest', 'latest', 'all', or 'all_cron'.";
     }
 
     my $self = bless \%args, ref($class) || $class;
@@ -127,21 +145,48 @@
     return $self;
 }
 
-=head2 extract_date text => C<DateTime>
+=for subclasses
+
+This method will combine the arguments of parser->new and extract. Modify the
+"to" hash directly.
+
+=cut
+
+sub _combine_args {
+    shift;
+
+    my $from = shift;
+    my $to = shift;
+
+    $to->{prefers} ||= $from->{prefers};
+    $to->{returns} ||= $from->{returns};
+}
+
+=head2 extract text => C<DateTime>, ARGS
 
 Takes an arbitrary amount of text and extracts one or more dates from it. The
 return value will be zero or more C<DateTime> objects. If called in scalar
-context, the first will be returned, even if the C<conflict> argument specifies
+context, the first will be returned, even if the C<returns> argument specifies
 multiple possible return values.
 
-See the documentation of C<new> for the configuration of this method.
+See the documentation of C<new> for the configuration of this method. Any
+arguments passed into this method will trump those from the parser.
+
+You may reuse a parser for multiple calls to C<extract>.
 
-You may reuse a parser for multiple calls to C<extract_date>.
+You do not need to have an instantiated C<Date::Extract> object to call this
+method. Just C<< Date::Extract->extract($foo) >> will work.
 
 =cut
 
-sub extract_date {
-    my ($self, $text) = @_;
+sub extract {
+    my $self = shift;
+    my $text = shift;
+    my %args = @_;
+
+    # don't do this if called as a class method
+    $self->_combine_args($self, \%args)
+        if ref($self);
 }
 
 =head1 CAVEATS

Modified: Date-Extract/t/01-new.t
==============================================================================
--- Date-Extract/t/01-new.t	(original)
+++ Date-Extract/t/01-new.t	Thu Nov 15 19:39:44 2007
@@ -1,36 +1,37 @@
 #!perl -T
 use strict;
 use warnings;
-use Test::More tests => 24;
+use Test::More tests => 26;
 use Date::Extract;
 
 my $parser = Date::Extract->new();
 ok($parser, "got a parser out of Date::Extract->new");
 ok($parser->isa("Date::Extract"), "new parser is a Date::Extract object");
 
-# conflict {{{
-for my $preference (qw/first last earliest latest all all_cron/) {
-    $parser = Date::Extract->new(conflict => $preference);
-    ok($parser, "got a parser out of Date::Extract->new(conflict => 'first')");
+# returns {{{
+for my $opt (qw/first last earliest latest all all_cron/) {
+    $parser = Date::Extract->new(returns => $opt);
+    ok($parser, "got a parser out of Date::Extract->new(returns => '$opt')");
     ok($parser->isa("Date::Extract"), "new parser is a Date::Extract object");
 }
 
-$parser = eval { Date::Extract->new(conflict => "invalid argument") };
-ok(!$parser, "did NOT get a parser out of Date::Extract->new(conflict => 'invalid argument')");
-like($@, qr/Invalid `conflict` passed to constructor/, "(conflict => 'invalid argument') gave a sensible error message");
-like($@, qr/01-new\.t/, "invalid `conflict` error reported from caller's perspective");
+$parser = eval { Date::Extract->new(returns => "invalid argument") };
+ok(!$parser, "did NOT get a parser out of Date::Extract->new(returns => 'invalid argument')");
+like($@, qr/Invalid `returns` passed to constructor/, "(returns => 'invalid argument') gave a sensible error message");
+like($@, qr/01-new\.t/, "invalid `returns` error reported from caller's perspective");
 
 # }}}
-# prefer_future, prefer_past {{{
-for my $prefer (qw/prefer_future prefer_past/) {
-    $parser = Date::Extract->new($prefer => 1);
-    ok($parser, "got a parser out of Date::Extract->new($prefer => 1)");
+# prefer nearest, prefer future, prefer past {{{
+for my $opt (qw/nearest future past/) {
+    $parser = Date::Extract->new(prefer => $opt);
+    ok($parser, "got a parser out of Date::Extract->new(prefers => '$opt')");
     ok($parser->isa("Date::Extract"), "new parser is a Date::Extract object");
 }
 
-$parser = Date::Extract->new(prefer_future => 1, prefer_past => 1);
-ok(!$parser, "did NOT get a parser when specifying prefer future AND past");
-like($@, qr/Conflicting options `prefer_future` and `prefer_past` found in constructor/, "prefer_future and prefer_past together gives a specific error");
-like($@, qr/01-new\.t/, "conflicting options error reported from caller's perspective");
+$parser = eval { Date::Extract->new(prefers => "invalid argument") };
+ok(!$parser, "did NOT get a parser out of Date::Extract->new(prefers => 'invalid argument')");
+like($@, qr/Invalid `prefers` passed to constructor/, "(prefers => 'invalid argument') gave a sensible error message");
+like($@, qr/01-new\.t/, "invalid `prefers` error reported from caller's perspective");
+
 # }}}
 



More information about the Bps-public-commit mailing list