Meta/Role/Relationship/ViaFK.pm

filter: Statementstmt 100% 49 / 49 Branchbran 90.0% 18 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 96.3% 79 / 82 SCAR 24.0
CC
11
Cov
97%
_invert_fk_if_necessary
6.0
_find_one_fk_between_tables
4.0
__ANON__
2.0
CRAP 11.0
« ORM/Role/Iterator.pm ↑ Summary Object/Iterator/FromSelect/Caching.pm »
1 package Fey::Meta::Role::Relationship::ViaFK;
2 {
3 10 $Fey::Meta::Role::Relationship::ViaFK::VERSION = '0.46';
4 }
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Fey::ORM::Types qw( Bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 has fk => (
16 is => 'ro',
17 isa => 'Fey::FK',
18 lazy => 1,
19 builder => '_build_fk',
20 predicate => '_has_fk',
21 writer => '_set_fk',
22 );
23  
24 has _is_has_many => (
25 is => 'ro',
26 isa => Bool,
27 lazy => 1,
28 14 ▶ default => sub { ( ref $_[0] ) =~ /HasMany/ ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref($_[0]) =~ /HasMany/ ? : 6 8 14
29 10 );
30  
31 ▶sub BUILD { }
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
32  
33 after BUILD => sub {
34 16 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
35  
36 16 ▶ return unless $self->_has_fk();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_fk 14 2 16
37  
38 2 $self->_set_fk( $self->_invert_fk_if_necessary( $self->fk() ) );
39 10 };
40  
41 sub _build_fk {
42 14 ▶ my $self = shift;
Subroutine _build_fk: called CC=1 SCAR=0.0
43  
44 14 $self->_find_one_fk_between_tables(
45 $self->table(),
46 $self->foreign_table(),
47 );
48 }
49  
50 sub _find_one_fk_between_tables {
51 14 ▶ my $self = shift;
Subroutine _find_one_fk_between_tables: called CC=4 SCAR=13.9
52 14 my $source_table = shift;
53 14 my $target_table = shift;
54  
55 14 my @fk = $source_table->schema()
56 ->foreign_keys_between_tables( $source_table, $target_table );
57  
58 14 ▶ my $desc = $self->_is_has_many() ? 'has_many' : 'has_one';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_has_many ? : 6 8 14
59  
60 14 ▶ if ( @fk == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@fk == 0) { } 2 12 14
elsif (@fk > 1) { } 2 10 12
61 2 param_error
62 'There are no foreign keys between the table for this class, '
63 . $source_table->name()
64 . " and the table you passed to $desc(), "
65 . $target_table->name() . '.';
66 }
67 elsif ( @fk > 1 ) {
68 2 param_error
69 'There is more than one foreign key between the table for this class, '
70 . $source_table->name()
71 . " and the table you passed to $desc(), "
72 . $target_table->name()
73 . '. You must specify one explicitly.';
74 }
75  
76 10 return $self->_invert_fk_if_necessary( $fk[0] );
77 }
78  
79 # We may need to invert the meaning of source & target since source &
80 # target for an FK object are sort of arbitrary. The source should be
81 # "our" table, and the target the foreign table.
82 sub _invert_fk_if_necessary {
83 12 ▶ my $self = shift;
Subroutine _invert_fk_if_necessary: called CC=6 SCAR=18.0
84 12 my $fk = shift;
85  
86 # Self-referential keys are a special case, and that case differs
87 # for has_one vs has_many.
88 12 ▶ if ( $fk->is_self_referential() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fk->is_self_referential) { } 2 10 12
89 2 ▶ if ( $self->_is_has_many() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_has_many) { } 1 1 2
90 return $fk
91 unless $fk->target_table()
92 1 ▶ ->has_candidate_key( @{ $fk->target_columns() } );
Branch: 1 branch
BranchTrueFalseTotal
unless $fk->target_table->has_candidate_key(@{$fk->target_columns;}) 0 1 1
93 }
94 else {
95  
96 # A self-referential key is a special case. If the target
97 # columns are _not_ a key, then we need to invert source &
98 # target so we do our select by a key. This doesn't
99 # address a pathological case where neither source nor
100 # target column sets make up a key. That shouldn't happen,
101 # though ;)
102 return $fk
103 if $fk->target_table()
104 1 ▶ ->has_candidate_key( @{ $fk->target_columns() } );
Branch: 1 branch
BranchTrueFalseTotal
if $fk->target_table->has_candidate_key(@{$fk->target_columns;}) 1 0 1
105 }
106 }
107 else {
108 10 ▶ return $fk
Branch: 1 branch
BranchTrueFalseTotal
if $fk->target_table->name eq $self->foreign_table->name 6 4 10
109 if $fk->target_table()->name() eq $self->foreign_table->name();
110 }
111  
112 5 return Fey::FK->new(
113 source_columns => $fk->target_columns(),
114 target_columns => $fk->source_columns(),
115 );
116 }
117  
118 10 1;
« ORM/Role/Iterator.pm ↑ Summary Object/Iterator/FromSelect/Caching.pm »