isa => 'ArrayRef',
);
+has '_modifiers' => (
+ is => 'rw',
+ isa => 'ArrayRef',
+);
+
=head1 VERSION
Version 0.999003
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;
}
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};
}
$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]);
}
$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</display> 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
$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</display>.
+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</build_metadata>.
=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 });
}
$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});
], '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, [
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;