| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package DBIx::Class::ResultSet::CallbackInflator; | ||
| 5 | |||
| 6 | 1 | ▶ | use DBIx::Class::ResultClass::CallbackInflator; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | sub inflator { |
|
Subroutine
inflator: called CC=1 SCAR=0.0
Pod
documented
| |||
| 9 | 1 | my ($self, $callback, @args) = @_; | |
| 10 | 1 | my $result_instance = DBIx::Class::ResultClass::CallbackInflator | |
| 11 | ->new(callback=>$callback, args=>\@args); | ||
| 12 | 1 | return $self->search(+{}, +{result_class=>$result_instance}); | |
| 13 | } | ||
| 14 | |||
| 15 | 1 | 1; | |
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | DBIx::Class::ResultSet::CallbackInflator - Easy way to use DBIx::Class::ResultClass::CallbackInflator | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | Given a L<DBIx::Class::ResultSet> that consumes this component, such as the | ||
| 24 | following: | ||
| 25 | |||
| 26 | package MySchema::ResultSet::Peson; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | use parent 'DBIx::Class::ResultSet'; | ||
| 31 | |||
| 32 | __PACKAGE__->load_components('ResultSet::CallbackInflator'); | ||
| 33 | |||
| 34 | ## Additional custom resultset methods, if any | ||
| 35 | |||
| 36 | 1; | ||
| 37 | |||
| 38 | Then later when you have a resultset of that class: | ||
| 39 | |||
| 40 | my $rs = $schema->resultset('Person') | ||
| 41 | ->inflator(sub { | ||
| 42 | my ($cb, $source, $data, $optional_prefetch, @args) = @_; | ||
| 43 | return $data; | ||
| 44 | }, @args); | ||
| 45 | |||
| 46 | When the resultset in C<$rs> is unrolled the callback will be run for each row returned from | ||
| 47 | the query and you can customize the response (must be a reference). For example you might use | ||
| 48 | this to change or add to the existing C<$data> for a purpose that is meaningful to your | ||
| 49 | application. | ||
| 50 | |||
| 51 | B<NOTE> Currently we localize C<%_> in the callback coderef to be C<%$data> to make it | ||
| 52 | easier to do the most standard (I think) types of transformations. | ||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | Lets you decide how a resultset inflates any data in rows from the database table | ||
| 57 | it is pointing at (with any prefetch information included) by using a coderef | ||
| 58 | which will in in place of 'inflate_result'. | ||
| 59 | |||
| 60 | This is basically sugar for L<DBIx::Class::ResultClass::CallbackInflator> | ||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | This component defines the following methods. | ||
| 65 | |||
| 66 | =head2 inflator | ||
| 67 | |||
| 68 | Allows you to use a callback as a custom inflator class. Example: | ||
| 69 | |||
| 70 | $rs->inflator(sub { | ||
| 71 | my ($cb, $result_source, \%columndata, \%prefetcheddata, @args) = @_; | ||
| 72 | return ... | ||
| 73 | })->all; | ||
| 74 | |||
| 75 | Should return a reference to the representation of the row that you are seeking. | ||
| 76 | |||
| 77 | B<NOTE>: The last argument C<\%prefetcheddata> is optional. If there isn't any the | ||
| 78 | location in C<@_> will be undef so that any @args passed will be in expected position. | ||
| 79 | |||
| 80 | B<NOTE>: The call to ->inflator returns the original resultset to allow for easy | ||
| 81 | chaining. | ||
| 82 | |||
| 83 | =head1 AUTHOR | ||
| 84 | |||
| 85 | See L<DBIx::Class::ResultClass::CallbackInflator>. | ||
| 86 | |||
| 87 | =head1 COPYRIGHT & LICENSE | ||
| 88 | |||
| 89 | See L<DBIx::Class::ResultClass::CallbackInflator>. | ||
| 90 | |||
| 91 | =cut | ||