« blib/lib/App/DBCritic/Policy/NoPrimaryKey.pm
↑ Summary
blib/lib/App/DBCritic/Policy/NullableTextColumn.pm »
| 1 | package App::DBCritic::Policy; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Role for criticizing database schemas | ||||||||||
| 4 | |||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||
| 6 | #pod | ||||||||||
| 7 | #pod package App::DBCritic::Policy::MyPolicy; | ||||||||||
| 8 | #pod use Moo; | ||||||||||
| 9 | #pod | ||||||||||
| 10 | #pod has description => ( default => sub{'Follow my policy'} ); | ||||||||||
| 11 | #pod has explanation => ( default => {'My way or the highway'} ); | ||||||||||
| 12 | #pod has applies_to => ( default => sub { ['ResultSource'] } ); | ||||||||||
| 13 | #pod with 'App::DBCritic::Policy'; | ||||||||||
| 14 | #pod | ||||||||||
| 15 | #pod sub violates { $_[0]->element ne '' } | ||||||||||
| 16 | #pod | ||||||||||
| 17 | #pod =head1 DESCRIPTION | ||||||||||
| 18 | #pod | ||||||||||
| 19 | #pod This is a L<role|Moo::Role> consumed by all L<App::DBCritic|App::DBCritic> | ||||||||||
| 20 | #pod policy plugins. | ||||||||||
| 21 | #pod | ||||||||||
| 22 | #pod =cut | ||||||||||
| 23 | |||||||||||
| 24 | 14 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 14 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 14 | ▶ | use Modern::Perl '2011'; ## no critic (Modules::ProhibitUseQuotedVersion) | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 14 | our $VERSION = '0.023'; # VERSION | |||||||||
| 29 | 14 | ▶ | use English '-no_match_vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 14 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 14 | ▶ | use App::DBCritic::Violation; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 14 | ▶ | use namespace::autoclean -also => qr{\A _}xms; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 14 | requires qw(description explanation violates applies_to); | |||||||||
| 35 | |||||||||||
| 36 | #pod =method description | ||||||||||
| 37 | #pod | ||||||||||
| 38 | #pod Required method. Returns a short string describing what's wrong. | ||||||||||
| 39 | #pod | ||||||||||
| 40 | #pod =method explanation | ||||||||||
| 41 | #pod | ||||||||||
| 42 | #pod Required method. Returns a string giving further details. | ||||||||||
| 43 | #pod | ||||||||||
| 44 | #pod =method applies_to | ||||||||||
| 45 | #pod | ||||||||||
| 46 | #pod Required method. Returns an array reference of types of | ||||||||||
| 47 | #pod L<DBIx::Class|DBIx::Class> objects | ||||||||||
| 48 | #pod indicating what part(s) of the schema the policy is interested in. | ||||||||||
| 49 | #pod | ||||||||||
| 50 | #pod =cut | ||||||||||
| 51 | |||||||||||
| 52 | 28 | ▶ | around violates => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 53 | 28 | my ( $orig, $self ) = splice @_, 0, 2; | |||||||||
| 54 | 28 | $self->_set_element(shift); | |||||||||
| 55 | 28 | $self->_set_schema(shift); | |||||||||
| 56 | |||||||||||
| 57 | 28 | my $details = $self->$orig(@_); | |||||||||
| 58 | 28 | ▶ | return $self->violation($details) if $details; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 24 | return; | |||||||||
| 61 | 14 | }; | |||||||||
| 62 | |||||||||||
| 63 | #pod =method violates | ||||||||||
| 64 | #pod | ||||||||||
| 65 | #pod Required method. Role consumers must implement a C<violates> method that | ||||||||||
| 66 | #pod returns true if the | ||||||||||
| 67 | #pod policy is violated and false otherwise, based on attributes provided by the | ||||||||||
| 68 | #pod role. Callers should call the C<violates> method as the following: | ||||||||||
| 69 | #pod | ||||||||||
| 70 | #pod $policy->violates($element, $schema); | ||||||||||
| 71 | #pod | ||||||||||
| 72 | #pod =over | ||||||||||
| 73 | #pod | ||||||||||
| 74 | #pod =item Arguments: I<$element>, I<$schema> | ||||||||||
| 75 | #pod | ||||||||||
| 76 | #pod =item Return value: nothing if the policy passes, or a | ||||||||||
| 77 | #pod L<App::DBCritic::Violation|App::DBCritic::Violation> | ||||||||||
| 78 | #pod object if it doesn't. | ||||||||||
| 79 | #pod | ||||||||||
| 80 | #pod =back | ||||||||||
| 81 | #pod | ||||||||||
| 82 | #pod =cut | ||||||||||
| 83 | |||||||||||
| 84 | 14 | has element => ( is => 'ro', init_arg => undef, writer => '_set_element' ); | |||||||||
| 85 | |||||||||||
| 86 | #pod =attr element | ||||||||||
| 87 | #pod | ||||||||||
| 88 | #pod Read-only accessor for the current schema element being examined by | ||||||||||
| 89 | #pod L<App::DBCritic|App::DBCritic>. | ||||||||||
| 90 | #pod | ||||||||||
| 91 | #pod =cut | ||||||||||
| 92 | |||||||||||
| 93 | 4 | ▶ | sub violation { | ||||||||
|
Subroutine
violation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 94 | 4 | my $self = shift; | |||||||||
| 95 | return App::DBCritic::Violation->new( | ||||||||||
| 96 | details => shift, | ||||||||||
| 97 | 4 | map { $_ => $self->$_ } qw(description explanation element), | |||||||||
| 98 | ); | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | #pod =method violation | ||||||||||
| 102 | #pod | ||||||||||
| 103 | #pod Given a string description of a violation that has been encountered, creates a | ||||||||||
| 104 | #pod new L<App::DBCritic::Violation|App::DBCritic::Violation> | ||||||||||
| 105 | #pod object from the current policy. | ||||||||||
| 106 | #pod | ||||||||||
| 107 | #pod =cut | ||||||||||
| 108 | |||||||||||
| 109 | 14 | has schema => ( is => 'ro', writer => '_set_schema' ); | |||||||||
| 110 | |||||||||||
| 111 | #pod =attr schema | ||||||||||
| 112 | #pod | ||||||||||
| 113 | #pod Read-only accessor for the current schema object being examined by | ||||||||||
| 114 | #pod L<App::DBCritic|App::DBCritic>. | ||||||||||
| 115 | #pod | ||||||||||
| 116 | #pod =cut | ||||||||||
| 117 | |||||||||||
| 118 | 14 | 1; | |||||||||
| 119 | |||||||||||
| 120 | __END__ | ||||||||||