lib/HTML/FormFu/ObjectUtil.pm

filter: Statementstmt 76.0% 127 / 167 Branchbran 60.6% 40 / 66 Conditioncond 40.0% 8 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 67.7% 21 / 31 Podpod 0.0% 0 / 11 totaltotal 64.8% 199 / 307 SCAR 47.5
CC
48
Cov
69%
_add_constraints
90.0
constraints_from_dbic
20.0
_load_config
14.1
CRAP 115.5
« lib/HTML/FormFu/Role/CreateChildren.pm ↑ Summary lib/HTML/FormFu/Role/Element/SingleValueField.pm »
1 package HTML::FormFu::ObjectUtil;
2  
3 401 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 401 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 401 our $VERSION = '2.05'; # VERSION
7  
8 401 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 401 use HTML::FormFu::Util qw(
11 _parse_args require_class
12 _get_elements
13 _filter_components _merge_hashes
14 401 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 401 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 401 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 401 ▶use Data::Visitor::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 401 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 401 ▶use Scalar::Util qw( refaddr weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 401 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 401 our @EXPORT_OK = ( qw(
23 deflator
24 load_config_file load_config_filestem
25 form
26 get_parent
27 insert_before insert_after
28 clone
29 name
30 stash
31 constraints_from_dbic
32 parent
33 nested_name nested_names
34 remove_element
35 _string_equals _object_equals
36 _load_file
37 ),
38 );
39  
40 sub load_config_file {
41 140 ▶ my ( $self, @files ) = @_;
Subroutine load_config_file: called CC=1 SCAR=0.0
Pod undocumented
42  
43 140 my $use_stems = 0;
44  
45 140 return _load_config( $self, $use_stems, @files );
46 }
47  
48 sub load_config_filestem {
49 1 ▶ my ( $self, @files ) = @_;
Subroutine load_config_filestem: called CC=1 SCAR=0.0
Pod undocumented
50  
51 1 my $use_stems = 1;
52  
53 1 return _load_config( $self, $use_stems, @files );
54 }
55  
56 sub _load_config {
57 141 ▶ my ( $self, $use_stems, @filenames ) = @_;
Subroutine _load_config: called CC=14 SCAR=26.5
58  
59 141 ▶ if ( scalar @filenames == 1 && ref $filenames[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @filenames == 1 and ref $filenames[0] eq "ARRAY") 1 140 141
Condition66%2/3
scalar @filenames == 1 and ref $filenames[0] eq "ARRAY"
!ll&&!rl&&r
01401
MC/DC50%1/2
scalar @filenames == 1 and ref $filenames[0] eq "ARRAY"
scalar @filenames == 1 ref $filenames[0] eq "ARRAY"
Truth table66%2/3
A and B
A= scalar @filenames == 1B= ref $filenames[0] eq "ARRAY"
ABresult
0X0
100
111
60 1 @filenames = @{ $filenames[0] };
61 }
62  
63 141 my $config_callback = $self->config_callback;
64 141 my $data_visitor;
65  
66 141 ▶ if ( defined $config_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_callback) 2 139 141
67 2 $data_visitor = Data::Visitor::Callback->new( %$config_callback,
68 ignore_return_values => 1, );
69 }
70  
71 141 ▶ my $config_any_arg = $use_stems ? 'stems' : 'files';
Branch: 1 branch
BranchTrueFalseTotal
$use_stems ? : 1 140 141
72 141 ▶ my $config_any_method = $use_stems ? 'load_stems' : 'load_files';
Branch: 1 branch
BranchTrueFalseTotal
$use_stems ? : 1 140 141
73  
74 141 my @config_file_path;
75  
76 141 ▶ if ( my $config_file_path = $self->config_file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_file_path = $self->config_file_path) { } 4 137 141
77  
78 4 ▶ if ( ref $config_file_path eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_file_path eq 'ARRAY') { } 3 1 4
79 3 push @config_file_path, @$config_file_path;
80 }
81 else {
82 1 push @config_file_path, $config_file_path;
83 }
84 }
85 else {
86 137 push @config_file_path, File::Spec->curdir;
87 }
88  
89 141 for my $file (@filenames) {
90 142 my $loaded = 0;
91 142 my $fullpath;
92  
93 142 foreach my $config_file_path (@config_file_path) {
94  
95 149 ▶ if ( defined $config_file_path
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file_path and not 'File::Spec'->file_name_is_absolute($file)) { } 149 0 149
Condition33%1/3
defined $config_file_path and not "File::Spec"->file_name_is_absolute($file)
!ll&&!rl&&r
00149
MC/DC0%0/2
defined $config_file_path and not "File::Spec"->file_name_is_absolute($file)
defined $config_file_path not "File::Spec"->file_name_is_absolute($file)
Truth table33%1/3
A and not(B)
A= defined $config_file_pathB= not "File::Spec"->file_name_is_absolute($file)
ABresult
0X0
100
111
96 && !File::Spec->file_name_is_absolute($file) )
97 {
98 149 $fullpath = File::Spec->catfile( $config_file_path, $file );
99 }
100 else {
101 0 $fullpath = $file;
102 }
103  
104 149 my $config = Config::Any->$config_any_method( {
105 $config_any_arg => [$fullpath],
106 use_ext => 1,
107 driver_args => { General => { -UTF8 => 1 }, },
108 } );
109  
110 149 ▶ next if !@$config;
Branch: 1 branch
BranchTrueFalseTotal
unless @$config 8 141 149
111  
112 141 $loaded = 1;
113 141 my ( $filename, $filedata ) = %{ $config->[0] };
114  
115 141 _load_file( $self, $data_visitor, $filedata );
116 141 last;
117 }
118 142 ▶ croak "config file '$file' not found" if !$loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 1 141 142
119 }
120  
121 140 return $self;
122 }
123  
124 sub _load_file {
125 141 ▶ my ( $self, $data_visitor, $data ) = @_;
Subroutine _load_file: called CC=4 SCAR=13.9
126  
127 141 ▶ if ( defined $data_visitor ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data_visitor) 2 139 141
128 2 $data_visitor->visit($data);
129 }
130  
131 141 ▶ for my $config ( ref $data eq 'ARRAY' ? @$data : $data ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 1 140 141
132 142 $self->populate( Clone::clone($config) );
133 }
134  
135 141 return;
136 }
137  
138 sub form {
139 28986 ▶ my ($self) = @_;
Subroutine form: called CC=2 SCAR=6.9
Pod undocumented
140  
141 # micro optimization! this method's called a lot, so access
142 # parent hashkey directly, instead of calling parent()
143 28986 ▶ while ( defined( my $parent = $self->{parent} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $self->{"parent"})) 50083 28986 79069
144 50083 $self = $parent;
145 }
146  
147 28986 return $self;
148 }
149  
150 sub clone {
151 2 ▶ my ($self) = @_;
Subroutine clone: called CC=3 SCAR=11.1
Pod undocumented
152  
153 2 my %new = %$self;
154  
155 2 $new{_elements} = [ map { $_->clone } @{ $self->_elements } ];
156 2 $new{attributes} = Clone::clone( $self->attributes );
157 2 $new{tt_args} = Clone::clone( $self->tt_args );
158 2 $new{model_config} = Clone::clone( $self->model_config );
159  
160 2 ▶ if ( $self->can('_plugins') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_plugins")) 2 0 2
161 2 $new{_plugins} = [ map { $_->clone } @{ $self->_plugins } ];
162 }
163  
164 $new{languages}
165 2 ▶ = ref $self->languages
Branch: 1 branch
BranchTrueFalseTotal
ref $self->languages ? : 2 0 2
166 ? Clone::clone( $self->languages )
167 : $self->languages;
168  
169 2 $new{default_args} = $self->default_args;
170  
171 2 my $obj = bless \%new, ref $self;
172  
173 2 map { $_->parent($obj) } @{ $new{_elements} };
174  
175 2 return $obj;
176 }
177  
178 sub name {
179 111 ▶ my $self = shift;
Subroutine name: called CC=2 SCAR=7.1
Pod undocumented
180  
181 111 ▶ croak 'cannot use name() as a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 111 111
182  
183 111 return $self->parent->name;
184 }
185  
186 sub nested_name {
187 1097 ▶ my $self = shift;
Subroutine nested_name: called CC=2 SCAR=7.1
Pod undocumented
188  
189 1097 ▶ croak 'cannot use nested_name() as a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1097 1097
190  
191 1097 return $self->parent->nested_name;
192 }
193  
194 sub nested_names {
195 0 ▶ my $self = shift;
Subroutine nested_names: not called CC=2 SCAR=17.9
Pod undocumented
196  
197 0 ▶ croak 'cannot use nested_names() as a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
198  
199 0 return $self->parent->nested_names;
200 }
201  
202 sub stash {
203 5097 ▶ my $self = shift;
Subroutine stash: called CC=5 SCAR=16.1
Pod undocumented
204  
205 5097 ▶ $self->{stash} = {} if not exists $self->{stash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"stash"} 1602 3495 5097
206 5097 ▶ return $self->{stash} if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1819 3278 5097
207  
208 3278 ▶ my %attrs = ( @_ == 1 ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3278 0 3278
209  
210 3278 $self->{stash}->{$_} = $attrs{$_} for keys %attrs;
211  
212 3278 return $self;
213 }
214  
215 sub constraints_from_dbic {
216 0 ▶ my ( $self, $source, $map ) = @_;
Subroutine constraints_from_dbic: not called CC=4 SCAR=30.0
Pod undocumented
217  
218 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
219  
220 0 $source = _result_source($source);
221  
222 0 for my $col ( $source->columns ) {
223 0 _add_constraints( $self, $col, $source->column_info($col) );
224 }
225  
226 0 for my $col ( keys %$map ) {
227 0 my $source = _result_source( $map->{$col} );
228  
229 0 _add_constraints( $self, $col, $source->column_info($col) );
230 }
231  
232 0 return $self;
233 }
234  
235 sub _result_source {
236 0 ▶ my ($source) = @_;
Subroutine _result_source: not called CC=2 SCAR=17.9
237  
238 0 ▶ if ( blessed $source ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($source)) 0 0 0
239 0 $source = $source->result_source;
240 }
241  
242 0 return $source;
243 }
244  
245 sub _add_constraints {
246 0 ▶ my ( $self, $col, $info ) = @_;
Subroutine _add_constraints: not called CC=9 SCAR=45.0
247  
248 0 ▶ return if !defined $self->get_field($col);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_field($col) 0 0 0
249  
250 0 ▶ return if !defined $info->{data_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"data_type"} 0 0 0
251  
252 0 my $type = lc $info->{data_type};
253  
254 0 ▶ if ( $type =~ /(char|text|binary)\z/ && defined $info->{size} ) {
Branch: 3 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
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
255  
256 # char, varchar, *text, binary, varbinary
257 0 _add_constraint_max_length( $self, $col, $info );
258 }
259 elsif ( $type =~ /int/ ) {
260 0 _add_constraint_integer( $self, $col, $info );
261  
262 0 ▶ if ( $info->{extra}{unsigned} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"extra"}{"unsigned"}) 0 0 0
263 0 _add_constraint_unsigned( $self, $col, $info );
264 }
265  
266 }
267 ▶ 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
268 0 _add_constraint_set( $self, $col, $info );
269 }
270 }
271  
272 sub _add_constraint_max_length {
273 0 ▶ my ( $self, $col, $info ) = @_;
Subroutine _add_constraint_max_length: not called CC=1 SCAR=6.9
274  
275 $self->constraint( {
276 type => 'MaxLength',
277 name => $col,
278 max => $info->{size},
279 0 } );
280 }
281  
282 sub _add_constraint_integer {
283 0 ▶ my ( $self, $col, $info ) = @_;
Subroutine _add_constraint_integer: not called CC=1 SCAR=6.9
284  
285 0 $self->constraint( {
286 type => 'Integer',
287 name => $col,
288 } );
289 }
290  
291 sub _add_constraint_unsigned {
292 0 ▶ my ( $self, $col, $info ) = @_;
Subroutine _add_constraint_unsigned: not called CC=1 SCAR=6.9
293  
294 0 $self->constraint( {
295 type => 'Range',
296 name => $col,
297 min => 0,
298 } );
299 }
300  
301 sub _add_constraint_set {
302 0 ▶ my ( $self, $col, $info ) = @_;
Subroutine _add_constraint_set: not called CC=1 SCAR=6.9
303  
304 $self->constraint( {
305 type => 'Set',
306 name => $col,
307 set => $info->{extra}{list},
308 0 } );
309 }
310  
311 sub parent {
312 16237 ▶ my $self = shift;
Subroutine parent: called CC=2 SCAR=6.9
Pod undocumented
313  
314 16237 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2432 13805 16237
315 2432 $self->{parent} = shift;
316  
317 2432 weaken( $self->{parent} );
318  
319 2432 return $self;
320 }
321  
322 13805 return $self->{parent};
323 }
324  
325 sub get_parent {
326 70 ▶ my $self = shift;
Subroutine get_parent: called CC=7 SCAR=19.5
Pod undocumented
327  
328 70 ▶ return $self->parent
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 69 70
329 if !@_;
330  
331 69 my %args = _parse_args(@_);
332  
333 69 ▶ while ( defined( my $parent = $self->parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $self->parent)) 140 2 142
334  
335 140 for my $name ( keys %args ) {
336 140 my $value;
337  
338 140 ▶ if ( $parent->can($name)
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->can($name) and defined($value = $parent->$name) and $value eq $args{$name}) 67 73 140
Condition83%5/6
$parent->can($name) and defined($value = $parent->$name) and $value eq $args{$name}
!ll&&!rl&&r
56867
$parent->can($name) and defined($value = $parent->$name)
!ll&&!rl&&r
50135
MC/DC66%2/3
$parent->can($name) and defined($value = $parent->$name) and $value eq $args{$name}
$parent->can($name) defined($value = $parent->$name) $value eq $args{$name}
Truth table75%3/4
A and B and C
A= $parent->can($name)B= defined($value = $parent->$name)C= $value eq $args{$name}
ABCresult
0XX0
10X0
1100
1111
339 && defined( $value = $parent->$name )
340 && $value eq $args{$name} )
341 {
342 67 return $parent;
343 }
344 }
345  
346 73 $self = $parent;
347 }
348  
349 2 return;
350 }
351  
352 sub _string_equals {
353 0 ▶ my ( $a, $b ) = @_;
Subroutine _string_equals: not called CC=2 SCAR=17.9
354  
355 0 ▶ return blessed($b)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($b) ? : 0 0 0
356 ? ( refaddr($a) eq refaddr($b) )
357 : ( "$a" eq "$b" );
358 }
359  
360 sub _object_equals {
361 0 ▶ my ( $a, $b ) = @_;
Subroutine _object_equals: not called CC=2 SCAR=17.9
362  
363 0 ▶ return blessed($b)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($b) ? : 0 0 0
364 ? ( refaddr($a) eq refaddr($b) )
365 : undef;
366 }
367  
368 401 1;
« lib/HTML/FormFu/Role/CreateChildren.pm ↑ Summary lib/HTML/FormFu/Role/Element/SingleValueField.pm »