[Bast-commits] r5762 - in DBIx-Class-InflateColumn-FS/1.000/trunk: . lib/DBIx/Class/InflateColumn t t/lib/My/TestSchema t/var

semifor at dev.catalyst.perl.org semifor at dev.catalyst.perl.org
Tue Mar 17 17:08:31 GMT 2009


Author: semifor
Date: 2009-03-17 17:08:31 +0000 (Tue, 17 Mar 2009)
New Revision: 5762

Added:
   DBIx-Class-InflateColumn-FS/1.000/trunk/t/02-uniq.t
   DBIx-Class-InflateColumn-FS/1.000/trunk/t/var/
   DBIx-Class-InflateColumn-FS/1.000/trunk/t/var/testfile.txt
Modified:
   DBIx-Class-InflateColumn-FS/1.000/trunk/Changes
   DBIx-Class-InflateColumn-FS/1.000/trunk/MANIFEST.SKIP
   DBIx-Class-InflateColumn-FS/1.000/trunk/README
   DBIx-Class-InflateColumn-FS/1.000/trunk/lib/DBIx/Class/InflateColumn/FS.pm
   DBIx-Class-InflateColumn-FS/1.000/trunk/t/lib/My/TestSchema/Book.pm
Log:
fs_new_on_update: optionally create new file name on update (Moritz Onken)

    - fs_new_on_update: new file name on update (Moritz Onken)
    - fix: ->delete with multiple fs_columns (Moritz Onken)
    - fs_file_name: factored out file name creation (Moritz Onken)


Modified: DBIx-Class-InflateColumn-FS/1.000/trunk/Changes
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/Changes	2009-03-17 15:31:08 UTC (rev 5761)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/Changes	2009-03-17 17:08:31 UTC (rev 5762)
@@ -1,3 +1,8 @@
+0.01002 2009-03-17
+    - fs_new_on_update: new file name on update (Moritz Onken)
+    - fix: ->delete with multiple fs_columns (Moritz Onken)
+    - fs_file_name: factored out file name creation (Moritz Onken)
+
 0.01001 2008-12-02 11:32:00
         - added SUPPORT section to POD
         - use ->result_source->column_info (mst)

Modified: DBIx-Class-InflateColumn-FS/1.000/trunk/MANIFEST.SKIP
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/MANIFEST.SKIP	2009-03-17 15:31:08 UTC (rev 5761)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/MANIFEST.SKIP	2009-03-17 17:08:31 UTC (rev 5762)
@@ -32,9 +32,6 @@
 # avoid OS X finder files
 \.DS_Store$
 
-# Don't ship the test db
-^t/var
-
 # Don't ship the last dist we built :)
 \.tar\.gz$
 

Modified: DBIx-Class-InflateColumn-FS/1.000/trunk/README
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/README	2009-03-17 15:31:08 UTC (rev 5761)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/README	2009-03-17 17:08:31 UTC (rev 5762)
@@ -13,6 +13,12 @@
               is_fs_column => 1,
               fs_column_path => '/var/lib/myapp/myfiles',
           },
+          file_2 => {
+              data_type => 'TEXT',
+              is_fs_column => 1,
+              fs_column_path => '/var/lib/myapp/myfiles',
+              fs_new_on_update => 1
+          },
       );
       __PACKAGE__->set_primary_key('id');
 
@@ -35,11 +41,17 @@
     names. Up to 256 sub-directories will be created, as needed. Override
     "_fs_column_dirs" in a derived class to change this behavior.
 
+    "fs_new_on_update" will create a new file name if the file has been
+    updated.
+
 METHODS
   register_column
-  _fs_column_storage
+  fs_file_name
     Provides the file naming algorithm. Override this method to change it.
 
+    This method is called with two parameters: The name of the column and
+    the "column_info" object.
+
   _fs_column_dirs
     Returns the sub-directory components for a given file name. Override it
     to provide a deeper directory tree or change the algorithm.
@@ -54,9 +66,9 @@
     Inflates a file column to a Path::Class::File object.
 
   _deflate_fs_column
-    Deflates a file column to the arbitrary value, 1. In the database, a
-    file column is just a place holder for inflation/deflation. The actual
-    file lives in the file system.
+    Deflates a file column to its storage path name, relative to
+    "fs_column_path". In the database, a file column is just a place holder
+    for inflation/deflation. The actual file lives in the file system.
 
   table
     Overridden to provide a hook for specifying the resultset_class. If you

Modified: DBIx-Class-InflateColumn-FS/1.000/trunk/lib/DBIx/Class/InflateColumn/FS.pm
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/lib/DBIx/Class/InflateColumn/FS.pm	2009-03-17 15:31:08 UTC (rev 5761)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/lib/DBIx/Class/InflateColumn/FS.pm	2009-03-17 17:08:31 UTC (rev 5762)
@@ -8,7 +8,7 @@
 use File::Copy;
 use Path::Class;
 
-our $VERSION = '0.01001';
+our $VERSION = '0.01002';
 
 =head1 NAME
 
@@ -27,6 +27,12 @@
           is_fs_column => 1,
           fs_column_path => '/var/lib/myapp/myfiles',
       },
+      file_2 => {
+          data_type => 'TEXT',
+          is_fs_column => 1,
+          fs_column_path => '/var/lib/myapp/myfiles',
+          fs_new_on_update => 1
+      },
   );
   __PACKAGE__->set_primary_key('id');
 
@@ -49,6 +55,8 @@
 256 sub-directories will be created, as needed.  Override C<_fs_column_dirs> in
 a derived class to change this behavior.
 
+C<fs_new_on_update> will create a new file name if the file has been updated.
+
 =cut
 
 =head1 METHODS
@@ -76,24 +84,32 @@
     });
 }
 
