From: Luke Saunders Date: Mon, 16 Aug 2010 11:01:45 +0000 (+0200) Subject: object key modifier works okay X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9432d8bfcbdce5343443278c6722241196fca9d5;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 01a9853..7e540c3 100644 --- a/lib/DBIx/Class/ResultSet/WithMetaData.pm +++ b/lib/DBIx/Class/ResultSet/WithMetaData.pm @@ -33,6 +33,16 @@ has '_key_modifiers' => ( isa => 'ArrayRef', ); +has '_object_hash_modifiers' => ( + is => 'rw', + isa => 'ArrayRef', +); + +has '_object_key_modifiers' => ( + is => 'rw', + isa => 'ArrayRef', +); + =head1 VERSION Version 1.000002 @@ -98,7 +108,7 @@ sub new { my $self = shift; my $new = $self->next::method(@_); - foreach my $key (qw/_row_info was_row id_cols _key_modifiers _hash_modifiers/) { + foreach my $key (qw/_row_info was_row id_cols _key_modifiers _hash_modifiers _object_key_modifiers _object_hash_modifiers/) { alias $new->{$key} = $new->{attrs}{$key}; } @@ -112,6 +122,12 @@ sub new { unless ($new->_hash_modifiers) { $new->_hash_modifiers([]); } + unless ($new->_object_key_modifiers) { + $new->_object_key_modifiers([]); + } + unless ($new->_object_hash_modifiers) { + $new->_object_hash_modifiers([]); + } unless ($new->id_cols && scalar(@{$new->id_cols})) { $new->id_cols([sort $new->result_source->primary_columns]); @@ -145,6 +161,7 @@ method display () { $rs->result_class('DBIx::Class::WithMetaData::Inflator'); my @rows; foreach my $row_rep ($rs->all) { + # the custom inflator inflates to a arrayref with two versions of the row in it - hash and obj my ($row, $row_obj) = @{$row_rep}; # THIS BLOCK IS DEPRECATED if (my $info = $self->row_info_for(id => $self->_mk_id(row => $row))) { @@ -169,6 +186,15 @@ method display () { $row->{$key} = $val; } } + + foreach my $params (@{$rs->_object_key_modifiers}) { + my $modifier = $params->{modifier}; + my $key = $params->{key}; + + if (my $val = $modifier->($row, $row_obj)) { + $row->{$key} = $val; + } + } push(@rows, $row); } @@ -207,6 +233,41 @@ method _with_meta_key ($key, $modifier) { return $rs; } +=head2 _with_object_meta_key + +=over 4 + +=item Arguments: key_name => subref($row_hash, $row_obj) + +=item Return Value: ResultSet + +=back + + $self->_with_object_meta_key( substr => sub { + my ($row_hash, $row_obj) = @_; + return substr($row_obj->row_method, 0, 3); + }); + +The same as L 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_key ($key, $modifier) { + my $rs = $self->search({}); + unless ($key) { + die '_with_object_meta_key called without key'; + } + + unless ($modifier && (ref $modifier eq 'CODE')) { + die '_with_object_meta_key called without modifier param'; + } + + push( @{$rs->_object_key_modifiers}, { key => $key, modifier => $modifier }); + return $rs; +} + =head2 _with_meta_hash =over 4 diff --git a/lib/DBIx/Class/WithMetaData/Inflator.pm b/lib/DBIx/Class/WithMetaData/Inflator.pm index 6f42dd0..c07b016 100644 --- a/lib/DBIx/Class/WithMetaData/Inflator.pm +++ b/lib/DBIx/Class/WithMetaData/Inflator.pm @@ -13,8 +13,6 @@ around inflate_result => sub { my ($source, @rest) = @_; my $row = $source->result_class->inflate_result(@_); - warn $row; - warn $self; return [$hash, $row]; }; diff --git a/t/lib/DBICTest/Schema/ResultSet/Artist.pm b/t/lib/DBICTest/Schema/ResultSet/Artist.pm index 20ac92d..4cc4a09 100644 --- a/t/lib/DBICTest/Schema/ResultSet/Artist.pm +++ b/t/lib/DBICTest/Schema/ResultSet/Artist.pm @@ -28,7 +28,7 @@ method with_substr_key () { } method with_substr_key_obj () { - $self->_with_meta_key( + $self->_with_object_meta_key( substr => sub { my ($hash, $obj) = @_; return substr($obj->name, 0, 3);