From: Ash Berlin Date: Sat, 13 Jan 2007 01:52:30 +0000 (+0000) Subject: Added some simple docs to HashRefInflator and updated cookbook to match X-Git-Tag: v0.08010~150^2~108 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=137c657c19009ba5ea9c350ed7ee5e69a8a1968b;p=dbsrgits%2FDBIx-Class.git Added some simple docs to HashRefInflator and updated cookbook to match --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 248c0b6..6c45d9b 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -1100,42 +1100,18 @@ B test.pl DBIx::Class is not built for speed, it's built for convenience and ease of use, but sometimes you just need to get the data, and skip the -fancy objects. Luckily this is also fairly easy using -C: - - # Define a class which just returns the results as a hashref: - package My::HashRefInflator; - - ## $me is the hashref of cols/data from the immediate resultsource - ## $prefetch is a deep hashref of all the data from the prefetched - ## related sources. - - sub mk_hash { - my ($me, $rest) = @_; - - return { %$me, - map { ($_ => mk_hash(@{$rest->{$_}})) } keys %$rest - }; - } - - sub inflate_result { - my ($self, $source, $me, $prefetch) = @_; - return mk_hash($me, $prefetch); - } - - # Change the object inflation to a hashref for just this resultset: - $rs->result_class('My::HashRefInflator'); - - my $datahashref = $rs->next; - foreach my $col (keys %$datahashref) { - if(!ref($datahashref->{$col})) { - # It's a plain value - } - elsif(ref($datahashref->{$col} eq 'HASH')) { - # It's a related value in a hashref - } - } - +fancy objects. + +To do this simply use L. + + my $rs = $schema->resultset('CD'); + + $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); + + my $hash_ref = $rs->find(1); + +Wasn't that easy? + =head2 Want to know if find_or_create found or created a row? Just use C instead, then check C: diff --git a/lib/DBIx/Class/ResultClass/HashRefInflator.pm b/lib/DBIx/Class/ResultClass/HashRefInflator.pm index d7dd411..ca6741e 100644 --- a/lib/DBIx/Class/ResultClass/HashRefInflator.pm +++ b/lib/DBIx/Class/ResultClass/HashRefInflator.pm @@ -1,24 +1,76 @@ package DBIx::Class::ResultClass::HashRefInflator; -# $me is the hashref of cols/data from the immediate resultsource -# $rest is a deep hashref of all the data from the prefetched -# related sources. +=head1 NAME + +DBIx::Class::ResultClass::HashRefInflator + +=head1 SYNOPSIS + + my $rs = $schema->resultset('CD'); + + $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); + +=head1 DESCRIPTION + +DBIx::Class is not built for speed: it's built for convenience and +ease of use. But sometimes you just need to get the data, and skip the +fancy objects. That is what this class provides. + +There are two ways of using this class. + +=over + +=item * + +Specify C<< $rs->result_class >> on a specific resultset to affect only that +resultser (and any chained off of it); or + +=item * + +Specify C<< __PACKAGE__->result_class >> on your source object to force all +uses of that result source to be inflated to hash-refs - this approach is not +recomended + +=back + +=head1 METHODS + +=head2 inflate_result + +Inflates the result and prefetched data into a hash-ref using L. + +=cut + +sub inflate_result { + my ($self, $source, $me, $prefetch) = @_; + + return mk_hash($me, $prefetch); +} + +=head2 mk_hash + +This does all the work of inflating the (pre)fetched data. + +=cut sub mk_hash { my ($me, $rest) = @_; + # $me is the hashref of cols/data from the immediate resultsource + # $rest is a deep hashref of all the data from the prefetched + # related sources. + # to avoid emtpy has_many rels contain one empty hashref return if (not keys %$me); return { %$me, - map { ($_ => ref($rest->{$_}[0]) eq 'ARRAY' ? [ map { mk_hash(@$_) } @{$rest->{$_}} ] : mk_hash(@{$rest->{$_}}) ) } keys %$rest + map { + ( $_ => + ref($rest->{$_}[0]) eq 'ARRAY' ? [ map { mk_hash(@$_) } @{$rest->{$_}} ] + : mk_hash( @{$rest->{$_}} ) + ) + } keys %$rest }; } -sub inflate_result { - my ($self, $source, $me, $prefetch) = @_; - - return mk_hash($me, $prefetch); -} - 1;