File Coverage

blib/lib/App/DBCritic/Policy.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
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 consumed by all L
20             #pod policy plugins.
21             #pod
22             #pod =cut
23              
24 5     5   3182 use strict;
  5         14  
  5         150  
25 5     5   27 use utf8;
  5         12  
  5         36  
26 5     5   126 use Modern::Perl '2011'; ## no critic (Modules::ProhibitUseQuotedVersion)
  5         11  
  5         39  
27              
28             our $VERSION = '0.023'; # VERSION
29 5     5   910 use English '-no_match_vars';
  5         10  
  5         41  
30 5     5   2049 use Moo::Role;
  5         9  
  5         34  
31 5     5   4558 use App::DBCritic::Violation;
  5         34  
  5         281  
32 5     5   50 use namespace::autoclean -also => qr{\A _}xms;
  5         11  
  5         51  
33              
34             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 objects
48             #pod indicating what part(s) of the schema the policy is interested in.
49             #pod
50             #pod =cut
51              
52             around violates => sub {
53             my ( $orig, $self ) = splice @_, 0, 2;
54             $self->_set_element(shift);
55             $self->_set_schema(shift);
56              
57             my $details = $self->$orig(@_);
58             return $self->violation($details) if $details;
59              
60             return;
61             };
62              
63             #pod =method violates
64             #pod
65             #pod Required method. Role consumers must implement a C 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 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
78             #pod object if it doesn't.
79             #pod
80             #pod =back
81             #pod
82             #pod =cut
83              
84             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.
90             #pod
91             #pod =cut
92              
93             sub violation {
94 4     4 1 13 my $self = shift;
95             return App::DBCritic::Violation->new(
96             details => shift,
97 4         14 map { $_ => $self->$_ } qw(description explanation element),
  12         75  
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
105             #pod object from the current policy.
106             #pod
107             #pod =cut
108              
109             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.
115             #pod
116             #pod =cut
117              
118             1;
119              
120             __END__