blib/lib/DBICx/Shortcuts.pm

filter: Statementstmt 78.9% 45 / 57 Branchbran 55.0% 11 / 20 Conditioncond 33.3% 3 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 80.0% 8 / 10 Podpod 100% 3 / 3 totaltotal 68.5% 72 / 105 CC 16 SCAR 26.3
CC
16
Cov
69%
__ANON__
36.6
setup
11.3
schema
6.5
CRAP 13.9
↑ Summary
1 package DBICx::Shortcuts;
2 4 our $VERSION = '0.007';
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 my %schemas;
9  
10 4 ▶sub setup {
Subroutine setup: called CC=9 SCAR=24.3
Pod documented
11 4 my ($class, $schema_class, @methods) = @_;
12  
13 4 eval "require $schema_class";
14 4 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
15 3 local $ENV{DBIC_NO_VERSION_CHECK} = 1;
16 3 my $schema = $schema_class->connect;
17  
18 3 SOURCE: for my $source ($schema->sources) {
19 10 my $info = $schema->source($source)->source_info;
20 10 ▶ next SOURCE if exists $info->{skip_shortcut} && $info->{skip_shortcut};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"skip_shortcut"} and $info->{"skip_shortcut"} 3 7 10
Condition100%3/3
exists $info->{"skip_shortcut"} and $info->{"skip_shortcut"}
!ll&&!rl&&r
523
MC/DC100%2/2
exists $info->{"skip_shortcut"} and $info->{"skip_shortcut"}
exists $info->{"skip_shortcut"} $info->{"skip_shortcut"}
Truth table100%3/3
A and B
A= exists $info->{"skip_shortcut"}B= $info->{"skip_shortcut"}
ABresult
0X0
100
111
21  
22 7 my $method;
23 7 ▶ if (exists $info->{shortcut}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{'shortcut'}) { } 4 3 7
24 4 $method = $info->{shortcut};
25 4 ▶ next SOURCE unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 2 2 4
26 }
27 else {
28 3 $method = $source;
29 3 $method =~ s/.+::(.+)$/$1/; ## deal with nested sources
30 3 $method =~ s/([a-z])([A-Z])/${1}_$2/g;
31 3 $method = lc($method);
32 }
33  
34 5 ▶ croak("Shortcut failed, '$method' already defined in '$class', ")
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($method) 1 4 5
35 if $class->can($method);
36  
37 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶ *{__PACKAGE__ . "::$method"} = sub {
Subroutine __ANON__: not called CC=6 SCAR=36.0
39 0 my $rs = shift->schema->resultset($source);
40  
41 ## No arguments, return empty result set;
42 0 ▶ return $rs unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
43  
44 ## first argument not a reference, assume find by PK
45 0 ▶ return $rs->find(@_) if defined($_[0]) && !ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not ref $_[0] 0 0 0
Condition0%0/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table0%0/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
46  
47 ## first argument is a scalar ref, assume unique constraint name,
48 ## use find
49 0 ▶ return $rs->find(@_[1 .. $#_], {key => ${$_[0]}})
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] eq "SCALAR" 0 0 0
Condition0%0/3
defined $_[0] and ref $_[0] eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "SCALAR"
defined $_[0] ref $_[0] eq "SCALAR"
Truth table0%0/3
A and B
A= defined $_[0]B= ref $_[0] eq "SCALAR"
ABresult
0X0
100
111
50 if defined($_[0]) && ref($_[0]) eq 'SCALAR';
51  
52 ## otherwise, its a search
53 0 return $rs->search(@_);
54 4 };
55 }
56  
57 ## Enable set of schema shortcuts
58 2 for my $meth (@methods) {
59 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 0 ▶ *{__PACKAGE__ . "::$meth"} = sub { return shift->schema->$meth(@_) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
61 }
62  
63 2 $schemas{$class} = {class => $schema_class};
64  
65 2 return;
66 }
67  
68 1 ▶sub schema {
Subroutine schema: called CC=3 SCAR=18.7
Pod documented
69 1 my ($class) = @_;
70  
71 croak("Class '$class' did not call 'setup()'")
72 1 ▶ unless exists $schemas{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schemas{$class} 1 0 1
73  
74 0 my $info = $schemas{$class};
75 0 my $schema = $info->{schema};
76 0 ▶ return $schema if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 0 0 0
77  
78 0 my @connect_args = $class->connect_info();
79 0 return $info->{schema} = $info->{class}->connect(@connect_args);
80 }
81  
82 1 ▶sub connect_info {
Subroutine connect_info: called CC=1 SCAR=0.0
Pod documented
83 1 croak("Class '$_[0]' needs to override 'connect_info()', ");
84 }
85  
86 4 1;
87  
88 __END__
↑ Summary