Matt S Trout wrote:

> Bernhard Graf wrote:
> > Since I have a current DBIC working copy on my HD now, I also offer
> > this patch to be applied. It lets you set an inflator for multiple
> > columns at once, as described in my email one month ago:
> > http://lists.scsys.co.uk/pipermail/dbix-class/2006-October/002687.h
> >tml
>
> Again, NO TESTS.
>
> Please don't expect to me to apply any patch without tests unless
> it's a bugfix for something for which tests have already been
> provided.

I was afraid you will say this. ;-)
OK. I'm starting with the inflate_column patch, hoping the tests are as 
desired.

Changes in this patch:
- DBIx::Class::InflateColumn::inflate_column() can handle multiple
  columns to which the inflator is applied
- ergo the method is renamed to "inflate_columns" and has an alias
  called "inflate_column"

Join condition patch comes soon.
-- 
Bernhard Graf
Index: t/68inflate_columns.t
===================================================================
--- t/68inflate_columns.t	(revision 0)
+++ t/68inflate_columns.t	(revision 0)
@@ -0,0 +1,44 @@
+use strict;
+use warnings;  
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+eval { require DateTime::Format::MySQL };
+plan skip_all => "Need DateTime::Format::MySQL for inflation tests" if $@;
+
+plan tests => 9;
+
+DBICTest::Schema::Holiday->inflate_columns(qw/begins ends/ =>
+    { inflate => sub { DateTime::Format::MySQL->parse_date(shift) },
+      deflate => sub { DateTime::Format::MySQL->format_date(shift) } }
+);
+Class::C3->reinitialize;
+
+# inflation test
+my $holiday = $schema->resultset("Holiday")->find(2);
+
+is( ref($holiday->begins), 'DateTime', 'begins is a DateTime, ok' );
+is( ref($holiday->ends), 'DateTime', 'ends is a DateTime, ok' );
+
+is( $holiday->begins->mday, 2, 'inflated day of begins ok' );
+is( $holiday->ends->mday, 13, 'inflated day of ends ok' );
+
+# deflate test
+my $today = DateTime->today;
+my $later = $today->clone->add(weeks => 3);
+$holiday->begins($today);
+$holiday->ends($later);
+$holiday->update;
+
+($holiday) = $schema->resultset("Holiday")
+    ->search(begins => DateTime::Format::MySQL->format_date($today));
+ok($holiday, 'search holiday ok');
+isa_ok($holiday->begins, 'DateTime', 'returned holiday->begins');
+isa_ok($holiday->ends, 'DateTime', 'returned holiday->ends');
+is($holiday->begins, $today, 'deflate of begins ok' );
+is($holiday->ends, $later, 'deflate of ends ok' );
+
Index: t/lib/sqlite.sql
===================================================================
--- t/lib/sqlite.sql	(revision 2953)
+++ t/lib/sqlite.sql	(working copy)
@@ -253,7 +253,17 @@
   title varchar(100)
 );
 
+--
+-- Table: holiday
+--
+CREATE TABLE holiday (
+  id INTEGER PRIMARY KEY NOT NULL,
+  name varchar(100),
+  begins varchar(100),
+  ends varchar(100)
+);
 
+
 CREATE UNIQUE INDEX tktlnameunique_twokeytreelike on twokeytreelike (name);
 CREATE UNIQUE INDEX cd_artist_title_cd on cd (artist, title);
 CREATE UNIQUE INDEX track_cd_position_track on track (cd, position);
Index: t/lib/DBICTest/Schema/Holiday.pm
===================================================================
--- t/lib/DBICTest/Schema/Holiday.pm	(revision 0)
+++ t/lib/DBICTest/Schema/Holiday.pm	(revision 0)
@@ -0,0 +1,30 @@
+package DBICTest::Schema::Holiday;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table('holiday');
+
+__PACKAGE__->add_columns(
+  id => {
+    data_type => 'integer',
+    is_auto_increment => 1
+  },
+  name => {
+      data_type => 'varchar',
+      size      => '100',
+  },
+  begins => {
+      data_type => 'varchar',
+      size      => '100',
+  },
+  ends => {
+      data_type => 'varchar',
+      size      => '100',
+  },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
Index: t/lib/DBICTest/Schema.pm
===================================================================
--- t/lib/DBICTest/Schema.pm	(revision 2953)
+++ t/lib/DBICTest/Schema.pm	(working copy)
@@ -35,7 +35,7 @@
   ),
   qw/SelfRefAlias TreeLike TwoKeyTreeLike Event NoPrimaryKey/,
   qw/Collection CollectionObject TypedObject/,
