1 package DBIx::Class::ResultSet::WithMetaData;
8 use Method::Signatures::Simple;
9 extends 'DBIx::Class::ResultSet';
32 our $VERSION = '0.999001';
36 DBIx::Class::ResultSet::WithMetaData
40 package MyApp::Schema::ResultSet::ObjectType;
43 use MooseX::Method::Signatures;
44 extends 'DBIx::Class::ResultSet::WithMetaData;
46 method with_substr () {
47 foreach my $row ($self->all) {
48 my $substr = substr($row->name, 0, 3);
49 $self->add_row_info(row => $row, info => { substr => $substr });
59 my $object_type_arrayref = $object_type_rs->limit(count => 3)->with_substr->display();
63 # 'name' => 'Caterwauler McCrae',
68 # 'name' => 'Random Boy Band',
73 # 'name' => 'We Are Goth',
79 Attach metadata to rows by chaining ResultSet methods together. When the ResultSet is
80 flattened to an ArrayRef the attached metadata is merged with the row hashes to give
81 a combined 'hash-plus-other-stuff' representation.
90 my $new = $self->next::method(@_);
91 foreach my $key (qw/_row_info was_row id_cols/) {
92 alias $new->{$key} = $new->{attrs}{$key};
95 unless ($new->_row_info) {
99 unless ($new->id_cols && scalar(@{$new->id_cols})) {
100 $new->id_cols([sort $new->result_source->primary_columns]);
110 =item Arguments: none
112 =item Return Value: ArrayRef
116 $arrayref_of_row_hashrefs = $rs->display();
118 This method uses L<DBIx::Class::ResultClass::HashRefInflator> to convert all
119 rows in the ResultSet to HashRefs. These are then merged with any metadata
120 that had been attached to the rows using L</add_row_info>.
125 my $rs = $self->search({});
126 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
128 foreach my $row ($rs->all) {
129 if (my $info = $self->row_info_for(id => $self->_mk_id(row => $row))) {
130 $row = { %{$row}, %{$info} };
135 return ($self->was_row) ? $rows[0] : \@rows;
142 =item Arguments: row => DBIx::Class::Row object, info => HashRef to attach to the row
144 =item Return Value: ResultSet
148 $rs = $rs->add_row_info(row => $row, info => { dates => [qw/mon weds fri/] } );
150 This method allows you to attach a HashRef of metadata to a row which will be merged
151 with that row when the ResultSet is flattened to a datastructure with L</display>.
155 method add_row_info (%opts) {
156 my ($row, $id, $info) = map { $opts{$_} } qw/row id info/;
158 $id = $self->_mk_id(row => { $row->get_columns });
161 unless ($row || $self->find($id)) {
162 die 'invalid id passed to add_row_info';
165 if (my $existing = $self->_row_info->{$id}) {
166 $info = { %{$existing}, %{$info} };
169 $self->_row_info->{$id} = $info;
172 method row_info_for (%opts) {
174 return $self->_row_info->{$id};
177 =head2 order_by (EXPERIMENTAL)
181 =item Arguments: col => $column_name
183 =item Return Value: ResultSet
187 $ordered_rs = $rs->order_by(col => 'name');
189 Convenience method. Essentually a shortcut for $rs->search({}, { order_by => $col }).
193 method order_by (%opts) {
194 my $col = $opts{col};
195 $col = "me.$col" unless ($col =~ m/\./);
196 return $self->search({}, { order_by => $col });
199 =head2 limit (EXPERIMENTAL)
203 =item Arguments: count => Int
205 =item Return Value: ResultSet
209 $limitted_rs = $rs->limit(count => 3);
211 Convenience method. Essentually a shortcut for $rs->search({}, { rows => $count }).
215 method limit (%opts) {
216 my $count = $opts{count};
217 return $self->search({}, { rows => $count });
220 method _mk_id (%opts) {
221 my $row = $opts{row};
222 return join('-', map { $row->{$_} } @{$self->id_cols});
227 Luke Saunders <luke.saunders@gmail.com>
231 As usual, thanks to Matt S Trout for the sanity check.
235 This library is free software under the same license as perl itself