[Bast-commits] r3467 - in trunk/DBIx-Class-InflateColumn-ISBN: . lib/DBIx/Class/InflateColumn t t/lib t/lib/DBICTest/Schema t/var

penguin at dev.catalyst.perl.org penguin at dev.catalyst.perl.org
Sat Jun 2 14:33:09 GMT 2007


Author: penguin
Date: 2007-06-02 14:33:08 +0100 (Sat, 02 Jun 2007)
New Revision: 3467

Modified:
   trunk/DBIx-Class-InflateColumn-ISBN/Changes
   trunk/DBIx-Class-InflateColumn-ISBN/README
   trunk/DBIx-Class-InflateColumn-ISBN/TODO
   trunk/DBIx-Class-InflateColumn-ISBN/lib/DBIx/Class/InflateColumn/ISBN.pm
   trunk/DBIx-Class-InflateColumn-ISBN/t/01-isbn.t
   trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest.pm
   trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest/Schema/Library.pm
   trunk/DBIx-Class-InflateColumn-ISBN/t/lib/sqlite.sql
   trunk/DBIx-Class-InflateColumn-ISBN/t/var/test.db
Log:
Updated DBIC::IF::ISBN.

Modified: trunk/DBIx-Class-InflateColumn-ISBN/Changes
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/Changes	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/Changes	2007-06-02 13:33:08 UTC (rev 3467)
@@ -1,7 +1,12 @@
 Revision history for DBIx::Class::InflateColumn::ISBN
 
+0.04000
+    - Added as_string option to also store hyphens.
     - Fixed 'validation error checked' test to work for future
       versions of Business::ISBN.
+    - "Datatype is too small" is now an exception rather than a
+      warning.
+    - More tests.
 
 0.03000 Tue May 29 17:50:28 2007
     - Tweaked Makefile.PL and TODO files.

Modified: trunk/DBIx-Class-InflateColumn-ISBN/README
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/README	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/README	2007-06-02 13:33:08 UTC (rev 3467)
@@ -3,7 +3,7 @@
     from columns.
 
 VERSION
-    Version 0.03000
+    Version 0.04000
 
 SYNOPSIS
     Load this component and declare columns as ISBNs with the appropriate
@@ -17,6 +17,7 @@
                 size => 13,
                 is_nullable => 0,
                 is_isbn => 1,
+                as_string => 0,
             }
         );
 
@@ -24,6 +25,10 @@
     possible for ISBNs to contain the character X. Old style ISBNs are 10
     characters, not including hyphens, but new style ones are 13 characters.
 
+    The "as_string" attribute is optional, and if set to 1 then values will
+    be stored in the database with hyphens in the appopriate places. In this
+    case, an extra 3 characters will be required.
+
     Then you can treat the specified column as a Business::ISBN object.
 
         print 'ISBN: ', $book->isbn->as_string;

Modified: trunk/DBIx-Class-InflateColumn-ISBN/TODO
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/TODO	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/TODO	2007-06-02 13:33:08 UTC (rev 3467)
@@ -1,5 +1,5 @@
-- Allow the option of storing ISBNs in the database with hyphens?
-
 - Add a warning when trying to inflate ISBNs with an invalid checksum?
 
+- Option to automatically convert to ISBN13?
+
 - Other things I haven't thought of yet.

Modified: trunk/DBIx-Class-InflateColumn-ISBN/lib/DBIx/Class/InflateColumn/ISBN.pm
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/lib/DBIx/Class/InflateColumn/ISBN.pm	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/lib/DBIx/Class/InflateColumn/ISBN.pm	2007-06-02 13:33:08 UTC (rev 3467)
@@ -3,7 +3,7 @@
 use warnings;
 use strict;
 
-our $VERSION = '0.03000';
+our $VERSION = '0.04000';
 
 use base qw/DBIx::Class/;
 __PACKAGE__->mk_classdata('isbn_class');
@@ -15,7 +15,7 @@
 
 =head1 VERSION
 
-Version 0.03000
+Version 0.04000
 
 =head1 SYNOPSIS
 
@@ -29,13 +29,18 @@
             size => 13,
             is_nullable => 0,
             is_isbn => 1,
+            as_string => 0,
         }
     );
 
-It has to be a varchar rather than a simple integer given that it is possible for
-ISBNs to contain the character X. Old style ISBNs are 10 characters, not including
-hyphens, but new style ones are 13 characters.
+It has to be a varchar rather than a simple integer given that it is possible
+for ISBNs to contain the character X. Old style ISBNs are 10 characters, not
+including hyphens, but new style ones are 13 characters.
 
+The C<as_string> attribute is optional, and if set to 1 then values will be
+stored in the database with hyphens in the appopriate places. In this case, an
+extra 3 characters will be required.
+
 Then you can treat the specified column as a Business::ISBN object.
 
     print 'ISBN: ', $book->isbn->as_string;
@@ -67,8 +72,12 @@
 
     return unless defined $info->{'is_isbn'};
 
-    warn "Datatype is too small" if ($info->{'size'} && $info->{'size'} < 10);
-    warn "Datatype must not be integer" if ($info->{'data_type'} eq 'integer');
+    if ( $info->{'size'} && ($info->{'size'} < 10 ||
+      ($info->{'as_string'} && $info->{'size'} < 13)) ) {
+        $self->throw_exception("ISBN field datatype is too small");
+    }
+    $self->throw_exception("ISBN field datatype must not be integer")
+        if ($info->{'data_type'} eq 'integer');
 
     my $isbn_class = $info->{'isbn_class'} || $self->isbn_class || 'Business::ISBN';
 
