| 1 | package App::DBCritic::Policy::BidirectionalRelationship; | ||
| 2 | |||
| 3 | # ABSTRACT: Check for missing bidirectional relationships in ResultSources | ||
| 4 | |||
| 5 | #pod =head1 SYNOPSIS | ||
| 6 | #pod | ||
| 7 | #pod use App::DBCritic; | ||
| 8 | #pod | ||
| 9 | #pod my $critic = App::DBCritic->new( | ||
| 10 | #pod dsn => 'dbi:Oracle:HR', username => 'scott', password => 'tiger'); | ||
| 11 | #pod $critic->critique(); | ||
| 12 | #pod | ||
| 13 | #pod =head1 DESCRIPTION | ||
| 14 | #pod | ||
| 15 | #pod This policy returns a violation if one or more of a | ||
| 16 | #pod L<DBIx::Class::ResultSource|DBIx::Class::ResultSource>'s relationships does not | ||
| 17 | #pod have a corresponding reverse relationship in the other class. | ||
| 18 | #pod | ||
| 19 | #pod =cut | ||
| 20 | |||
| 21 | 6 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 6 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 6 | ▶ | use Modern::Perl '2011'; ## no critic (Modules::ProhibitUseQuotedVersion) |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | |||
| 25 | 2 | our $VERSION = '0.023'; # VERSION | |
| 26 | 6 | ▶ | use English '-no_match_vars'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 6 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 6 | ▶ | use Sub::Quote; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 6 | ▶ | use namespace::autoclean -also => qr{\A _}xms; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | 2 | has description => ( | |
| 32 | is => 'ro', | ||
| 33 | default => quote_sub q{'Missing bidirectional relationship'}, | ||
| 34 | ); | ||
| 35 | |||
| 36 | #pod =attr description | ||
| 37 | #pod | ||
| 38 | #pod "Missing bidirectional relationship" | ||
| 39 | #pod | ||
| 40 | #pod =cut | ||
| 41 | |||
| 42 | 2 | has explanation => ( | |
| 43 | is => 'ro', | ||
| 44 | default => quote_sub | ||
| 45 | q{'Related tables should have relationships defined in both classes.'}, | ||
| 46 | ); | ||
| 47 | |||
| 48 | #pod =attr explanation | ||
| 49 | #pod | ||
| 50 | #pod "Related tables should have relationships defined in both classes." | ||
| 51 | #pod | ||
| 52 | #pod =cut | ||
| 53 | |||
| 54 | 7 | ▶ | sub violates { |
|
Subroutine
violates: called CC=1 SCAR=0.0
Pod
documented
| |||
| 55 | 7 | my $source = shift->element; | |
| 56 | |||
| 57 | return join "\n", | ||
| 58 | 2 | map { _message( $source->name, $source->related_source($_)->name ) } | |
| 59 | 7 | grep { !keys %{ $source->reverse_relationship_info($_) } } | |
| 60 | $source->relationships; | ||
| 61 | } | ||
| 62 | |||
| 63 | #pod =method violates | ||
| 64 | #pod | ||
| 65 | #pod If the L<"current element"|App::DBCritic::Policy>'s | ||
| 66 | #pod L<relationships|DBIx::Class::ResultSource/relationships> do not all have | ||
| 67 | #pod corresponding | ||
| 68 | #pod L<"reverse relationships"|DBIx::Class::ResultSource/reverse_relationship_info>, | ||
| 69 | #pod returns a string describing details of the issue. | ||
| 70 | #pod | ||
| 71 | #pod =cut | ||
| 72 | |||
| 73 | 5 | ▶ | sub _message { return "$_[0] to $_[1] not reciprocated" } |
|
Subroutine
_message: called CC=1 SCAR=0.0
| |||
| 74 | |||
| 75 | 2 | with 'App::DBCritic::PolicyType::ResultSource'; | |
| 76 | |||
| 77 | #pod =attr applies_to | ||
| 78 | #pod | ||
| 79 | #pod This policy applies to L<ResultSource|DBIx::Class::ResultSource>s. | ||
| 80 | #pod | ||
| 81 | #pod =cut | ||
| 82 | |||
| 83 | 2 | 1; | |
| 84 | |||
| 85 | __END__ | ||