lib/HTML/FormFu/Role/FormAndBlockMethods.pm

filter: Statementstmt 26.8% 26 / 97 Branchbran 7.1% 3 / 42 Conditioncond 8.3% 2 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 43.7% 7 / 16 Podpod 0.0% 0 / 2 totaltotal 19.9% 39 / 196 CC 34 SCAR 47.1
CC
34
Cov
19%
_add_constraints
272.0
_add_constraint_decimal
90.0
constraints_from_dbic
42.0
CRAP 111.6
↑ Summary lib/HTML/FormFu/Role/NestedHashUtils.pm »
1 419 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::FormAndBlockMethods;
4 # ABSTRACT: role for form and block methods
5 419 $HTML::FormFu::Role::FormAndBlockMethods::VERSION = '2.09';
6 419 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 419 ▶use HTML::FormFu::Util qw( _merge_hashes );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 419 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 419 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2449 ▶sub default_args {
Subroutine default_args: called CC=5 SCAR=16.1
Pod undocumented
13 2449 my ( $self, $defaults ) = @_;
14  
15 2449 ▶ $self->{default_args} ||= {};
Condition100%2/2
$self->{"default_args"} ||= {}
l!l
1777672
MC/DC100%1/1
$self->{"default_args"} ||= {}
$self->{"default_args"}
Truth table100%2/2
A
A= $self->{"default_args"}
Aresult
11
00
16  
17 2449 ▶ if ($defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults) 111 2338 2449
18  
19 111 my @valid_types = qw(
20 elements deflators
21 filters constraints
22 inflators validators
23 transformers output_processors
24 );
25  
26 111 for my $type ( keys %$defaults ) {
27 croak "not a valid type for default_args: '$type'"
28 131 ▶ if none { $type eq $_ } @valid_types;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $type eq $_; } , @valid_types) 0 131 131
29 }
30  
31 $self->{default_args}
32 111 = _merge_hashes( $self->{default_args}, $defaults );
33 }
34  
35 2449 return $self->{default_args};
36 }
37  
38 0 ▶sub constraints_from_dbic {
Subroutine constraints_from_dbic: not called CC=6 SCAR=37.4
Pod undocumented
39 0 my ( $self, $source, $map ) = @_;
40  
41 0 ▶ if ( 2 == @_ && 'ARRAY' eq ref $source ) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 == @_ and "ARRAY" eq ref $source) 0 0 0
Condition0%0/3
2 == @_ and "ARRAY" eq ref $source
!ll&&!rl&&r
000
MC/DC0%0/2
2 == @_ and "ARRAY" eq ref $source
2 == @_ "ARRAY" eq ref $source
Truth table0%0/3
A and B
A= 2 == @_B= "ARRAY" eq ref $source
ABresult
0X0
100
111
42 0 ( $source, $map ) = @$source;
43 }
44  
45 0 ▶ $map ||= {};
Condition0%0/2
$map ||= {}
l!l
00
MC/DC0%0/1
$map ||= {}
$map
Truth table0%0/2
A
A= $map
Aresult
11
00
46  
47 0 $source = _result_source($source);
48  
49 0 for my $col ( $source->columns ) {
50 0 _add_constraints( $self, $col, $source->column_info($col) );
51 }
52  
53 0 for my $col ( keys %$map ) {
54 0 my $source = _result_source( $map->{$col} );
55  
56 0 _add_constraints( $self, $col, $source->column_info($col) );
57 }
58  
59 0 return $self;
60 }
61  
62 0 ▶sub _result_source {
Subroutine _result_source: not called CC=2 SCAR=17.9
63 0 my ($source) = @_;
64  
65 0 ▶ if ( blessed $source ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($source)) 0 0 0
66 0 $source = $source->result_source;
67 }
68  
69 0 return $source;
70 }
71  
72 0 ▶sub _add_constraints {
Subroutine _add_constraints: not called CC=16 SCAR=56.1
73 0 my ( $self, $col, $info ) = @_;
74  
75 # We need to ensure we're only using this Block's children,
76 # as far as 'nested_name' is concerned.
77 # But we can't use get_elements() in case the fields are in sub-Blocks
78 # that don't have 'nested_name' set.
79  
80 0 my $parent = $self;
81 0 my @parent_names;
82  
83 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent and do { do { my $nested_name = $parent->nested_name; if (defined $nested_name and length $nested_name) { push @parent_names, $nested_name; } } } 0 0 0
84 0 my $nested_name = $parent->nested_name;
85 0 ▶ if ( defined $nested_name && length $nested_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nested_name and length $nested_name) 0 0 0
Condition0%0/3
defined $nested_name and length $nested_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $nested_name and length $nested_name
defined $nested_name length $nested_name
Truth table0%0/3
A and B
A= defined $nested_nameB= length $nested_name
ABresult
0X0
100
111
86 0 push @parent_names, $nested_name;
87 }
88 } while ( $parent = $parent->parent );
89  
90 0 my $fields = $self->get_fields($col);
91 0 ▶ return if !@$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @$fields 0 0 0
92  
93 0 ▶ if (@parent_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parent_names) { } 0 0 0
94 0 my $pre = join ".", reverse @parent_names;
95 0 @$fields = grep { $_->nested_name eq "$pre." . $_->name } @$fields;
96 }
97 else {
98 0 @$fields = grep { $_->nested_name eq $_->name } @$fields;
99 }
100  
101 0 ▶ return if !@$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @$fields 0 0 0
102  
103 0 ▶ return if !defined $info->{data_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"data_type"} 0 0 0
104  
105 0 my $type = lc $info->{data_type};
106  
107 0 ▶ if ( $type =~ /(char|text|binary)\z/ && defined $info->{size} ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type =~ /(char|text|binary)\z/ and defined $info->{'size'}) { } 0 0 0
elsif ($type =~ /int/) { } 0 0 0
elsif ($type =~ /enum|set/ and defined $info->{'extra'}{'list'}) { } 0 0 0
elsif ($type =~ /bool/) { } 0 0 0
elsif ($type =~ /decimal/) { } 0 0 0
Condition0%0/3
$type =~ /(char|text|binary)\z/ and defined $info->{"size"}
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ /(char|text|binary)\z/ and defined $info->{"size"}
$type =~ /(char|text|binary)\z/ defined $info->{"size"}
Truth table0%0/3
A and B
A= $type =~ /(char|text|binary)\z/B= defined $info->{"size"}
ABresult
0X0
100
111
108  
109 # char, varchar, *text, binary, varbinary
110 0 _add_constraint_max_length( $self, $fields, $info );
111 }
112 elsif ( $type =~ /int/ ) {
113 0 _add_constraint_integer( $self, $fields, $info );
114  
115 0 ▶ if ( $info->{extra}{unsigned} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"extra"}{"unsigned"}) 0 0 0
116 0 _add_constraint_unsigned( $self, $fields, $info );
117 }
118 }
119 ▶ elsif ( $type =~ /enum|set/ && defined $info->{extra}{list} ) {
Condition0%0/3
$type =~ /enum|set/ and defined $info->{"extra"}{"list"}
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ /enum|set/ and defined $info->{"extra"}{"list"}
$type =~ /enum|set/ defined $info->{"extra"}{"list"}
Truth table0%0/3
A and B
A= $type =~ /enum|set/B= defined $info->{"extra"}{"list"}
ABresult
0X0
100
111
120 0 _add_constraint_set( $self, $fields, $info );
121 }
122 elsif ( $type =~ /bool/ ) {
123 0 _add_constraint_bool( $self, $fields, $info );
124 }
125 elsif ( $type =~ /decimal/ ) {
126 0 _add_constraint_decimal( $self, $fields, $info );
127 }
128 }
129  
130 0 ▶sub _add_constraint_max_length {
Subroutine _add_constraint_max_length: not called CC=1 SCAR=6.9
131 0 my ( $self, $fields, $info ) = @_;
132  
133 0 map { $_->constraint( { type => 'MaxLength', max => $info->{size}, } ) }
134 @$fields;
135 }
136  
137 0 ▶sub _add_constraint_integer {
Subroutine _add_constraint_integer: not called CC=1 SCAR=6.9
138 0 my ( $self, $fields ) = @_;
139  
140 0 map { $_->constraint( { type => 'Integer', } ) } @$fields;
141 }
142  
143 0 ▶sub _add_constraint_unsigned {
Subroutine _add_constraint_unsigned: not called CC=1 SCAR=6.9
144 0 my ( $self, $fields ) = @_;
145  
146 0 map { $_->constraint( { type => 'Range', min => 0, } ) } @$fields;
147 }
148  
149 0 ▶sub _add_constraint_set {
Subroutine _add_constraint_set: not called CC=1 SCAR=6.9
150 0 my ( $self, $fields, $info ) = @_;
151  
152 0 map { $_->constraint( { type => 'Set', set => $info->{extra}{list}, } ) }
153 @$fields;
154 }
155  
156 0 ▶sub _add_constraint_bool {
Subroutine _add_constraint_bool: not called CC=1 SCAR=6.9
157 0 my ( $self, $fields ) = @_;
158  
159 0 map { $_->constraint( { type => 'Set', set => [ 0, 1 ] } ) } @$fields;
160 }
161  
162 0 ▶sub _add_constraint_decimal {
Subroutine _add_constraint_decimal: not called CC=9 SCAR=45.0
163 0 my ( $self, $fields, $info ) = @_;
164  
165 0 my $size = $info->{size};
166 0 my $regex;
167  
168 0 ▶ if ( defined $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 0 0
169 0 ▶ if ( 'ARRAY' eq ref $size && 2 == @$size ) {
Branch: 4 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $size and 2 == @$size) { } 0 0 0
elsif ('ARRAY' eq ref $size and 1 == @$size) { } 0 0 0
elsif (0 == $size) { } 0 0 0
elsif ($size =~ /^[0-9]+\z/) { } 0 0 0
Condition0%0/3
"ARRAY" eq ref $size and 2 == @$size
!ll&&!rl&&r
000
MC/DC0%0/2
"ARRAY" eq ref $size and 2 == @$size
"ARRAY" eq ref $size 2 == @$size
Truth table0%0/3
A and B
A= "ARRAY" eq ref $sizeB= 2 == @$size
ABresult
0X0
100
111
170 0 my ( $i, $j ) = @$size;
171 0 $i -= $j;
172 0 $regex = qr/^ [0-9]{0,$i} (?: \. [0-9]{0,$j} )? \z/x;
173 }
174 ▶ elsif ( 'ARRAY' eq ref $size && 1 == @$size ) {
Condition0%0/3
"ARRAY" eq ref $size and 1 == @$size
!ll&&!rl&&r
000
MC/DC0%0/2
"ARRAY" eq ref $size and 1 == @$size
"ARRAY" eq ref $size 1 == @$size
Truth table0%0/3
A and B
A= "ARRAY" eq ref $sizeB= 1 == @$size
ABresult
0X0
100
111
175 0 my ($i) = @$size;
176 0 $regex = qr/^ [0-9]{0,$i} \z/x;
177 }
178 elsif ( 0 == $size ) {
179 0 $regex = qr/^ [0-9]+ \z/x;
180 }
181 elsif ( $size =~ /^[0-9]+\z/ ) {
182 0 $regex = qr/^ [0-9]{0,$size} \z/x;
183 }
184 }
185  
186 0 ▶ $regex ||= qr/^ [0-9]+ (?: \. [0-9]+ )? \z/x;
Condition0%0/2
$regex ||= qr/^ [0-9]+ (?: \. [0-9]+ )? \z/x
l!l
00
MC/DC0%0/1
$regex ||= qr/^ [0-9]+ (?: \. [0-9]+ )? \z/x
$regex
Truth table0%0/2
A
A= $regex
Aresult
11
00
187  
188 0 map { $_->constraint( { type => 'Regex', regex => $regex } ) } @$fields;
189 }
190  
191 419 1;
192  
193 __END__
↑ Summary lib/HTML/FormFu/Role/NestedHashUtils.pm »