| 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
| |||||||||||||||||||||||||||||||||||
| 21 | unless $arg; | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | my @args = (ref $arg && ref $arg eq 'ARRAY') ? @$arg : ($arg, @rest); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $arg and ref $arg eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 6 | my @definitions; | |||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | while (@args) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 27 | 10 | my $next = shift @args; | |||||||||||||||||||||||||||||||||
| 28 | 10 | ▶ | if (ref $next && ref $next eq 'HASH') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $next and ref $next eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | 10 | my $data = _populate_more_visit($state, $info->{data}); | |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | while (my ($rs_key, $values) = each %$data) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | 804 | ▶ | my @values = ref $values eq 'ARRAY' ? @$values : ($values); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $fields[$i] and defined $values[$i]
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]
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 70 | 806 | my $addr = refaddr $target; | |||||||||||||||||||||||||||||||||
| 71 | 806 | ▶ | return $seen->{$addr} if defined $seen->{$addr}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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/
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/
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | return $state->{rs_index}{$command} | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$state->{'rs_index'}{$command} || $schema->throw_exception("Bad Index in fixture: $command")
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")
| |||||||||||||||||||||||||||||||||||
| 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")
$ENV{$command} // $ENV{uc $command}
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")
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 104 | or $schema->throw_exception("Can't find resultset for $source"); | ||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($id =~ /^\[(.+)\]$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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}'")
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}'")
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||