1 package DBIx::Class::ResultSet::WithMetaData;
8 use Method::Signatures::Simple;
9 extends 'DBIx::Class::ResultSet';
37 our $VERSION = '0.999003';
41 DBIx::Class::ResultSet::WithMetaData
45 package MyApp::Schema::ResultSet::ObjectType;
48 use MooseX::Method::Signatures;
49 extends 'DBIx::Class::ResultSet::WithMetaData;
51 method with_substr () {
52 $self->build_metadata( modifier => sub () {
54 my $substr = substr($row->{name}, 0, 3);
55 $row->{substr} = $substr;
66 my $object_type_arrayref = $object_type_rs->with_substr->display();
70 # 'name' => 'Caterwauler McCrae',
75 # 'name' => 'Random Boy Band',
80 # 'name' => 'We Are Goth',
86 Attach metadata to rows by chaining ResultSet methods together. When the ResultSet is
87 flattened to an ArrayRef the attached metadata is merged with the row hashes to give
88 a combined 'hash-plus-other-stuff' representation.
97 my $new = $self->next::method(@_);
98 foreach my $key (qw/_row_info was_row id_cols _modifiers/) {
99 alias $new->{$key} = $new->{attrs}{$key};
102 unless ($new->_row_info) {
106 unless ($new->_modifiers) {
107 $new->_modifiers([]);
110 unless ($new->id_cols && scalar(@{$new->id_cols})) {
111 $new->id_cols([sort $new->result_source->primary_columns]);
121 =item Arguments: none
123 =item Return Value: ArrayRef
127 $arrayref_of_row_hashrefs = $rs->display();
129 This method uses L<DBIx::Class::ResultClass::HashRefInflator> to convert all
130 rows in the ResultSet to HashRefs. These are then merged with any metadata
131 that had been attached to the rows using L</add_row_info>.
136 my $rs = $self->search({});
137 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
139 foreach my $row ($rs->all) {
140 # THIS BLOCK IS DEPRECATED
141 if (my $info = $self->row_info_for(id => $self->_mk_id(row => $row))) {
142 $row = { %{$row}, %{$info} };
145 foreach my $modifier (@{$rs->_modifiers}) {
146 my $new_row = $modifier->($row);
147 if (ref $new_row ne 'HASH') {
148 die 'modifier subref (added via build_metadata did not return hashref)';
155 return ($self->was_row) ? $rows[0] : \@rows;
158 =head2 build_metadata
162 =item Arguments: modifier => subref($row_hash)
164 =item Return Value: ResultSet
168 $self->build_metadata( modifier => sub ($row) {
169 $row->{dates} = [qw/mon weds fri/];
173 This method allows you to alter the hash to add additional metadata to it
178 method build_metadata (%opts) {
179 my ($modifier) = map { $opts{$_} } qw/modifier/;
180 unless ($modifier && (ref $modifier eq 'CODE')) {
181 die 'build_metadata called without modifier param';
184 push( @{$self->_modifiers}, $modifier );
188 =head2 add_row_info (DEPRECATED)
192 =item Arguments: row => DBIx::Class::Row object, info => HashRef to attach to the row
194 =item Return Value: ResultSet
198 $rs = $rs->add_row_info(row => $row, info => { dates => [qw/mon weds fri/] } );
200 DEPRECATED - this method is quite slow as it requires that you iterate through
201 the resultset each time you want to add metadata. Replaced by L</build_metadata>.
205 method add_row_info (%opts) {
206 my ($row, $id, $info) = map { $opts{$_} } qw/row id info/;
208 warn 'DEPRECATED - add_row_info is deprecated in favour of build_metadata';
210 $id = $self->_mk_id(row => { $row->get_columns });
213 unless ($row || $self->find($id)) {
214 die 'invalid id passed to add_row_info';
217 if (my $existing = $self->_row_info->{$id}) {
218 $info = { %{$existing}, %{$info} };
221 $self->_row_info->{$id} = $info;
225 method row_info_for (%opts) {
227 return $self->_row_info->{$id};
231 method _mk_id (%opts) {
232 my $row = $opts{row};
233 return join('-', map { $row->{$_} } @{$self->id_cols});
238 Luke Saunders <luke.saunders@gmail.com>
242 As usual, thanks to Matt S Trout for the sanity check.
246 This library is free software under the same license as perl itself