@@ -77,8 +86,12 @@
 
     $self->inflate_column(
         $column => {
-            inflate => sub { return $isbn_class->new(sprintf("%010s", shift)); },
-            deflate => sub { return shift->isbn; },
+            inflate => sub {
+                return $isbn_class->new(sprintf("%010s", shift));
+            },
+            deflate => sub {
+                $info->{'as_string'} ? shift->as_string : shift->isbn;
+            },
         }
     );
 }

Modified: trunk/DBIx-Class-InflateColumn-ISBN/t/01-isbn.t
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/t/01-isbn.t	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/t/01-isbn.t	2007-06-02 13:33:08 UTC (rev 3467)
@@ -4,7 +4,7 @@
 
 use lib qw(t/lib);
 use DBICTest;
-use Test::More tests => 19;
+use Test::More tests => 23;
 use Business::ISBN;
 
 my $schema = DBICTest->init_schema();
@@ -15,6 +15,8 @@
 my $book = $rs->find(1);
 isa_ok($book->isbn, 'Business::ISBN', 'data inflated to right class');
 is($book->isbn->isbn, $code, 'data correctly inflated');
+isa_ok($book->full_isbn, 'Business::ISBN', 'full_isbn data inflated to right class');
+is($book->full_isbn->isbn, $code, 'full_isbn data correctly inflated');
 
 $book = $rs->find(2);
 isa_ok($book->isbn, 'Business::ISBN', 'data inflated to right class');
@@ -43,19 +45,24 @@
 
 my $isbn = Business::ISBN->new('0374292795');
 $book = $rs->create({ book => 'foo', isbn => $isbn });
-isa_ok($book, 'DBICTest::Library', 'create with object');
+isa_ok($book, 'DBICTest::Library', 'create with object (1)');
 is($book->get_column('isbn'), $isbn->isbn, 'numeric code correctly deflated');
 
+$isbn = Business::ISBN->new('0575073772');
+$book = $rs->create({ book => 'The Last Hero', isbn => $isbn, full_isbn => $isbn });
+isa_ok($book, 'DBICTest::Library', 'create with object (2)');
+is($book->get_column('full_isbn'), $isbn->as_string, 'as_string code correctly deflated');
+
 $isbn = Business::ISBN->new('071351700X');
 $book = $rs->create({ book => 'Elementary Mechanics', isbn => $isbn });
-isa_ok($book, 'DBICTest::Library', 'create with object');
+isa_ok($book, 'DBICTest::Library', 'create with object (3)');
 is($book->get_column('isbn'), $isbn->isbn, 'code with X correctly deflated');
 ok($book->isbn->is_valid, 'validation checked');
 
 $isbn = Business::ISBN->new('0713517001');
 my $host = $rs->create({ book => 'Elementary Mechanics', isbn => $isbn });
-isa_ok($host, 'DBICTest::Library', 'create with object');
-is($host->get_column('isbn'), $isbn->isbn, 'code with X correctly deflated');
+isa_ok($host, 'DBICTest::Library', 'create with object (4)');
+is($host->get_column('isbn'), $isbn->isbn, 'invalid code correctly deflated');
 isnt($host->isbn->is_valid_checksum, Business::ISBN::GOOD_ISBN, 'validation error checked');
 
 #$isbn = Business::ISBN->new('foobar');

Modified: trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest/Schema/Library.pm
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest/Schema/Library.pm	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest/Schema/Library.pm	2007-06-02 13:33:08 UTC (rev 3467)
@@ -20,7 +20,14 @@
         size        => 13,
         is_nullable => 0,
         is_isbn     => 1,
-    }
+    },
+    full_isbn => {
+        data_type   => 'varchar',
+        size        => 16,
+        is_nullable => 1,
+        is_isbn     => 1,
+        as_string   => 1,
+    },
 );
 
 __PACKAGE__->set_primary_key('id');

Modified: trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest.pm
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest.pm	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/t/lib/DBICTest.pm	2007-06-02 13:33:08 UTC (rev 3467)
@@ -105,8 +105,8 @@
     my $schema = shift;
 
     $schema->populate('Library', [
-        [ qw/id book isbn/ ],
-        [ 1, 'HTML for the World Wide Web: With XHTML and CSS', '0321430840' ],
+        [ qw/id book isbn full_isbn / ],
+        [ 1, 'HTML for the World Wide Web: With XHTML and CSS', '0321430840', '032-143-084-0' ],
         [ 2, 'Final Fantasy X Official Strategy Guide', '190351133X' ],
     ]);
 

Modified: trunk/DBIx-Class-InflateColumn-ISBN/t/lib/sqlite.sql
===================================================================
--- trunk/DBIx-Class-InflateColumn-ISBN/t/lib/sqlite.sql	2007-06-01 14:43:16 UTC (rev 3466)
+++ trunk/DBIx-Class-InflateColumn-ISBN/t/lib/sqlite.sql	2007-06-02 13:33:08 UTC (rev 3467)
@@ -1,5 +1,6 @@
 CREATE TABLE library (
 	id INTEGER NOT NULL PRIMARY KEY,
 	book TEXT NOT NULL,
-	isbn VARHCAR(13) NOT NULL UNIQUE
+	isbn VARHCAR(13) NOT NULL UNIQUE,
+	full_isbn VARHCAR(16) UNIQUE
 );

Modified: trunk/DBIx-Class-InflateColumn-ISBN/t/var/test.db
===================================================================
(Binary files differ)




More information about the Bast-commits mailing list