File Coverage

blib/lib/Aniki/Result/Collection.pm
Criterion Covered Total %
statement 25 27 92.5
branch 2 2 100.0
condition n/a
subroutine 9 11 81.8
pod 5 5 100.0
total 41 45 91.1


line stmt bran cond sub pod time code
1             package Aniki::Result::Collection;
2 27     27   468 use 5.014002;
  27         94  
3              
4 27     27   142 use namespace::autoclean;
  27         232  
  27         168  
5 27     27   1635 use Mouse v2.4.5;
  27         416  
  27         161  
6             extends qw/Aniki::Result/;
7              
8             use overload
9 0     0   0 '@{}' => sub { shift->rows },
10 27     27   35741 fallback => 1;
  27         22752  
  27         211  
11              
12             has row_datas => (
13             is => 'ro',
14             required => 1,
15             );
16              
17             has inflated_rows => (
18             is => 'ro',
19             lazy => 1,
20             builder => '_inflate',
21             );
22              
23             sub _inflate {
24 68     68   141 my $self = shift;
25 68         347 my $row_class = $self->row_class;
26 68         462 my $table_name = $self->table_name;
27 68         211 my $handler = $self->handler;
28             return [
29             map {
30 132         1128 $row_class->new(
31             table_name => $table_name,
32             handler => $handler,
33             row_data => $_
34             )
35 68         127 } @{ $self->row_datas }
  68         268  
36             ];
37             }
38              
39             sub rows {
40 158     158 1 332 my $self = shift;
41 158 100       1265 return $self->suppress_row_objects ? $self->row_datas : $self->inflated_rows;
42             }
43              
44 72     72 1 22473 sub count { scalar @{ shift->rows(@_) } }
  72         235  
45              
46 21     21 1 81 sub first { shift->rows(@_)->[0] }
47 0     0 1 0 sub last :method { shift->rows(@_)->[-1] }
48 55     55 1 7945 sub all { @{ shift->rows(@_) } }
  55         137  
49              
50             __PACKAGE__->meta->make_immutable();
51             __END__
52              
53             =pod
54              
55             =encoding utf-8
56              
57             =head1 NAME
58              
59             Aniki::Result::Collection - Rows as a collection
60              
61             =head1 SYNOPSIS
62              
63             my $result = $db->select(foo => { bar => 1 });
64             for my $row ($result->all) {
65             print $row->id, "\n";
66             }
67              
68             =head1 DESCRIPTION
69              
70             This is collection result class.
71              
72             =head1 INSTANCE METHODS
73              
74             =head2 C<rows()>
75              
76             Returns rows as array reference.
77              
78             =head2 C<count()>
79              
80             Returns rows count.
81              
82             =head2 C<first()>
83              
84             Returns first row.
85              
86             =head2 C<last()>
87              
88             Returns last row.
89              
90             =head2 C<all()>
91              
92             Returns rows as array.
93              
94             =head1 ACCESSORS
95              
96             =over 4
97              
98             =item C<handler : Aniki>
99              
100             =item C<table_name : Str>
101              
102             =item C<suppress_row_objects : Bool>
103              
104             =item C<row_class : ClassName>
105              
106             =item C<row_datas : ArrayRef[HashRef]>
107              
108             =item C<inflated_rows : ArrayRef[Aniki::Row]>
109              
110             =back
111              
112             =head1 LICENSE
113              
114             Copyright (C) karupanerura.
115              
116             This library is free software; you can redistribute it and/or modify
117             it under the same terms as Perl itself.
118              
119             =head1 AUTHOR
120              
121             karupanerura E<lt>karupa@cpan.orgE<gt>
122              
123             =cut