File Coverage

blib/lib/DBIx/Class/Helper/ResultClass/Tee.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 20 21 95.2


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::ResultClass::Tee;
2             $DBIx::Class::Helper::ResultClass::Tee::VERSION = '2.036000';
3             # ABSTRACT: Inflate to multiple result classes at the same time
4              
5 1     1   1195 use utf8;
  1         12  
  1         5  
6              
7 1     1   28 use Moo;
  1         1  
  1         7  
8 1     1   299 use Module::Runtime 'use_module';
  1         2  
  1         4  
9 1     1   47 use Scalar::Util 'blessed';
  1         2  
  1         168  
10              
11             has inner_classes => (
12             is => 'ro',
13             required => 1,
14             coerce => sub {
15             [ map {
16             s/^::/DBIx::Class::ResultClass::/;
17             s/::HRI$/::HashRefInflator/;
18             $_
19             } @{$_[0]} ]
20             },
21             );
22              
23             sub inflate_result {
24 1     1 0 6444 my ($self, @rest) = @_;
25              
26 1         2 [ map scalar use_module($_)->inflate_result(@rest), @{$self->inner_classes} ]
  1         10  
27             }
28              
29             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) 2020 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