Commit | Line | Data |
b0930c1e |
1 | package DBIx::Class::ResultClass::HashRefInflator; |
2 | |
137c657c |
3 | =head1 NAME |
4 | |
5 | DBIx::Class::ResultClass::HashRefInflator |
6 | |
7 | =head1 SYNOPSIS |
8 | |
9 | my $rs = $schema->resultset('CD'); |
10 | |
11 | $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
12 | |
13 | =head1 DESCRIPTION |
14 | |
15 | DBIx::Class is not built for speed: it's built for convenience and |
16 | ease of use. But sometimes you just need to get the data, and skip the |
17 | fancy objects. That is what this class provides. |
18 | |
19 | There are two ways of using this class. |
20 | |
21 | =over |
22 | |
23 | =item * |
24 | |
25 | Specify C<< $rs->result_class >> on a specific resultset to affect only that |
57dd9393 |
26 | resultset (and any chained off of it); or |
137c657c |
27 | |
28 | =item * |
29 | |
30 | Specify C<< __PACKAGE__->result_class >> on your source object to force all |
31 | uses of that result source to be inflated to hash-refs - this approach is not |
57dd9393 |
32 | recommended. |
137c657c |
33 | |
34 | =back |
35 | |
36 | =head1 METHODS |
37 | |
38 | =head2 inflate_result |
39 | |
40 | Inflates the result and prefetched data into a hash-ref using L<mk_hash>. |
41 | |
42 | =cut |
43 | |
44 | sub inflate_result { |
45 | my ($self, $source, $me, $prefetch) = @_; |
46 | |
47 | return mk_hash($me, $prefetch); |
48 | } |
49 | |
50 | =head2 mk_hash |
51 | |
52 | This does all the work of inflating the (pre)fetched data. |
53 | |
54 | =cut |
b0930c1e |
55 | |
56 | sub mk_hash { |
57 | my ($me, $rest) = @_; |
58 | |
137c657c |
59 | # $me is the hashref of cols/data from the immediate resultsource |
60 | # $rest is a deep hashref of all the data from the prefetched |
61 | # related sources. |
62 | |
b0930c1e |
63 | # to avoid emtpy has_many rels contain one empty hashref |
64 | return if (not keys %$me); |
65 | |
b25e9fa0 |
66 | my $def; |
67 | |
68 | foreach (values %$me) { |
69 | if (defined $_) { |
70 | $def = 1; |
71 | last; |
72 | } |
73 | } |
74 | return unless $def; |
75 | |
b0930c1e |
76 | return { %$me, |
137c657c |
77 | map { |
78 | ( $_ => |
79 | ref($rest->{$_}[0]) eq 'ARRAY' ? [ map { mk_hash(@$_) } @{$rest->{$_}} ] |
80 | : mk_hash( @{$rest->{$_}} ) |
81 | ) |
82 | } keys %$rest |
b0930c1e |
83 | }; |
84 | } |
85 | |
b0930c1e |
86 | 1; |