X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet%2FWithMetaData.pm;h=7e540c3be1fd31c14d4ce1379cd058b21c882b5f;hb=9432d8bfcbdce5343443278c6722241196fca9d5;hp=01a98538d053ea69f779d294ea44702da517c898;hpb=4cac0ed673bc17698d8e736fa4d4fbb70666c7fe;p=dbsrgits%2FDBIx-Class-ResultSet-WithMetaData.git 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