-  qw/Owners BooksInLibrary/
+  qw/Owners BooksInLibrary Holiday/
 );
 
 1;
Index: t/lib/DBICTest.pm
===================================================================
--- t/lib/DBICTest.pm	(revision 2953)
+++ t/lib/DBICTest.pm	(working copy)
@@ -270,6 +270,14 @@
         [ 2, 1, "Dynamical Systems", "Library" ],
         [ 3, 2, "Best Recipe Cookbook", "Library" ],
     ]);
+
+    $schema->populate('Holiday', [
+        [ qw/id name begins ends/ ],
+        [ 1, 'Winter', '2006-12-22', '2007-01-05'],
+        [ 2, 'Easter', '2007-04-02', '2007-04-13'],
+        [ 3, 'Summer', '2007-07-12', '2007-08-25'],
+        [ 4, 'Autumn', '2007-10-15', '2007-10-20'],
+    ]);
 }
 
 1;
Index: lib/DBIx/Class/InflateColumn.pm
===================================================================
--- lib/DBIx/Class/InflateColumn.pm	(revision 2953)
+++ lib/DBIx/Class/InflateColumn.pm	(working copy)
@@ -29,20 +29,20 @@
 
 =head1 METHODS
 
-=head2 inflate_column
+=head2 inflate_columns
 
-Instruct L<DBIx::Class> to inflate the given column.
+Instruct L<DBIx::Class> to inflate the given columns.
 
-In addition to the column name, you must provide C<inflate> and
+In addition to the column names, you must provide C<inflate> and
 C<deflate> methods. The C<inflate> method is called when you access
 the field, while the C<deflate> method is called when the field needs
 to used by the database.
 
-For example, if you have a table C<events> with a timestamp field
-named C<insert_time>, you could inflate the column in the
-corresponding table class using something like:
+For example, if you have a table C<events> with two timestamp fields
+named C<insert_time> and C<update_time>, you could inflate the columns
+in the corresponding table class using something like:
 
-    __PACKAGE__->inflate_column('insert_time', {
+    __PACKAGE__->inflate_columns('insert_time', 'update_time', {
         inflate => sub { DateTime::Format::Pg->parse_datetime(shift); },
         deflate => sub { DateTime::Format::Pg->format_datetime(shift); },
     });
@@ -55,23 +55,34 @@
 row object itself. Thus you can call C<< ->result_source->schema->storage->dbh >> on
 it, to feed to L<DateTime::Format::DBI>.
 
-In this example, calls to an event's C<insert_time> accessor return a
-L<DateTime> object. This L<DateTime> object is later "deflated" when
+In this example, calls to an event's C<insert_time> or C<update_time> accessor
+return a L<DateTime> object. This L<DateTime> object is later "deflated" when
 used in the database layer.
 
 =cut
 
-sub inflate_column {
-  my ($self, $col, $attrs) = @_;
-  $self->throw_exception("No such column $col to inflate")
-    unless $self->has_column($col);
-  $self->throw_exception("inflate_column needs attr hashref")
+sub inflate_columns {
+  my $self = shift;
+  my $attrs = pop;
+  $self->throw_exception("inflate_columns needs attr hashref")
     unless ref $attrs eq 'HASH';
-  $self->column_info($col)->{_inflate_info} = $attrs;
-  $self->mk_group_accessors('inflated_column' => $col);
+  for (@_) {
+    $self->throw_exception("No such column $_ to inflate")
+      unless $self->has_column($_);
+    $self->column_info($_)->{_inflate_info} = $attrs;
+    $self->mk_group_accessors('inflated_column' => $_);
+  }
   return 1;
 }
 
+=head2 inflate_column
+
+A convenience alias to inflate_columns.
+
+=cut
+
+*inflate_column = \&inflate_columns;
+
 sub _inflated_column {
   my ($self, $col, $value) = @_;
   return $value unless defined $value; # NULL is NULL is NULL
_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/

Reply via email to