[Bast-commits] r5259 - in DBIx-Class/0.08/trunk: lib/DBIx/Class t

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Fri Dec 19 20:48:35 GMT 2008


Author: jnapiorkowski
Date: 2008-12-19 20:48:35 +0000 (Fri, 19 Dec 2008)
New Revision: 5259

Modified:
   DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
   DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm
   DBIx-Class/0.08/trunk/t/101populate_rs.t
Log:
altered schema->populate so that it is a very thin wrapper on top of resultset->populate and changed resultset_populate so that is accepted both the arrayref of hashes and the arrayref of arrayref style of args.  Documented this, updated the tests a bit to make sure it is all good.

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm	2008-12-19 19:52:44 UTC (rev 5258)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm	2008-12-19 20:48:35 UTC (rev 5259)
@@ -1362,8 +1362,9 @@
 
 =back
 
-Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
-submitting to a $resultset->create(...) method.
+Accepts either an arrayref of hashrefs or alternatively an arrayref of arrayrefs.
+For the arrayref of hashrefs style each hashref should be a structure suitable
+forsubmitting to a $resultset->create(...) method.
 
 In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
 to insert the data, as this is a faster method.  
@@ -1403,7 +1404,18 @@
   
   print $ArtistOne->name; ## response is 'Artist One'
   print $ArtistThree->cds->count ## reponse is '2'
-  
+
+For the arrayref of arrayrefs style,  the first element should be a list of the
+fieldsnames to which the remaining elements are rows being inserted.  For
+example:
+
+  $Arstist_rs->populate([
+    [qw/artistid name/],
+    [100, 'A Formally Unknown Singer'],
+    [101, 'A singer that jumped the shark two albums ago'],
+    [102, 'An actually cool singer.'],
+  ]);
+
 Please note an important effect on your data when choosing between void and
 wantarray context. Since void context goes straight to C<insert_bulk> in 
 L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
@@ -1415,7 +1427,10 @@
 =cut
 
 sub populate {
-  my ($self, $data) = @_;
+  my $self = shift @_;
+  my $data = ref $_[0][0] eq 'HASH'
+    ? $_[0] : ref $_[0][0] eq 'ARRAY' ? $self->_normalize_populate_args($_[0]) :
+    $self->throw_exception('Populate expects an arrayref of hashes or arrayref of arrayrefs');
   
   if(defined wantarray) {
     my @created;
@@ -1489,6 +1504,28 @@
   }
 }
 
+=head2 _normalize_populate_args ($args)
+
+Private method used by L</populate> to normalize it's incoming arguments.  Factored
+out in case you want to subclass and accept new argument structures to the
+L</populate> method.
+
+=cut
+
+sub _normalize_populate_args {
+  my ($self, $data) = @_;
+  my @names = @{shift(@$data)};
+  my @results_to_create;
+  foreach my $datum (@$data) {
+    my %result_to_create;
+    foreach my $index (0..$#names) {
+      $result_to_create{$names[$index]} = $$datum[$index];
+    }
+    push @results_to_create, \%result_to_create;    
+  }
+  return \@results_to_create;
+}
+
 =head2 pager
 
 =over 4

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm	2008-12-19 19:52:44 UTC (rev 5258)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm	2008-12-19 20:48:35 UTC (rev 5259)
@@ -707,26 +707,15 @@
 
 sub populate {
   my ($self, $name, $data) = @_;
-  my $rs = $self->resultset($name);
-  my @names = @{shift(@$data)};
-  if(defined wantarray) {
-    my @created;
-    foreach my $item (@$data) {
-      my %create;
-      @create{@names} = @$item;
-      push(@created, $rs->create(\%create));
+  if(my $rs = $self->resultset($name)) {
+    if(defined wantarray) {
+        return $rs->populate($data);
+    } else {
+        $rs->populate($data);
     }
-    return @created;
+  } else {
+      $self->throw_exception("$name is not a resultset"); 
   }
-  my @results_to_create;
-  foreach my $datum (@$data) {
-    my %result_to_create;
-    foreach my $index (0..$#names) {
-      $result_to_create{$names[$index]} = $$datum[$index];
-    }
-    push @results_to_create, \%result_to_create;
-  }
-  $rs->populate(\@results_to_create);
 }
 
 =head2 connection

Modified: DBIx-Class/0.08/trunk/t/101populate_rs.t
===================================================================
--- DBIx-Class/0.08/trunk/t/101populate_rs.t	2008-12-19 19:52:44 UTC (rev 5258)
+++ DBIx-Class/0.08/trunk/t/101populate_rs.t	2008-12-19 20:48:35 UTC (rev 5259)
@@ -15,7 +15,7 @@
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 134;
+plan tests => 142;
 
 
 ## ----------------------------------------------------------------------------
@@ -601,4 +601,30 @@
 		ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
 	}
 
+}
+
+ARRAYREF_OF_ARRAYREF_STYLE: {
+  $art_rs->populate([
+    [qw/artistid name/],
+    [1000, 'A Formally Unknown Singer'],
+    [1001, 'A singer that jumped the shark two albums ago'],
+    [1002, 'An actually cool singer.'],
+  ]);
+  
+  ok my $unknown = $art_rs->find(1000), "got Unknown";
+  ok my $jumped = $art_rs->find(1001), "got Jumped";
+  ok my $cool = $art_rs->find(1002), "got Cool";
+  
+  is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
+  is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
+  is $cool->name, 'An actually cool singer.', 'Correct Name';
+  
+  my ($cooler, $lamer) = $art_rs->populate([
+    [qw/artistid name/],
+    [1003, 'Cooler'],
+    [1004, 'Lamer'],	
+  ]);
+  
+  is $cooler->name, 'Cooler', 'Correct Name';
+  is $lamer->name, 'Lamer', 'Correct Name';  
 }
\ No newline at end of file




More information about the Bast-commits mailing list