| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
exists $info->{"skip_shortcut"} and $info->{"skip_shortcut"}
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"}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 7 | my $method; | |||||||||||||||||||||||||||
| 23 | 7 | ▶ | if (exists $info->{shortcut}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 4 | $method = $info->{shortcut}; | |||||||||||||||||||||||||||
| 25 | 4 | ▶ | next SOURCE unless defined $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | ## first argument not a reference, assume find by PK | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return $rs->find(@_) if defined($_[0]) && !ref($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_[0] and not ref $_[0]
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]
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $_[0] and ref $_[0] eq "SCALAR"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $info = $schemas{$class}; | |||||||||||||||||||||||||||
| 75 | 0 | my $schema = $info->{schema}; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return $schema if $schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||