-=head2 _fs_column_storage
+=head2 fs_file_name
 
 Provides the file naming algorithm.  Override this method to change it.
 
+This method is called with two parameters: The name of the column and the
+C<< column_info >> object.
+
 =cut
 
+sub fs_file_name {
+    my ($self, $column, $column_info) = @_;
+    return $self->get_uuid;
+}
+
 sub _fs_column_storage {
-    my ( $self, $column ) = @_;
+    my ( $self, $column, $deflate ) = @_;
 
     my $column_info = $self->result_source->column_info($column);
     $self->throw_exception("$column is not an fs_column")
         unless $column_info->{is_fs_column};
 
-    if ( my $filename = $self->{_column_data}{$column} ) {
+    if ( (!$column_info->{fs_new_on_update} || !$deflate) && ( my $filename = $self->{_column_data}{$column} ) ) {
         return Path::Class::File->new($column_info->{fs_column_path}, $filename);
     }
     else {
-        $filename = $self->get_uuid;
+        $filename = $self->fs_file_name($column, $column_info);
         return Path::Class::File->new(
             $column_info->{fs_column_path},
             $self->_fs_column_dirs($filename),
@@ -127,6 +143,7 @@
 
     for ( $self->columns ) {
         if ( $self->result_source->column_info($_)->{is_fs_column} ) {
+            next unless $self->$_;
             $self->$_->remove;
         }
     }
@@ -177,7 +194,6 @@
 
 sub _inflate_fs_column {
     my ( $self, $column, $value ) = @_;
-
     return unless defined $value;
 
     return $self->_fs_column_storage($column);
@@ -185,20 +201,26 @@
 
 =head2 _deflate_fs_column
 
-Deflates a file column to the arbitrary value, 1.  In the database, a
-file column is just a place holder for inflation/deflation.  The actual
-file lives in the file system.
+Deflates a file column to its storage path name, relative to C<fs_column_path>.
+In the database, a file column is just a place holder for inflation/deflation.
+The actual file lives in the file system.
 
 =cut
 
 sub _deflate_fs_column {
     my ( $self, $column, $value ) = @_;
-
+    
     # already deflated?
     return $value unless ref $value;
-
-    my $file = $self->_fs_column_storage($column);
-    if ( $value ne $file ) {
+    my $fs_new_on_update = $self->result_source->column_info($column)->{fs_new_on_update};
+    my $file = $self->_fs_column_storage($column, 1);
+    
+    if ( $fs_new_on_update && (my $oldfile = $self->get_column($column)) ) {
+        my $column_info = $self->result_source->column_info($column);
+        Path::Class::File->new($column_info->{fs_column_path}, $oldfile)->remove;
+    }
+    
+    if ( $fs_new_on_update || $value ne $file ) {
         File::Path::mkpath([$file->dir]);
 
         # get a filehandle if we were passed a Path::Class::File

Added: DBIx-Class-InflateColumn-FS/1.000/trunk/t/02-uniq.t
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/t/02-uniq.t	                        (rev 0)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/t/02-uniq.t	2009-03-17 17:08:31 UTC (rev 5762)
@@ -0,0 +1,53 @@
+use strict;
+use warnings;
+use Test::More tests => 5;
+use Path::Class::File;
+use Path::Class::Dir;
+
+
+use lib qw(t/lib);
+use DBICx::TestDatabase;
+use IO::File;
+
+
+my $schema = DBICx::TestDatabase->new('My::TestSchema');
+my $rs = $schema->resultset('Book');
+
+# we'll use *this* file as our content
+# TODO: Copy it or create something else so errant tests don't inadvertently
+# delete it!
+
+my $book = $rs->create({
+    name => 'Alice in Wonderland',
+});
+
+my $base = $book->column_info('cover_image_2')->{fs_column_path};
+
+
+my $fh = new IO::File "t/var/testfile.txt";
+
+$book->cover_image_2($fh);
+
+$book->update;
+
+my $name;
+
+like($name = $book->cover_image_2, qr{^\Q$base\E});
+
+$fh = new IO::File "t/var/testfile.txt";
+
+$book->cover_image_2($fh);
+
+$book->update;
+
+ok(!-e $name, "old file does not exist anymore");
+
+like($name = $book->cover_image_2, qr{^\Q$base\E});
+
+ok(-e $name);
+
+$book = $schema->resultset("Book")->first;
+
+is($book->cover_image_2, $name, "name did not change on retrieve");
+
+

Modified: DBIx-Class-InflateColumn-FS/1.000/trunk/t/lib/My/TestSchema/Book.pm
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/t/lib/My/TestSchema/Book.pm	2009-03-17 15:31:08 UTC (rev 5761)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/t/lib/My/TestSchema/Book.pm	2009-03-17 17:08:31 UTC (rev 5762)
@@ -22,6 +22,13 @@
         is_fs_column => 1,
         fs_column_path => tempdir(CLEANUP => 1),
     },
+    cover_image_2 => {
+        data_type => 'TEXT',
+        is_nullable => 1,
+        is_fs_column => 1,
+        fs_column_path => tempdir(CLEANUP => 1),
+        fs_new_on_update => 1
+    },
 );
 __PACKAGE__->set_primary_key(qw/id/);
 

Added: DBIx-Class-InflateColumn-FS/1.000/trunk/t/var/testfile.txt
===================================================================
--- DBIx-Class-InflateColumn-FS/1.000/trunk/t/var/testfile.txt	                        (rev 0)
+++ DBIx-Class-InflateColumn-FS/1.000/trunk/t/var/testfile.txt	2009-03-17 17:08:31 UTC (rev 5762)
@@ -0,0 +1 @@
+Simple file with content for tests.




More information about the Bast-commits mailing list