isa => 'ArrayRef',
);
-has '_modifiers' => (
+has '_hash_modifiers' => (
+ is => 'rw',
+ isa => 'ArrayRef',
+);
+
+has '_key_modifiers' => (
is => 'rw',
isa => 'ArrayRef',
);
=head1 VERSION
-Version 0.999003
+Version 1.000000
=cut
-our $VERSION = '0.999003';
+our $VERSION = '1.000000';
=head1 NAME
extends 'DBIx::Class::ResultSet::WithMetaData;
method with_substr () {
- $self->build_metadata( modifier => sub () {
- my $row = shift;
- my $substr = substr($row->{name}, 0, 3);
- $row->{substr} = $substr;
- return $row;
- });
- return $self;
+ return $self->_with_meta_key(
+ substr => sub {
+ return substr(shift->{name}, 0, 3);
+ }
+ );
}
...
my $self = shift;
my $new = $self->next::method(@_);
- foreach my $key (qw/_row_info was_row id_cols _modifiers/) {
+ foreach my $key (qw/_row_info was_row id_cols _key_modifiers _hash_modifiers/) {
alias $new->{$key} = $new->{attrs}{$key};
}
$new->_row_info({});
}
- unless ($new->_modifiers) {
- $new->_modifiers([]);
+ unless ($new->_key_modifiers) {
+ $new->_key_modifiers([]);
+ }
+ unless ($new->_hash_modifiers) {
+ $new->_hash_modifiers([]);
}
unless ($new->id_cols && scalar(@{$new->id_cols})) {
$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)';
+ foreach my $modifier (@{$rs->_hash_modifiers}) {
+ my $row_hash = $modifier->($row);
+ 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};
+
+ if (my $val = $modifier->($row)) {
+ $row->{$key} = $val;
}
- $row = $new_row;
}
push(@rows, $row);
}
return ($self->was_row) ? $rows[0] : \@rows;
}
-=head2 build_metadata
+=head2 _with_meta_key
=over 4
-=item Arguments: modifier => subref($row_hash)
+=item Arguments: key_name => subref($row_hash)
=item Return Value: ResultSet
=back
- $self->build_metadata( modifier => sub ($row) {
- $row->{dates} = [qw/mon weds fri/];
- return $row;
+ $self->_with_meta_key( substr => sub ($row) {
+ return substr(shift->{name}, 0, 3);
});
-This method allows you to alter the hash to add additional metadata to it
-at L</display> time.
+This method allows you populate a certain key for each row hash at L</display> time.
=cut
-method build_metadata (%opts) {
- my ($modifier) = map { $opts{$_} } qw/modifier/;
+method _with_meta_key ($key, $modifier) {
+ my $rs = $self->search({});
+ unless ($key) {
+ die 'build_metadata called without key';
+ }
+
unless ($modifier && (ref $modifier eq 'CODE')) {
die 'build_metadata called without modifier param';
}
- push( @{$self->_modifiers}, $modifier );
+ push( @{$rs->_key_modifiers}, { key => $key, modifier => $modifier });
+ return $rs;
}
+=head2 _with_meta_hash
+
+=over 4
+
+=item Arguments: subref($row_hash)
+
+=item Return Value: ResultSet
+
+=back
+
+ $self->_with_meta_hash( sub ($row) {
+ my $row = shift;
+ my $return_hash = { substr => substr($row->{name}, 0, 3), substr2 => substr($row->{name}, 0, 4) };
+ return $return_hash;
+ });
+
+Use this method when you want to populate multiple keys of the hash at the same time. If you just want to
+populate one key, use L</_with_meta_key>.
+
+=cut
+
+method _with_meta_hash ($modifier) {
+ my $rs = $self->search({});
+ unless ($modifier && (ref $modifier eq 'CODE')) {
+ die 'build_metadata called without modifier param';
+ }
+
+ push( @{$rs->_hash_modifiers}, $modifier );
+ return $rs;
+}
=head2 add_row_info (DEPRECATED)