Schema/Component/Validation.pm

filter: Statementstmt 100% 27 / 28 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 7 Podpod 100% 3 / 3 totaltotal 100% 40 / 42 CC 3 SCAR 3.7
CC
3
Cov
100%
insert
2.0
update
2.0
BEGIN
1.0
CRAP 1.4
« Test/Role/SQLite.pm ↑ Summary Schema/Result/Media.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Interchange6::Schema::Component::Validation;
4  
5 =head1 NAME
6  
7 Interchange6::Schema::Component::Validation
8  
9 =head1 SYNOPSIS
10  
11 package My::Result;
12  
13 __PACKAGE__->load_components(qw(
14 +Interchange6::Schema::Component::Validation
15 ));
16  
17 sub validate {
18 my $self = shift;
19 my $schema = $self->result_source->schema;
20  
21 unless ( $self->some_column =~ /magic/ ) {
22 $schema->throw_exception("some_column does not contain magic");
23 }
24 }
25  
26 =head1 DESCRIPTION
27  
28 This component allows validation of row attributes to be deferred until other components in the stack have been called. For example you might want to have the TimeStamp component called before validation so that datetime columns with set_on_create are defined before validation occurs. In this case your local_components call might look like;
29  
30 __PACKAGE__->load_components(
31 qw(TimeStamp +Interchange6::Schema::Component::Validation)
32 );
33  
34 In order to fail validation the L</validation> method must throw an exception.
35  
36 =cut
37  
38 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 ▶use base 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 =head1 METHODS
44  
45 =head2 validate
46  
47 Called before insert or update action. Method should be overloaded by class which load this component. Validation failures should result in L<DBIx::Class::Schema::throw_exception|DBIx::Class::Schema/throw_exception> being called.
48  
49 =cut
50  
51 sub validate {
52 # This method should be overloaded by calling class so this should never get
53 # hit by Devel::Cover
54 # uncoverable subroutine
55 # uncoverable statement
56 0 ▶ my $self = shift;
Subroutine validate: not called CC=1 SCAR=0.0
Pod documented
57 }
58  
59 =head2 insert
60  
61 Overload insert to call L</validate> before insert is performed.
62  
63 =cut
64  
65 sub insert {
66 169 ▶ my ( $self, @args ) = @_;
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
67 169 eval{ $self->validate };
68 169 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 9 160 169
69 9 $self->result_source->schema->throw_exception($@);
70 }
71 else {
72 160 $self->next::method(@args);
73 }
74 160 return $self;
75 }
76  
77 =head2 update
78  
79 Overload update to call L</validate> before update is performed.
80  
81 =cut
82  
83 sub update {
84 3 ▶ my ( $self, @args ) = @_;
Subroutine update: called CC=2 SCAR=6.9
Pod documented
85 3 eval{ $self->validate };
86 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 2 3
87 1 $self->result_source->schema->throw_exception($@);
88 }
89 else {
90 2 $self->next::method(@args);
91 }
92 2 return $self;
93 }
94  
95 2 1;
« Test/Role/SQLite.pm ↑ Summary Schema/Result/Media.pm »