File Coverage

blib/lib/DBIx/DataModel/Schema/ResultAs/Categorize.pm
Criterion Covered Total %
statement 37 37 100.0
branch 4 6 66.6
condition 4 5 80.0
subroutine 10 10 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::Categorize;
3             #----------------------------------------------------------------------
4 1     1   620 use warnings;
  1         2  
  1         37  
5 1     1   6 use strict;
  1         3  
  1         25  
6 1     1   5 use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)];
  1         2  
  1         6  
7 1     1   105 use List::Categorize 0.04 qw/categorize/;
  1         21  
  1         81  
8              
9 1     1   8 use parent 'DBIx::DataModel::Schema::ResultAs';
  1         2  
  1         6  
10              
11 1     1   45 use namespace::clean;
  1         3  
  1         7  
12              
13             sub new {
14 2     2 0 6 my $class = shift;
15              
16 2 50       6 @_ or croak "-result_as => [categorize => ...] ... need field names or sub{} ";
17              
18 2         5 my $self;
19              
20 2 100 100     11 if ((ref $_[0] || '') eq 'CODE') {
21 1         3 $self = {make_key => shift};
22 1 50       8 !@_ or croak "-result_as => [categorize => sub {...}] : improper other args after sub{}";
23             }
24             else {
25 1         5 $self = {cols => \@_};
26             }
27              
28 2         7 return bless $self, $class;
29             }
30              
31             sub get_result {
32 2     2 1 5 my ($self, $statement) = @_;
33              
34              
35 2   66     10 my $make_key = $self->{make_key} || do {
36             my @cols = @{$self->{cols}};
37 4     4   8 sub {my $row = shift; @{$row}{@cols}};
  4         5  
  4         13  
38             };
39              
40              
41 2         7 $statement->execute;
42 2         7 my $rows = $statement->all;
43 2     7   13 my %result = categorize {$make_key->($_)} @$rows;
  7         177  
44 2         56 $statement->finish;
45              
46 2         74 return \%result;
47             }
48              
49              
50             1;
51              
52              
53             __END__