Constraint/DBIC/Unique.pm

filter: Statementstmt 81.9% 68 / 83 Branchbran 50.0% 21 / 42 Conditioncond 57.1% 12 / 21 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 66.6% 112 / 168 CC 30 SCAR 39.9
CC
30
Cov
69%
constrain_value
71.1
__ANON__
4.2
BEGIN
1.0
CRAP 53.9
↑ Summary Model/DBIC.pm »
1 package HTML::FormFu::Constraint::DBIC::Unique;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = '2.03'; # VERSION
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 extends 'HTML::FormFu::Constraint';
10  
11 2 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use HTML::FormFu::Util qw( DEBUG_CONSTRAINTS debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 has model => ( is => 'rw', traits => ['Chained'] );
16 2 has resultset => ( is => 'rw', traits => ['Chained'] );
17 2 has column => ( is => 'rw', traits => ['Chained'] );
18 2 has method_name => ( is => 'rw', traits => ['Chained'] );
19 2 has self_stash_key => ( is => 'rw', traits => ['Chained'] );
20 2 has others => ( is => 'rw', traits => ['Chained'] );
21 2 has id_field => ( is => 'rw', traits => ['Chained'] );
22  
23 16 ▶sub constrain_value {
Subroutine constrain_value: called CC=27 SCAR=42.6
Pod undocumented
24 16 my ( $self, $value ) = @_;
25  
26 16 ▶ return 1 if !defined $value || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 0 16 16
Condition33%1/3
not defined $value or $value eq ""
l!l&&r!l&&!r
0016
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
27  
28 16 for (qw/ resultset /) {
29 16 ▶ if ( !defined $self->$_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->$_) 0 16 16
30 # warn and die, as errors are swallowed by HTML-FormFu
31 0 carp "'$_' is not defined";
32 0 croak "'$_' is not defined";
33 }
34 }
35  
36 # get stash
37 16 my $stash = $self->form->stash;
38  
39 16 my $schema;
40  
41 16 ▶ if ( defined $stash->{schema} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $stash->{'schema'}) { } 16 0 16
elsif (defined $stash->{'context'} and defined $self->model) { } 0 0 0
elsif (defined $stash->{'context'}) { } 0 0 0
42 16 $schema = $stash->{schema};
43 }
44 ▶ elsif ( defined $stash->{context} && defined $self->model ) {
Condition0%0/3
defined $stash->{"context"} and defined $self->model
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stash->{"context"} and defined $self->model
defined $stash->{"context"} defined $self->model
Truth table0%0/3
A and B
A= defined $stash->{"context"}B= defined $self->model
ABresult
0X0
100
111
45 0 $schema = $stash->{context}->model( $self->model );
46 }
47 elsif ( defined $stash->{context} ) {
48 0 $schema = $stash->{context}->model;
49 }
50  
51 16 ▶ if ( !defined $schema ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema) 0 16 16
52 # warn and die, as errors are swallowed by HTML-FormFu
53 0 carp 'could not find DBIC schema';
54 0 croak 'could not find DBIC schema';
55 }
56  
57 16 my $resultset = $schema->resultset( $self->resultset );
58  
59 16 ▶ if ( !defined $resultset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $resultset) 0 16 16
60 # warn and die, as errors are swallowed by HTML-FormFu
61 0 carp 'could not find DBIC resultset';
62 0 croak 'could not find DBIC resultset';
63 }
64  
65 16 ▶ if ( my $method_name = $self->method_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method_name = $self->method_name) { } 3 13 16
66 # warn "using $method_name to look for $value";
67  
68 # need to be able to tell $method_name about record on the form stash
69 3 my $pk_val;
70  
71 3 ▶ if ( defined( my $self_stash_key = $self->self_stash_key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $self_stash_key = $self->self_stash_key)) 3 0 3
72  
73 3 ▶ if ( defined( my $self_stash = $stash->{ $self_stash_key } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $self_stash = $stash->{$self_stash_key})) 3 0 3
74  
75 3 my ($pk) = $resultset->result_source->primary_columns;
76  
77 3 $pk_val = $self_stash->$pk;
78 }
79 }
80  
81 3 return $resultset->$method_name( $value, $pk_val );
82 }
83 else {
84  
85 13 ▶ my $column = $self->column || $self->parent->name;
Condition66%2/3
$self->column || $self->parent->name
l!l&&r!l&&!r
1120
MC/DC0%0/2
$self->column || $self->parent->name
$self->column $self->parent->name
Truth table66%2/3
A || B
A= $self->columnB= $self->parent->name
ABresult
1X1
011
000
86 13 my %others;
87 13 ▶ if ( $self->others ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->others) 2 11 13
88 0 ▶ my @others = ref $self->others ? @{ $self->others }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->others ? : 0 2 2
89 : $self->others;
90  
91 2 my $params = $self->form->input;
92  
93 %others =
94 grep {
95 defined && length
96 4 ▶ }
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
004
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
97 map {
98 2 $_ => $self->get_nested_hash_value( $params, $_ )
99 } @others;
100  
101 }
102  
103 13 my $existing_row = eval {
104 13 $resultset->find( { %others, $column => $value } );
105 };
106  
107 13 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 13 13
108 # warn and die, as errors are swallowed by HTML-FormFu
109 0 carp $error;
110 0 croak $error;
111 }
112  
113 # if a row exists, first check whether it matches a known object on the
114 # form stash
115  
116 13 ▶ if ( $existing_row && defined( my $self_stash_key = $self->self_stash_key ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($existing_row and defined(my $self_stash_key = $self->self_stash_key)) { } 0 13 13
elsif ($existing_row and defined(my $id_field = $self->id_field)) { } 6 11 17
Condition66%2/3
$existing_row and defined(my $self_stash_key = $self->self_stash_key)
!ll&&!rl&&r
490
MC/DC0%0/2
$existing_row and defined(my $self_stash_key = $self->self_stash_key)
$existing_row defined(my $self_stash_key = $self->self_stash_key)
Truth table66%2/3
A and B
A= $existing_rowB= defined(my $self_stash_key = $self->self_stash_key)
ABresult
0X0
100
111
117  
118 0 ▶ if ( defined( my $self_stash = $stash->{ $self_stash_key } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $self_stash = $stash->{$self_stash_key})) 0 0 0
119  
120 0 my ($pk) = $resultset->result_source->primary_columns;
121  
122 0 ▶ if ( $existing_row->$pk eq $self_stash->$pk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_row->$pk eq $self_stash->$pk) 0 0 0
123 0 return 1;
124 }
125 }
126 }
127 ▶ elsif ( $existing_row && defined (my $id_field = $self->id_field ) ) {
Condition100%3/3
$existing_row and defined(my $id_field = $self->id_field)
!ll&&!rl&&r
436
MC/DC100%2/2
$existing_row and defined(my $id_field = $self->id_field)
$existing_row defined(my $id_field = $self->id_field)
Truth table100%3/3
A and B
A= $existing_rowB= defined(my $id_field = $self->id_field)
ABresult
0X0
100
111
128 6 my $value = $self->get_nested_hash_value( $self->form->input, $id_field );
129 6 ▶ if ( defined $value && length $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and length $value) 4 2 6
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
114
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
130 4 my ($pk) = $resultset->result_source->primary_columns;
131 4 return ($existing_row->$pk eq $value);
132 }
133 }
134  
135 9 return !$existing_row;
136  
137 }
138 }
139  
140 1 ▶after repeatable_repeat => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
141 1 my ( $self, $repeatable, $new_block ) = @_;
142  
143 # rename any 'id_field' fields
144 1 ▶ if ( my $id_field = $self->id_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id_field = $self->id_field) 1 0 1
145 1 my $block_fields = $new_block->get_fields;
146  
147 1 my $field = $repeatable->get_field_with_original_name( $id_field, $block_fields );
148  
149 1 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 1 0 1
150 1 ▶ DEBUG_CONSTRAINTS && debug(
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_CONSTRAINTS() and debug(sprintf("Repeatable renaming constraint 'id_field' '%s' to '%s'", $id_field, $field->nested_name)) 0 1 1
151 sprintf "Repeatable renaming constraint 'id_field' '%s' to '%s'",
152 $id_field,
153 $field->nested_name,
154 );
155  
156 1 $self->id_field( $field->nested_name );
157 }
158 }
159 2 };
160  
161 2 1;
162  
163 __END__
↑ Summary Model/DBIC.pm »