[Jifty-commit] r4824 - in Jifty-DBI/trunk: lib/Jifty/DBI/Filter t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Jan 11 15:16:26 EST 2008


Author: trs
Date: Fri Jan 11 15:16:23 2008
New Revision: 4824

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Duration.pm
   Jifty-DBI/trunk/t/06filter_duration.t

Log:
 r30707 at zot:  tom | 2008-01-11 15:15:56 -0500
 Better error checking


Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Duration.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Duration.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Duration.pm	Fri Jan 11 15:16:23 2008
@@ -16,7 +16,8 @@
 =head2 encode
 
 If value is defined, then encode it using
-L<Time::Duration::Parse/parse_duration>, otherwise do nothing.
+L<Time::Duration::Parse/parse_duration>, otherwise do nothing.  If the value
+can't be parsed, then set it to undef.
 
 =cut
 
@@ -26,13 +27,20 @@
     my $value_ref = $self->value_ref;
     return unless defined $$value_ref and length $$value_ref;
 
-    # Convert hh:mm(::ss)? to something Time::Duration::Parse understands
+    # Convert hh:mm(:ss)? to something Time::Duration::Parse understands
     $$value_ref =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g;
     $$value_ref =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
 
-    $$value_ref = Time::Duration::Parse::parse_duration($$value_ref);
+    my ($parsed) = eval { Time::Duration::Parse::parse_duration($$value_ref) };
 
-    return 1;
+    if ( not $@ ) {
+        $$value_ref = $parsed;
+        return 1;
+    }
+    else {
+        $$value_ref = undef;
+        return;
+    }
 }
 
 =head2 decode
@@ -47,7 +55,8 @@
     my $self = shift;
 
     my $value_ref = $self->value_ref;
-    return unless defined $$value_ref and length $$value_ref;
+    return unless defined $$value_ref and length $$value_ref
+              and $$value_ref =~ /^\s*\d+\s*$/;
 
     $$value_ref = Time::Duration::concise(Time::Duration::duration_exact($$value_ref));
 }

Modified: Jifty-DBI/trunk/t/06filter_duration.t
==============================================================================
--- Jifty-DBI/trunk/t/06filter_duration.t	(original)
+++ Jifty-DBI/trunk/t/06filter_duration.t	Fri Jan 11 15:16:23 2008
@@ -6,7 +6,7 @@
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 37;
+use constant TESTS_PER_DRIVER => 42;
 
 eval "use Time::Duration ()";
 if ($@) {
@@ -21,6 +21,7 @@
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;
 
+my @bad_input        = ('foo');
 my @duration_input   = ('3h5m', '3:05', '3:04:60', '3h 0:05', '1h 2:04:60');
 my $duration_output  = '3h5m';
 my $duration_seconds = 11100;
@@ -46,6 +47,18 @@
         isa_ok($ret, 'DBI::st', 'init schema');
     }
 
+    for my $input ( @bad_input ) {
+        my $rec = TestApp::User->new( handle => $handle );
+        isa_ok($rec, 'Jifty::DBI::Record');
+
+        my ($id) = $rec->create( my_data => $input );
+        ok($id, 'created record');
+        ok($rec->load($id), 'loaded record');
+        is($rec->id, $id, 'record id matches');
+        
+        is($rec->my_data, undef, 'my_data output is undef');
+    }
+
     for my $input ( @duration_input ) {
         my $rec = TestApp::User->new( handle => $handle );
         isa_ok($rec, 'Jifty::DBI::Record');


More information about the Jifty-commit mailing list