[Jifty-commit] r5031 - in Jifty-DBI/branches/tisql: lib/Jifty/DBI/Filter t

Jifty commits jifty-commit at lists.jifty.org
Tue Feb 5 21:17:37 EST 2008


Author: ruz
Date: Tue Feb  5 21:17:37 2008
New Revision: 5031

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

Log:
 r4785 at cubic-pc (orig r4784):  trs | 2008-01-04 06:21:57 +0300
  r30589 at zot:  tom | 2008-01-03 22:20:24 -0500
  Add a duration filter which uses Time::Duration and Time::Duration::Parse.  Stores durations as seconds in the database and converts them to English strings for output.
 


Added: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Filter/Duration.pm
==============================================================================
--- (empty file)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Filter/Duration.pm	Tue Feb  5 21:17:37 2008
@@ -0,0 +1,56 @@
+package Jifty::DBI::Filter::Duration;
+
+use warnings;
+use strict;
+
+use base qw|Jifty::DBI::Filter|;
+use Time::Duration qw();
+use Time::Duration::Parse qw();
+
+=head1 NAME
+
+Jifty::DBI::Filter::Duration - Encodes time durations
+
+=head1 DESCRIPTION
+
+=head2 encode
+
+If value is defined, then encode it using
+L<Time::Duration::Parse/parse_duration>, otherwise do nothing.
+
+=cut
+
+sub encode {
+    my $self = shift;
+
+    my $value_ref = $self->value_ref;
+    return unless defined $$value_ref;
+
+    $$value_ref = Time::Duration::Parse::parse_duration($$value_ref);
+
+    return 1;
+}
+
+=head2 decode
+
+If value is defined, then decode it using
+L<Time::Duration/duration_exact>, otherwise do nothing.
+
+=cut
+
+sub decode {
+    my $self = shift;
+
+    my $value_ref = $self->value_ref;
+    return unless defined $$value_ref;
+
+    $$value_ref = Time::Duration::duration_exact($$value_ref);
+}
+
+=head1 SEE ALSO
+
+L<Jifty::DBI::Filter>, L<Time::Duration>, L<Time::Duration::Parse>
+
+=cut
+
+1;

Added: Jifty-DBI/branches/tisql/t/06filter_duration.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/branches/tisql/t/06filter_duration.t	Tue Feb  5 21:17:37 2008
@@ -0,0 +1,114 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 8;
+
+eval "use Time::Duration ()";
+if ($@) {
+    plan skip_all => "Time::Duration not installed";
+}
+
+eval "use Time::Duration::Parse ()";
+if ($@) {
+    plan skip_all => "Time::Duration::Parse not installed";
+}
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+my $duration_string  = '3 hours and 5 minutes';
+my $duration_seconds = 11100;
+
+foreach my $d (@available_drivers) {
+SKIP: {
+    unless (has_schema('TestApp::User', $d)) {
+        skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+    }
+
+    unless (should_test($d)) {
+        skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+    }
+
+    diag("start testing with '$d' handle") if $ENV{TEST_VERBOSE};
+
+    my $handle = get_handle($d);
+    connect_handle($handle);
+    isa_ok($handle->dbh, 'DBI::db');
+
+    {
+        my $ret = init_schema('TestApp::User', $handle);
+        isa_ok($ret, 'DBI::st', 'init schema');
+    }
+
+   my $rec = TestApp::User->new( handle => $handle );
+   isa_ok($rec, 'Jifty::DBI::Record');
+
+   my ($id) = $rec->create( my_data => $duration_string );
+   ok($id, 'created record');
+   ok($rec->load($id), 'loaded record');
+   is($rec->id, $id, 'record id matches');
+   
+   is($rec->my_data, $duration_string, 'my_data matches initial data');
+
+   # undef/NULL
+   $rec->set_my_data;
+   is($rec->my_data, undef, 'set undef value');
+
+   cleanup_schema('TestApp', $handle);
+   disconnect_handle($handle);
+}
+}
+
+package TestApp::User;
+use base qw/ Jifty::DBI::Record /;
+
+1;
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE table users (
+    id integer primary key,
+    my_data integer
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id integer auto_increment primary key,
+    my_data integer
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id serial primary key,
+    my_data integer
+)
+EOF
+
+}
+
+BEGIN {
+    use Jifty::DBI::Schema;
+
+    use Jifty::DBI::Record schema {
+    column my_data =>
+        type is 'integer',
+        filters are qw/ Jifty::DBI::Filter::Duration /;
+    }
+}
+


More information about the Jifty-commit mailing list