blib/lib/DBIx/ActiveRecord/Relation.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 96 CC 7 SCAR 17.0
CC
7
Cov
0%
first
12.0
last
12.0
AUTOLOAD
6.0
CRAP 5.5
1 package DBIx::ActiveRecord::Relation;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 use DBIx::ActiveRecord;
7 use DBIx::ActiveRecord::Scope;
8  
9 use overload '@{}' => \&all;
10  
11 sub new {
12 my ($self, $model) = @_;
13 bless {model => $model, arel => $model->arel}, $self;
14 }
15  
16 sub scoped {
17 my ($self) = @_;
18 my $s = __PACKAGE__->new($self->{model});
19 $s->{arel} = $self->{arel}->clone;
20 $s;
21 }
22  
23 sub to_sql {shift->{arel}->to_sql}
24 sub _binds {shift->{arel}->binds}
25  
26 our $AUTOLOAD;
27 sub AUTOLOAD {
28 my $self = shift;
29 $AUTOLOAD =~ /([^:]+)$/;
30 my $m = $1;
31 my $s = $self->{model}->_global->{scopes}->{$m};
32 croak "method missing $AUTOLOAD" if !$s;
33 $s->($self->scoped, @_);
34 }
35  
36 sub DESTROY{}
37  
38 sub all {
39 my $self = shift;
40 $self->{cache}->{all} ||= DBIx::ActiveRecord::Scope::all($self, @_);
41 }
42  
43 sub first {
44 my $self = shift;
45 return $self->{cache}->{all}->[0] if $self->{cache}->{all};
46 return $self->{cache}->{first} if exists $self->{cache}->{first};
47 $self->{cache}->{first} = DBIx::ActiveRecord::Scope::first($self, @_);
48 }
49  
50 sub last {
51 my $self = shift;
52 return $self->{cache}->{all}->[-1] if $self->{cache}->{all};
53 return $self->{cache}->{last} if exists $self->{cache}->{last};
54 $self->{cache}->{last} = DBIx::ActiveRecord::Scope::last($self, @_);
55 }
56  
57 sub count {
58 my ($self) = @_;
59 return $self->{cache}->{count} if exists $self->{cache}->{count};
60 my $arel = $self->{arel}->count;
61 my $sth = $self->{model}->dbh->prepare($arel->to_sql);
62 $sth->execute($arel->binds) || croak $sth->errstr;
63 my $row = $sth->fetchrow_arrayref;
64 $self->{cache}->{count} = $row->[0];
65 }
66  
67 1;