From: Luke Saunders Date: Wed, 19 May 2010 10:20:50 +0000 (+0100) Subject: deprecated add_row_info and added replacement method build_metadata X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f16908636c014b7711a8df1d8cddce78db1e4b8b;p=dbsrgits%2FDBIx-Class-ResultSet-WithMetaData.git deprecated add_row_info and added replacement method build_metadata --- diff --git a/lib/DBIx/Class/ResultSet/WithMetaData.pm b/lib/DBIx/Class/ResultSet/WithMetaData.pm index 131fcdd..b1efe90 100644 --- a/lib/DBIx/Class/ResultSet/WithMetaData.pm +++ b/lib/DBIx/Class/ResultSet/WithMetaData.pm @@ -23,6 +23,11 @@ has 'id_cols' => ( isa => 'ArrayRef', ); +has '_modifiers' => ( + is => 'rw', + isa => 'ArrayRef', +); + =head1 VERSION Version 0.999003 @@ -44,10 +49,12 @@ DBIx::Class::ResultSet::WithMetaData extends 'DBIx::Class::ResultSet::WithMetaData; method with_substr () { - foreach my $row ($self->all) { - my $substr = substr($row->name, 0, 3); - $self->add_row_info(row => $row, info => { substr => $substr }); - } + $self->build_metadata( modifier => sub () { + my $row = shift; + my $substr = substr($row->{name}, 0, 3); + $row->{substr} = $substr; + return $row; + }); return $self; } @@ -88,7 +95,7 @@ sub new { my $self = shift; my $new = $self->next::method(@_); - foreach my $key (qw/_row_info was_row id_cols/) { + foreach my $key (qw/_row_info was_row id_cols _modifiers/) { alias $new->{$key} = $new->{attrs}{$key}; } @@ -96,6 +103,10 @@ sub new { $new->_row_info({}); } + unless ($new->_modifiers) { + $new->_modifiers([]); + } + unless ($new->id_cols && scalar(@{$new->id_cols})) { $new->id_cols([sort $new->result_source->primary_columns]); } @@ -126,16 +137,55 @@ method display () { $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); my @rows; foreach my $row ($rs->all) { + # THIS BLOCK IS DEPRECATED if (my $info = $self->row_info_for(id => $self->_mk_id(row => $row))) { $row = { %{$row}, %{$info} }; } + + foreach my $modifier (@{$rs->_modifiers}) { + my $new_row = $modifier->($row); + if (ref $new_row ne 'HASH') { + die 'modifier subref (added via build_metadata did not return hashref)'; + } + $row = $new_row; + } push(@rows, $row); } return ($self->was_row) ? $rows[0] : \@rows; } -=head2 add_row_info +=head2 build_metadata + +=over 4 + +=item Arguments: modifier => subref($row_hash) + +=item Return Value: ResultSet + +=back + + $self->build_metadata( modifier => sub ($row) { + $row->{dates} = [qw/mon weds fri/]; + return $row; + }); + +This method allows you to alter the hash to add additional metadata to it +at L time. + +=cut + +method build_metadata (%opts) { + my ($modifier) = map { $opts{$_} } qw/modifier/; + unless ($modifier && (ref $modifier eq 'CODE')) { + die 'build_metadata called without modifier param'; + } + + push( @{$self->_modifiers}, $modifier ); +} + + +=head2 add_row_info (DEPRECATED) =over 4 @@ -147,13 +197,15 @@ method display () { $rs = $rs->add_row_info(row => $row, info => { dates => [qw/mon weds fri/] } ); -This method allows you to attach a HashRef of metadata to a row which will be merged -with that row when the ResultSet is flattened to a datastructure with L. +DEPRECATED - this method is quite slow as it requires that you iterate through +the resultset each time you want to add metadata. Replaced by L. =cut method add_row_info (%opts) { my ($row, $id, $info) = map { $opts{$_} } qw/row id info/; + + warn 'DEPRECATED - add_row_info is deprecated in favour of build_metadata'; if ($row) { $id = $self->_mk_id(row => { $row->get_columns }); } @@ -169,11 +221,13 @@ method add_row_info (%opts) { $self->_row_info->{$id} = $info; } +# DEPRECATED method row_info_for (%opts) { my $id = $opts{id}; return $self->_row_info->{$id}; } +# DEPRECATED method _mk_id (%opts) { my $row = $opts{row}; return join('-', map { $row->{$_} } @{$self->id_cols}); diff --git a/t/custom_methods.t b/t/custom_methods.t index e466143..9db7886 100644 --- a/t/custom_methods.t +++ b/t/custom_methods.t @@ -47,6 +47,27 @@ ok(my $schema = DBICTest->init_schema(), 'got schema'); ], 'display with substring okay'); } +# { +# my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr_old->display(); +# is_deeply($artists, [ +# { +# 'artistid' => '1', +# 'name' => 'Caterwauler McCrae', +# 'substr' => 'Cat' +# }, +# { +# 'artistid' => '2', +# 'name' => 'Random Boy Band', +# 'substr' => 'Ran' +# }, +# { +# 'artistid' => '3', +# 'name' => 'We Are Goth', +# 'substr' => 'We ' +# } +# ], 'display with substring okay'); +# } + { my $artists = $schema->resultset('Artist')->search({}, { order_by => 'artistid' })->with_substr->search({}, { prefetch => 'cds', rows => 1 })->display(); is_deeply($artists, [ diff --git a/t/lib/DBICTest/Schema/ResultSet/Artist.pm b/t/lib/DBICTest/Schema/ResultSet/Artist.pm index b9d6a61..94f4fe1 100644 --- a/t/lib/DBICTest/Schema/ResultSet/Artist.pm +++ b/t/lib/DBICTest/Schema/ResultSet/Artist.pm @@ -5,11 +5,23 @@ use Method::Signatures::Simple; extends 'DBICTest::Schema::ResultSet'; method with_substr () { - foreach my $row ($self->all) { - my $substr = substr($row->name, 0, 3); - $self->add_row_info(row => $row, info => { substr => $substr }); - } - return $self; + $self->build_metadata( + modifier => sub () { + my $row = shift; + my $substr = substr($row->{name}, 0, 3); + $row->{substr} = $substr; + return $row; + } + ); + return $self; +} + +method with_substr_old () { + foreach my $row ($self->all) { + my $substr = substr($row->name, 0, 3); + $self->add_row_info(row => $row, info => { substr => $substr }); + } + return $self; } 1;