Added some simple docs to HashRefInflator and updated cookbook to match
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultClass / HashRefInflator.pm
1 package DBIx::Class::ResultClass::HashRefInflator;
2
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
26 resultser (and any chained off of it); or
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
32 recomended
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
55
56 sub mk_hash {
57     my ($me, $rest) = @_;
58
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
63     # to avoid emtpy has_many rels contain one empty hashref
64     return if (not keys %$me);
65
66     return { %$me,
67         map {
68           ( $_ =>
69              ref($rest->{$_}[0]) eq 'ARRAY' ? [ map { mk_hash(@$_) } @{$rest->{$_}} ]
70                                             : mk_hash( @{$rest->{$_}} )
71           )
72         } keys %$rest
73     };
74 }
75
76 1;