| 1 | package DBIx::Class::Helper::ResultClass::Tee; | ||
| 2 | 1 | $DBIx::Class::Helper::ResultClass::Tee::VERSION = '2.032002'; | |
| 3 | # ABSTRACT: Inflate to multiple result classes at the same time | ||
| 4 | |||
| 5 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Module::Runtime 'use_module'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Scalar::Util 'blessed'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | has inner_classes => ( | ||
| 12 | is => 'ro', | ||
| 13 | required => 1, | ||
| 14 | coerce => sub { | ||
| 15 | [ map { | ||
| 16 | 2 | s/^::/DBIx::Class::ResultClass::/; | |
| 17 | 2 | s/::HRI$/::HashRefInflator/; | |
| 18 | 2 | $_ | |
| 19 | 1 | ▶ | } @{$_[0]} ] |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 20 | }, | ||
| 21 | 1 | ); | |
| 22 | |||
| 23 | sub inflate_result { | ||
| 24 | 1 | ▶ | my ($self, @rest) = @_; |
|
Subroutine
inflate_result: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 25 | |||
| 26 | 1 | [ map scalar use_module($_)->inflate_result(@rest), @{$self->inner_classes} ] | |
| 27 | } | ||
| 28 | |||
| 29 | 1 | 1; | |
| 30 | |||
| 31 | __END__ | ||
| 32 | |||
| 33 | =pod | ||
| 34 | |||
| 35 | =head1 NAME | ||
| 36 | |||
| 37 | DBIx::Class::Helper::ResultClass::Tee - Inflate to multiple result classes at the same time | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | my ($hashref, $obj) = $rs->search(undef, { | ||
| 42 | result_class => DBIx::Class::Helper::ResultClass::Tee->new( | ||
| 43 | inner_classes => [ '::HRI', 'MyApp::Schema::Result::User'], | ||
| 44 | ), | ||
| 45 | })->first->@*; | ||
| 46 | |||
| 47 | (If you've never seen C<< ->@* >> before, check out | ||
| 48 | L<perlref/Postfix-Dereference-Syntax>, added in Perl v5.20!) | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This result class has one obvious use case: when you have prefetched data and | ||
| 53 | L<DBIx::Class::ResultClass::HashRefInflator> is the simplest way to access all | ||
| 54 | the data, but you still want to use some of the methods on your existing result | ||
| 55 | class. | ||
| 56 | |||
| 57 | =encoding UTF-8 | ||
| 58 | |||
| 59 | The other important I<raison d'être> of this module is that it is an example of | ||
| 60 | how to make a "parameterized" result class. It's almost a secret that | ||
| 61 | L<DBIx::Class> supports using objects to inflate results. This is an incredibly | ||
| 62 | powerful feature that can be used to make consistent interfaces to do all kinds | ||
| 63 | of things. | ||
| 64 | |||
| 65 | Once when I was at Micro Technology Services, Inc. I used it to efficiently do a | ||
| 66 | "reverse synthetic, LIKE-ish join". The "relationship" was basically | ||
| 67 | C<< foreign.name =~ self.name >>, which cannot actually be done if you want to | ||
| 68 | go from within the database, but if you are able to load the entire foreign | ||
| 69 | table into memory this can be done on-demand, and cached within the result class | ||
| 70 | for (in our case) the duration of a request. | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> | ||
| 75 | |||
| 76 | =head1 COPYRIGHT AND LICENSE | ||
| 77 | |||
| 78 | This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt. | ||
| 79 | |||
| 80 | This is free software; you can redistribute it and/or modify it under | ||
| 81 | the same terms as the Perl 5 programming language system itself. | ||
| 82 | |||
| 83 | =cut | ||