blib/lib/DBIO/Extension/List.pm

filter: Statementstmt 96.7% 88 / 91 Branchbran 83.3% 45 / 54 Conditioncond 76.4% 13 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 88.8% 168 / 189 CC 39 SCAR 27.2
CC
39
Cov
90%
new
25.5
check_driver
6.1
_load_class
6.1
CRAP 15.2
« blib/lib/DBIO/Generate/Style/Vanilla.pm ↑ Summary blib/lib/DBIO/Relationship/BelongsTo.pm »
1 package DBIO::Extension::List;
2 # ABSTRACT: The declared list of database extensions of a schema, resolved to classes
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.900007';
8  
9 6 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use DBIO::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14  
15 44 ▶sub new {
Subroutine new: called CC=25 SCAR=32.4
Pod documented
16 44 my ($class, %args) = @_;
17 44 ▶ my $declaration = $args{declaration} || [];
Condition50%1/2
$args{'declaration'} || []
l!l
440
MC/DC0%0/1
$args{'declaration'} || []
$args{'declaration'}
Truth table50%1/2
A
A= $args{'declaration'}
Aresult
11
00
18 44 my $table = $args{class_table};
19 44 my $plain = $args{plain_class};
20  
21 44 ▶ DBIO::Exception->throw('DBIO::Extension::List: declaration must be an array reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $declaration eq "ARRAY" 0 44 44
22 unless ref $declaration eq 'ARRAY';
23  
24 44 my %entry;
25 44 my @decl = @$declaration;
26 44 ▶ while (@decl) {
Branch: 1 branch
BranchTrueFalseTotal
if (@decl) 61 29 90
27 61 my $name = shift @decl;
28 61 ▶ DBIO::Exception->throw(
Branch: 3 branches
BranchTrueFalseTotal
if not defined $name or ref $name 1 60 61
ref $name ? : 1 0 1
defined $name ? : 0 0 0
Condition66%2/3
not defined $name or ref $name
l!l&&r!l&&!r
0160
MC/DC50%1/2
not defined $name or ref $name
not defined $name ref $name
Truth table66%2/3
not(A) or B
A= not defined $nameB= ref $name
ABresult
1X1
011
000
29 'Extension declaration: expected an extension name, got '
30 . (ref $name ? 'a ' . ref($name) . ' reference' : defined $name ? "'$name'" : 'undef')
31 . ' (options follow the name they belong to)'
32 ) if !defined $name || ref $name;
33 60 DBIO::Util::assert_identifier_text('Extension declaration: an extension name', $name);
34  
35 56 my %options;
36 56 ▶ if (@decl && ref $decl[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@decl and ref $decl[0]) 29 27 56
Condition100%3/3
@decl and ref $decl[0]
!ll&&!rl&&r
18929
MC/DC100%2/2
@decl and ref $decl[0]
@decl ref $decl[0]
Truth table100%3/3
A and B
A= @declB= ref $decl[0]
ABresult
0X0
100
111
37 29 my $options = shift @decl;
38 29 ▶ DBIO::Exception->throw("Extension '$name': the options must be a hash reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options eq "HASH" 0 29 29
39 unless ref $options eq 'HASH';
40 29 %options = %$options;
41 }
42  
43 DBIO::Util::assert_identifier_text("Extension '$name': option 'schema'", $options{schema})
44 56 ▶ if exists $options{schema};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"schema"} 8 48 56
45  
46 52 my $explicit = delete $options{class};
47 52 ▶ my ($resolved, $distribution) = defined $explicit ? ($explicit)
Branch: 2 branches
BranchTrueFalseTotal
defined $explicit ? : 6 46 52
$table ? : 46 0 46
48 : $table ? $table->($name) : ();
49 52 ▶ $resolved //= $plain;
Condition100%3/3
$resolved //= $plain
l!l&&r!l&&!r
4741
MC/DC100%2/2
$resolved //= $plain
$resolved $plain
Truth table100%3/3
A //= B
A= $resolvedB= $plain
ABresult
1X1
011
000
50 52 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resolved 1 51 52
51 "Extension '$name' is not known to the driver and no class is named for it "
52 . "(declare it as $name => { class => 'My::Extension::Class' })"
53 ) unless defined $resolved;
54  
55 51 ▶ if (my $have = $entry{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $have = $entry{$name}) 11 40 51
56 DBIO::Exception->throw(
57 "Extension '$name' is declared with two classes: $have->{class} and $resolved"
58 11 ▶ ) if $have->{class} ne $resolved;
Branch: 1 branch
BranchTrueFalseTotal
if $have->{"class"} ne $resolved 1 10 11
59 10 for my $key (sort keys %options) {
60 9 ▶ next unless exists $have->{options}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $have->{"options"}{$key} 1 8 9
61 DBIO::Exception->throw(
62 "Extension '$name' is declared with two values for option '$key'"
63 8 ▶ ) if DBIO::Util::canonical_text($have->{options}{$key}) ne DBIO::Util::canonical_text($options{$key});
Branch: 1 branch
BranchTrueFalseTotal
if DBIO::Util::canonical_text($have->{"options"}{$key}) ne DBIO::Util::canonical_text($options{$key}) 4 4 8
64 }
65 6 %{ $have->{options} } = (%{ $have->{options} }, %options);
66 6 next;
67 }
68  
69 40 ▶ $entry{$name} = {
Branch: 1 branch
BranchTrueFalseTotal
defined $distribution ? : 18 22 40
70 name => $name,
71 class => $resolved,
72 options => \%options,
73 (defined $distribution ? (_distribution => $distribution) : ()),
74 };
75 }
76  
77 29 for my $name (sort keys %entry) {
78 35 my $entry = $entry{$name};
79 35 _load_class($name, $entry->{class}, delete $entry->{_distribution});
80 33 DBIO::Extension->check_interface($entry->{class});
81 32 my $states = $entry->{class}->name;
82 32 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if defined $states and $states ne $name 1 31 32
Condition100%3/3
defined $states and $states ne $name
!ll&&!rl&&r
6251
MC/DC100%2/2
defined $states and $states ne $name
defined $states $states ne $name
Truth table100%3/3
A and B
A= defined $statesB= $states ne $name
ABresult
0X0
100
111
83 "Extension '$name' is declared with class $entry->{class}, which is the extension '$states'"
84 ) if defined $states && $states ne $name;
85 }
86  
87 25 return bless { entries => \%entry }, $class;
88 }
89  
90 35 ▶sub _load_class {
Subroutine _load_class: called CC=6 SCAR=18.1
91 35 my ($name, $class, $distribution) = @_;
92 35 DBIO::Util::assert_class_name($class);
93 35 ▶ return if $class->can('extension_interface');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("extension_interface") 26 9 35
94  
95 9 (my $file = "$class.pm") =~ s{::}{/}g;
96 9 ▶ return if $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$file} 0 9 9
97 9 ▶ return if eval { require $file; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require $file; 1 } } 7 2 9
98  
99 2 my $error = $@;
100 2 ▶ if ($error =~ /^Can't locate \Q$file\E in \@INC/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /^Can't locate \Q$file\E in \@INC/) 2 0 2
101 2 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
defined $distribution ? : 1 1 2
102 "Extension '$name': class $class is not installed"
103 . (defined $distribution ? " -- install the distribution $distribution" : '')
104 );
105 }
106 0 DBIO::Exception->throw("Extension '$name': class $class does not load: $error");
107 }
108  
109  
110 18 ▶sub entries {
Subroutine entries: called CC=1 SCAR=0.0
Pod documented
111 18 my ($self) = @_;
112 26 return map { { %$_, options => { %{ $_->{options} } } } }
113 18 map { $self->{entries}{$_} } sort keys %{ $self->{entries} };
114 }
115  
116  
117 0 ▶sub names { sort keys %{ $_[0]{entries} } }
Subroutine names: not called CC=1 SCAR=6.9
Pod documented
118  
119  
120 2 ▶sub set_installed {
Subroutine set_installed: called CC=5 SCAR=16.1
Pod documented
121 2 my ($self, $state) = @_;
122 2 for my $name (sort keys %$state) {
123 3 ▶ my $entry = $self->{entries}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->{"entries"}{$name} or "DBIO::Exception"->throw("Extension '${name}' is not in the declared list; its installed state is not attached") 2 1 3
124 or DBIO::Exception->throw("Extension '$name' is not in the declared list; its installed state is not attached");
125 2 for my $key (qw(installed version schema)) {
126 6 ▶ $entry->{$key} = $state->{$name}{$key} if exists $state->{$name}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $state->{$name}{$key} 4 2 6
127 }
128 }
129 1 return $self;
130 }
131  
132  
133 3 ▶sub check_driver {
Subroutine check_driver: called CC=6 SCAR=18.2
Pod documented
134 3 my ($self, $resolved) = @_;
135 3 for my $name (sort keys %{ $self->{entries} }) {
136 5 my $class = $self->{entries}{$name}{class};
137 5 my $asserted = $class->driver;
138 5 ▶ next unless defined $asserted;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $asserted 1 4 5
139 4 ▶ next if $asserted eq $resolved;
Branch: 1 branch
BranchTrueFalseTotal
if $asserted eq $resolved 3 1 4
140 1 ▶ next if $resolved->can('isa') && $resolved->isa($asserted);
Branch: 1 branch
BranchTrueFalseTotal
if $resolved->can("isa") and $resolved->isa($asserted) 0 1 1
Condition33%1/3
$resolved->can("isa") and $resolved->isa($asserted)
!ll&&!rl&&r
010
MC/DC0%0/2
$resolved->can("isa") and $resolved->isa($asserted)
$resolved->can("isa") $resolved->isa($asserted)
Truth table33%1/3
A and B
A= $resolved->can("isa")B= $resolved->isa($asserted)
ABresult
0X0
100
111
141 1 DBIO::Exception->throw(
142 "Extension '$name' ($class) belongs to the driver $asserted, "
143 . "but the schema's driver is $resolved"
144 );
145 }
146 2 return 1;
147 }
148  
149 6 1;
150  
151 __END__
« blib/lib/DBIO/Generate/Style/Vanilla.pm ↑ Summary blib/lib/DBIO/Relationship/BelongsTo.pm »