File Coverage

blib/lib/DBIx/DataModel/Schema/ResultAs/Hashref.pm
Criterion Covered Total %
statement 37 37 100.0
branch 4 6 66.6
condition 6 7 85.7
subroutine 8 8 100.0
pod 1 2 50.0
total 56 60 93.3


line stmt bran cond sub pod time code
1             #----------------------------------------------------------------------
2             package DBIx::DataModel::Schema::ResultAs::Hashref;
3             #----------------------------------------------------------------------
4 3     3   1856 use warnings;
  3         10  
  3         86  
5 3     3   14 use strict;
  3         4  
  3         88  
6 3     3   18 use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)];
  3         5  
  3         29  
7              
8 3     3   301 use parent 'DBIx::DataModel::Schema::ResultAs';
  3         9  
  3         15  
9              
10 3     3   147 use namespace::clean;
  3         6  
  3         20  
11              
12             sub new {
13 7     7 0 12 my $class = shift;
14              
15 7         11 my $self;
16              
17 7 100 100     31 if ((ref $_[0] || '') eq 'CODE') {
18 1         4 $self = {make_key => shift};
19 1 50       5 !@_ or croak "-result_as => [hashref => sub {...}] : improper other args after sub{}";
20             }
21             else {
22 6         13 $self = {cols => \@_};
23             }
24              
25 7         17 return bless $self, $class;
26             }
27              
28              
29             sub get_result {
30 7     7 1 14 my ($self, $statement) = @_;
31              
32 7   66     31 my $make_key = $self->{make_key} || do {
33             my @cols = @{$self->{cols}};
34             @cols = $statement->meta_source->primary_key if !@cols;
35             croak "-result_as=>'hashref' impossible: no primary key" if !@cols;
36 18 50   18   32 sub {my $row = shift; map {defined $row->{$_} ? $row->{$_} : ''} @cols};
  18         81  
  27         78  
37             };
38              
39 7         19 $statement->execute;
40              
41 7         10 my %hash;
42 7         19 while (my $row = $statement->next) {
43 21         52 my @key = $make_key->($row);
44 21         55 my $last_key_item = pop @key;
45 21         28 my $node = \%hash;
46 21   100     69 $node = $node->{$_} ||= {} foreach @key;
47 21         77 $node->{$last_key_item} = $row;
48             }
49 7         21 $statement->finish;
50 7         187 return \%hash;
51             }
52              
53              
54             1;
55              
56             __END__