From: Luke Saunders <luke.saunders@gmail.com>
Date: Mon, 16 Aug 2010 11:13:34 +0000 (+0200)
Subject: object key modifier works okay
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=dbsrgits%2FDBIx-Class-ResultSet-WithMetaData.git

object key modifier works okay
---

diff --git a/lib/DBIx/Class/ResultSet/WithMetaData.pm b/lib/DBIx/Class/ResultSet/WithMetaData.pm
index 7e540c3..8b425be 100644
--- a/lib/DBIx/Class/ResultSet/WithMetaData.pm
+++ b/lib/DBIx/Class/ResultSet/WithMetaData.pm
@@ -178,6 +178,16 @@ method display () {
       $row->{$_} = $row_hash->{$_} for keys %{$row_hash};
     }
 
+    foreach my $modifier (@{$rs->_object_hash_modifiers}) {
+      my $row_hash = $modifier->($row, $row_obj);
+      if (ref $row_hash ne 'HASH') {
+        die 'modifier subref (added via build_metadata) did not return hashref';
+      }
+
+      # simple merge for now, potentially needs to be more complex
+      $row->{$_} = $row_hash->{$_} for keys %{$row_hash};
+    }
+
     foreach my $params (@{$rs->_key_modifiers}) {
       my $modifier = $params->{modifier};
       my $key = $params->{key};
@@ -299,6 +309,39 @@ method _with_meta_hash ($modifier) {
   return $rs;
 }
 
+=head2 _with_object_meta_hash
+
+=over 4
+
+=item Arguments: subref($row_hash, $row_object)
+
+=item Return Value: ResultSet
+
+=back
+
+ $self->_with_object_meta_hash( sub { 
+   my ($row_hash, $row_object) = @_;
+
+   my $return_hash = { substr => substr($row_object->name, 0, 3), substr2 => substr($row_hash->{name}, 0, 4) };
+   return $return_hash;
+ });
+
+Like L</_with_meta_hash> but the subref gets the row object
+as well as the row hash. This should only be used when you need to
+access row methods as it's slower to inflate objects.
+
+=cut
+
+method _with_object_meta_hash ($modifier) {
+  my $rs = $self->search({});
+  unless ($modifier && (ref $modifier eq 'CODE')) {
+    die 'build_metadata called without modifier param';
+  }
+
+  push( @{$rs->_object_hash_modifiers}, $modifier );
+  return $rs;
+}
+
 =head2 add_row_info (DEPRECATED)
 
 =over 4
diff --git a/t/custom_methods.t b/t/custom_methods.t
index d8b70dd..43a3ae4 100644
--- a/t/custom_methods.t
+++ b/t/custom_methods.t
@@ -51,6 +51,30 @@ ok(my $schema = DBICTest->init_schema(), 'got schema');
 }
 
 {
+	my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_multi_object->display();
+	is_deeply($artists, [
+		{
+			'artistid' => '1',
+			'name' => 'Caterwauler McCrae',
+			'substr' => 'Cat',
+			'substr2' => 'Cate'
+		},
+		{
+			'artistid' => '2',
+			'name' => 'Random Boy Band',
+			'substr' => 'Ran',
+			'substr2' => 'Rand'
+		},
+		{
+			'artistid' => '3',
+			'name' => 'We Are Goth',
+			'substr' => 'We ',
+			'substr2' => 'We A'
+		}
+	], 'display with substring using _with_object_meta_hash okay');
+}
+
+{
 	my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_key->display();
 	is_deeply($artists, [
 		{
@@ -89,7 +113,7 @@ ok(my $schema = DBICTest->init_schema(), 'got schema');
 			'name' => 'We Are Goth',
 			'substr' => 'We '
 		}
-	], 'display with substring using _with_meta_key with object okay');
+	], 'display with substring using _with_object_meta_key okay');
 }
 
 # {
diff --git a/t/lib/DBICTest/Schema/ResultSet/Artist.pm b/t/lib/DBICTest/Schema/ResultSet/Artist.pm
index 4cc4a09..02622fc 100644
--- a/t/lib/DBICTest/Schema/ResultSet/Artist.pm
+++ b/t/lib/DBICTest/Schema/ResultSet/Artist.pm
@@ -18,6 +18,22 @@ method with_substr_multi () {
   return $self;
 }
 
+method with_substr_multi_object () {
+  $self->_with_object_meta_hash( 
+    sub {
+      my ($row_hash, $row_object) = @_;
+      my $substr = substr($row_object->name, 0, 3);
+      my $substr2 = substr($row_object->name, 0, 4);
+      my $row = {
+        substr => $substr,
+        substr2 => $substr2,
+      };
+      return $row;
+    }
+  );
+  return $self;
+}
+
 method with_substr_key () {
   $self->_with_meta_key( 
     substr => sub {