| 1 | package DBIx::Class::ResultClass::CallbackInflator; | ||
| 2 | |||
| 3 | 1 | our $VERSION = '0.002'; | |
| 4 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | has 'callback' => ( | ||
| 7 | is=>'ro', | ||
| 8 | 1 | ▶ | isa=>sub { ref($_[0]) eq 'CODE' }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 9 | 1 | required=>1); | |
| 10 | |||
| 11 | has 'args' => ( | ||
| 12 | is=>'ro', | ||
| 13 | 1 | ▶ | isa=>sub { ref($_[0]) eq 'ARRAY' }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | default=>sub { [] }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 15 | 1 | required=>1); | |
| 16 | |||
| 17 | sub inflate_result { | ||
| 18 | 5 | ▶ | my ($self, $result_source, $columndata, $prefetcheddata) = @_; |
|
Subroutine
inflate_result: called CC=1 SCAR=0.0
Pod
documented
| |||
| 19 | 5 | my @args = ($self->callback, $result_source, $columndata, $prefetcheddata, @{$self->args}); | |
| 20 | 5 | return $self->callback->(@args); | |
| 21 | } | ||
| 22 | |||
| 23 | 1 | 1; | |
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | DBIx::Class::ResultClass::CallbackInflator - Easier way to make a quick, custom inflator | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use DBIx::Class::ResultClass::CallbackInflator; | ||
| 32 | |||
| 33 | my $result_instance = DBIx::Class::ResultClass::CallbackInflator | ||
| 34 | ->new(callback=>$callback, args=>\@args); | ||
| 35 | |||
| 36 | my $person_resultset = $schema | ||
| 37 | ->resultset('Person') | ||
| 38 | ->resultclass($result_instance); | ||
| 39 | |||
| 40 | # Alternatively | ||
| 41 | my $person_resultset = $schema | ||
| 42 | ->resultset('Person') | ||
| 43 | ->search(+{}, +{result_class=>$result_instance}); | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | You might wish to review L<DBIx::Class::ResultSet::CallbackInflator> which is probably | ||
| 48 | the most easy way to actually make use of this in a resultset. | ||
| 49 | |||
| 50 | =head1 ATTRIBUTES | ||
| 51 | |||
| 52 | This class defines the following attributes. | ||
| 53 | |||
| 54 | =head2 callback | ||
| 55 | |||
| 56 | This is the callback with receives the row level data for the resultset as it gets it | ||
| 57 | from the database and is responsible for returning a reference to a version of that | ||
| 58 | data suitable for a user. | ||
| 59 | |||
| 60 | =head2 args | ||
| 61 | |||
| 62 | Arguments passed to the callback. | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | This component defines the following methods. | ||
| 67 | |||
| 68 | =head2 inflate_result | ||
| 69 | |||
| 70 | Provides the method needed to call table data on your custom callback (with any passed | ||
| 71 | arguments). | ||
| 72 | |||
| 73 | =head1 AUTHOR | ||
| 74 | |||
| 75 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||
| 76 | |||
| 77 | With thanks to mst for the original code suggestion! | ||
| 78 | |||
| 79 | =head1 SEE ALSO | ||
| 80 | |||
| 81 | L<DBIx::Class>, L<DBIx::Class::ResultClass::HashRefInflator>. | ||
| 82 | |||
| 83 | =head1 COPYRIGHT & LICENSE | ||
| 84 | |||
| 85 | Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org> | ||
| 86 | |||
| 87 | This library is free software; you can redistribute it and/or modify it under | ||
| 88 | the same terms as Perl itself. | ||
| 89 | |||
| 90 | =cut | ||