blib/lib/DBIO/PopulateMore.pm

filter: Statementstmt 87.3% 62 / 71 Branchbran 69.4% 25 / 36 Conditioncond 37.5% 9 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 68.1% 105 / 154 CC 29 SCAR 27.7
CC
29
Cov
73%
_populate_more_dispatch
27.1
populate_more
16.7
_populate_more_visit
7.0
CRAP 16.0
« blib/lib/DBIO/Diff/Compare.pm ↑ Summary blib/lib/DBIO/Storage/BlockRunner.pm »
1 package DBIO::PopulateMore;
2  
3 # ABSTRACT: Enhanced populate with cross-source references
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.900007';
9  
10 4 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 4 my $MATCH_CONDITION = qr/^!(\w+:.+)$/;
15  
16  
17 6 ▶sub populate_more {
Subroutine populate_more: called CC=14 SCAR=28.2
Pod documented
18 6 my ($self, $arg, @rest) = @_;
19  
20 6 ▶ $self->throw_exception("Argument is required.")
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 6 6
21 unless $arg;
22  
23 6 ▶ my @args = (ref $arg && ref $arg eq 'ARRAY') ? @$arg : ($arg, @rest);
Branch: 1 branch
BranchTrueFalseTotal
ref $arg && ref $arg eq 'ARRAY' ? : 6 0 6
Condition33%1/3
ref $arg and ref $arg eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $arg and ref $arg eq "ARRAY"
ref $arg ref $arg eq "ARRAY"
Truth table33%1/3
A and B
A= ref $argB= ref $arg eq "ARRAY"
ABresult
0X0
100
111
24  
25 6 my @definitions;
26 6 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 10 6 16
27 10 my $next = shift @args;
28 10 ▶ if (ref $next && ref $next eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next and ref $next eq 'HASH') { } 10 0 10
Condition33%1/3
ref $next and ref $next eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $next and ref $next eq "HASH"
ref $next ref $next eq "HASH"
Truth table33%1/3
A and B
A= ref $nextB= ref $next eq "HASH"
ABresult
0X0
100
111
29 10 push @definitions, $next;
30 } else {
31 0 my $value = shift @args;
32 0 push @definitions, { $next => $value };
33 }
34 }
35  
36 # The state is passed explicitly to the visitor and the dispatcher: they
37 # are plain subs, not closures holding themselves, so nothing outlives a
38 # populate_more that dies half way.
39 6 my $state = { schema => $self, seen => {}, rs_index => {} };
40  
41 6 for my $definition (@definitions) {
42 10 my ($source, $info) = %$definition;
43 my @fields = ref $info->{fields} eq 'ARRAY'
44 2 ? @{$info->{fields}}
45 10 ▶ : ($info->{fields});
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'fields'} eq 'ARRAY' ? : 2 8 10
46  
47 10 my $data = _populate_more_visit($state, $info->{data});
48  
49 6 ▶ while (my ($rs_key, $values) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 804 6 810
50 804 ▶ my @values = ref $values eq 'ARRAY' ? @$values : ($values);
Branch: 1 branch
BranchTrueFalseTotal
ref $values eq 'ARRAY' ? : 802 2 804
51  
52 804 my %create;
53 804 for my $i (0 .. $#fields) {
54 808 ▶ $create{$fields[$i]} = $values[$i] if defined $fields[$i] && defined $values[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields[$i] and defined $values[$i] 808 0 808
Condition33%1/3
defined $fields[$i] and defined $values[$i]
!ll&&!rl&&r
00808
MC/DC0%0/2
defined $fields[$i] and defined $values[$i]
defined $fields[$i] defined $values[$i]
Truth table33%1/3
A and B
A= defined $fields[$i]B= defined $values[$i]
ABresult
0X0
100
111
55 }
56  
57 804 my $new = $self->resultset($source)->update_or_create(\%create);
58 804 $state->{rs_index}{"$source.$rs_key"} = $new;
59 }
60 }
61  
62 2 return %{ $state->{rs_index} };
63 }
64  
65 # Visitor: recursively walk data structures, inflating !-prefixed values
66 2433 ▶sub _populate_more_visit {
Subroutine _populate_more_visit: called CC=7 SCAR=19.5
67 2433 my ($state, $target) = @_;
68 2433 my $seen = $state->{seen};
69 2433 ▶ if (ref $target eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $target eq 'ARRAY') { } 806 1627 2433
elsif (ref $target eq 'HASH') { } 10 1617 1627
elsif (defined $target and $target =~ /$MATCH_CONDITION/) { } 6 1611 1617
70 806 my $addr = refaddr $target;
71 806 ▶ return $seen->{$addr} if defined $seen->{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen->{$addr} 0 806 806
72 806 my $new = $seen->{$addr} = [];
73 806 @$new = map { _populate_more_visit($state, $_) } @$target;
74 802 return $new;
75 } elsif (ref $target eq 'HASH') {
76 10 my $addr = refaddr $target;
77 10 ▶ return $seen->{$addr} if defined $seen->{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen->{$addr} 0 10 10
78 10 my $new = $seen->{$addr} = {};
79 10 %$new = map { $_ => _populate_more_visit($state, $target->{$_}) } keys %$target;
80 6 return $new;
81 ▶ } elsif (defined $target && $target =~ $MATCH_CONDITION) {
Condition66%2/3
defined $target and $target =~ /$MATCH_CONDITION/
!ll&&!rl&&r
016116
MC/DC50%1/2
defined $target and $target =~ /$MATCH_CONDITION/
defined $target $target =~ /$MATCH_CONDITION/
Truth table66%2/3
A and B
A= defined $targetB= $target =~ /$MATCH_CONDITION/
ABresult
0X0
100
111
82 6 return _populate_more_dispatch($state, $1);
83 } else {
84 1611 return $target;
85 }
86 }
87  
88 # Dispatcher: route !Name:args to the correct inflator
89 6 ▶sub _populate_more_dispatch {
Subroutine _populate_more_dispatch: called CC=10 SCAR=33.0
90 6 my ($state, $arg) = @_;
91 6 my $schema = $state->{schema};
92 6 my ($name, $command) = ($arg =~ /^(\w+):(\S.*)$/);
93  
94 6 ▶ if ($name eq 'Index') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'Index') { } 4 2 6
elsif ($name eq 'Env') { } 1 1 2
elsif ($name eq 'Find') { } 0 1 1
95 4 ▶ return $state->{rs_index}{$command}
Condition66%2/3
$state->{'rs_index'}{$command} || $schema->throw_exception("Bad Index in fixture: $command")
l!l&&r!l&&!r
202
MC/DC0%0/2
$state->{'rs_index'}{$command} || $schema->throw_exception("Bad Index in fixture: $command")
$state->{'rs_index'}{$command} $schema->throw_exception("Bad Index in fixture: $command")
Truth table33%1/3
A || B
A= $state->{'rs_index'}{$command}B= $schema->throw_exception("Bad Index in fixture: $command")
ABresult
1X1
011
000
96 || $schema->throw_exception("Bad Index in fixture: $command");
97 } elsif ($name eq 'Env') {
98 return $ENV{$command}
99 1 ▶ // $ENV{uc $command}
Condition33%2/6
($ENV{$command} // $ENV{uc $command}) // $schema->throw_exception("No match for $command in %ENV")
l!l&&r!l&&!r
001
$ENV{$command} // $ENV{uc $command}
l!l&&r!l&&!r
001
MC/DC0%0/3
($ENV{$command} // $ENV{uc $command}) // $schema->throw_exception("No match for $command in %ENV")
$ENV{$command} $ENV{uc $command} $schema->throw_exception("No match for $command in %ENV")
Truth table0%0/4
A // B // C
A= $ENV{$command}B= $ENV{uc $command}C= $schema->throw_exception("No match for $command in %ENV")
ABCresult
1XX1
01X1
0011
0000
100 // $schema->throw_exception("No match for $command in \%ENV");
101 } elsif ($name eq 'Find') {
102 0 my ($source, $id) = split(/\./, $command, 2);
103 0 ▶ my $rs = $schema->resultset($source)
Branch: 1 branch
BranchTrueFalseTotal
my $rs = $schema->resultset($source) or $schema->throw_exception("Can't find resultset for $source") 0 0 0
104 or $schema->throw_exception("Can't find resultset for $source");
105 0 ▶ if ($id =~ /^\[(.+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^\[(.+)\]$/) 0 0 0
106 0 my %keys = map { split(/=/, $_, 2) } split(/,/, $1);
107 0 $id = \%keys;
108 }
109 0 ▶ return $rs->find($id)
Condition0%0/3
$rs->find($id) || $schema->throw_exception("Can't find result for '${id}' in '${source}'")
l!l&&r!l&&!r
000
MC/DC0%0/2
$rs->find($id) || $schema->throw_exception("Can't find result for '${id}' in '${source}'")
$rs->find($id) $schema->throw_exception("Can't find result for '${id}' in '${source}'")
Truth table0%0/3
A || B
A= $rs->find($id)B= $schema->throw_exception("Can't find result for '${id}' in '${source}'")
ABresult
1X1
011
000
110 || $schema->throw_exception("Can't find result for '$id' in '$source'");
111 } else {
112 1 $schema->throw_exception("Unknown inflator: $name");
113 }
114 }
115  
116 4 1;
117  
118 __END__
« blib/lib/DBIO/Diff/Compare.pm ↑ Summary blib/lib/DBIO/Storage/BlockRunner.pm »