lib/HTML/FormFu/ObjectUtil.pm

filter: Statementstmt 93.3% 127 / 136 Branchbran 76.9% 40 / 52 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 10 totaltotal 82.5% 199 / 241 CC 36 SCAR 18.1
CC
36
Cov
88%
_load_config
14.1
get_parent
7.0
nested_names
6.0
CRAP 6.1
« lib/HTML/FormFu/Role/Element/SingleValueField.pm ↑ Summary lib/HTML/FormFu/Deflator/CompoundDateTime.pm »
1 525 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::ObjectUtil;
4 # ABSTRACT: utilities for dealing with FormFu objects
5 525 $HTML::FormFu::ObjectUtil::VERSION = '2.09';
6 525 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 525 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 525 ▶use HTML::FormFu::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 _parse_args require_class
12 _get_elements
13 _filter_components _merge_hashes
14 525 );
15 525 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 525 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 525 ▶use Data::Visitor::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 525 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 525 ▶use Scalar::Util qw( refaddr weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 525 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 525 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 parent
32 nested_name nested_names
33 remove_element
34 _string_equals _object_equals
35 _load_file
36 ),
37 );
38  
39 153 ▶sub load_config_file {
Subroutine load_config_file: called CC=1 SCAR=0.0
Pod undocumented
40 153 my ( $self, @files ) = @_;
41  
42 153 my $use_stems = 0;
43  
44 153 return _load_config( $self, $use_stems, @files );
45 }
46  
47 1 ▶sub load_config_filestem {
Subroutine load_config_filestem: called CC=1 SCAR=0.0
Pod undocumented
48 1 my ( $self, @files ) = @_;
49  
50 1 my $use_stems = 1;
51  
52 1 return _load_config( $self, $use_stems, @files );
53 }
54  
55 154 ▶sub _load_config {
Subroutine _load_config: called CC=14 SCAR=26.5
56 154 my ( $self, $use_stems, @filenames ) = @_;
57  
58 154 ▶ if ( scalar @filenames == 1 && ref $filenames[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @filenames == 1 and ref $filenames[0] eq "ARRAY") 1 153 154
Condition66%2/3
scalar @filenames == 1 and ref $filenames[0] eq "ARRAY"
!ll&&!rl&&r
01531
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
59 1 @filenames = @{ $filenames[0] };
60 }
61  
62 154 my $config_callback = $self->config_callback;
63 154 my $data_visitor;
64  
65 154 ▶ if ( defined $config_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_callback) 2 152 154
66 2 $data_visitor = Data::Visitor::Callback->new( %$config_callback,
67 ignore_return_values => 1, );
68 }
69  
70 154 ▶ my $config_any_arg = $use_stems ? 'stems' : 'files';
Branch: 1 branch
BranchTrueFalseTotal
$use_stems ? : 1 153 154
71 154 ▶ my $config_any_method = $use_stems ? 'load_stems' : 'load_files';
Branch: 1 branch
BranchTrueFalseTotal
$use_stems ? : 1 153 154
72  
73 154 my @config_file_path;
74  
75 154 ▶ if ( my $config_file_path = $self->config_file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_file_path = $self->config_file_path) { } 4 150 154
76  
77 4 ▶ if ( ref $config_file_path eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_file_path eq 'ARRAY') { } 3 1 4
78 3 push @config_file_path, @$config_file_path;
79 }
80 else {
81 1 push @config_file_path, $config_file_path;
82 }
83 }
84 else {
85 150 push @config_file_path, File::Spec->curdir;
86 }
87  
88 154 for my $file (@filenames) {
89 155 my $loaded = 0;
90 155 my $fullpath;
91  
92 155 foreach my $config_file_path (@config_file_path) {
93  
94 162 ▶ if ( defined $config_file_path
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file_path and not 'File::Spec'->file_name_is_absolute($file)) { } 162 0 162
Condition33%1/3
defined $config_file_path and not "File::Spec"->file_name_is_absolute($file)
!ll&&!rl&&r
00162
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
95 && !File::Spec->file_name_is_absolute($file) )
96 {
97 162 $fullpath = File::Spec->catfile( $config_file_path, $file );
98 }
99 else {
100 0 $fullpath = $file;
101 }
102  
103 162 my $config = Config::Any->$config_any_method(
104 { $config_any_arg => [$fullpath],
105 use_ext => 1,
106 driver_args => { General => { -UTF8 => 1 }, },
107 } );
108  
109 162 ▶ next if !@$config;
Branch: 1 branch
BranchTrueFalseTotal
unless @$config 8 154 162
110  
111 154 $loaded = 1;
112 154 my ( $filename, $filedata ) = %{ $config->[0] };
113  
114 154 _load_file( $self, $data_visitor, $filedata );
115 154 last;
116 }
117 155 ▶ croak "config file '$file' not found" if !$loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 1 154 155
118 }
119  
120 153 return $self;
121 }
122  
123 154 ▶sub _load_file {
Subroutine _load_file: called CC=4 SCAR=13.9
124 154 my ( $self, $data_visitor, $data ) = @_;
125  
126 154 ▶ if ( defined $data_visitor ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data_visitor) 2 152 154
127 2 $data_visitor->visit($data);
128 }
129  
130 154 ▶ for my $config ( ref $data eq 'ARRAY' ? @$data : $data ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 1 153 154
131 155 $self->populate( Clone::clone($config) );
132 }
133  
134 154 return;
135 }
136  
137 33770 ▶sub form {
Subroutine form: called CC=2 SCAR=6.9
Pod undocumented
138 33770 my ($self) = @_;
139  
140 # micro optimization! this method's called a lot, so access
141 # parent hashkey directly, instead of calling parent()
142 33770 ▶ while ( defined( my $parent = $self->{parent} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $self->{"parent"})) 58109 33770 91879
143 58109 $self = $parent;
144 }
145  
146 33770 return $self;
147 }
148  
149 2 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.1
Pod undocumented
150 2 my ($self) = @_;
151  
152 2 my %new = %$self;
153  
154 2 $new{_elements} = [ map { $_->clone } @{ $self->_elements } ];
155 2 $new{attributes} = Clone::clone( $self->attributes );
156 2 $new{tt_args} = Clone::clone( $self->tt_args );
157 2 $new{model_config} = Clone::clone( $self->model_config );
158  
159 2 ▶ if ( $self->can('_plugins') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_plugins")) 2 0 2
160 0 $new{_plugins} = [ map { $_->clone } @{ $self->_plugins } ];
161 }
162  
163 $new{languages}
164 2 ▶ = ref $self->languages
Branch: 1 branch
BranchTrueFalseTotal
ref $self->languages ? : 2 0 2
165 ? Clone::clone( $self->languages )
166 : $self->languages;
167  
168 2 $new{default_args} = $self->default_args;
169  
170 2 my $obj = bless \%new, ref $self;
171  
172 2 map { $_->parent($obj) } @{ $new{_elements} };
173  
174 2 return $obj;
175 }
176  
177 111 ▶sub name {
Subroutine name: called CC=2 SCAR=7.1
Pod undocumented
178 111 my $self = shift;
179  
180 111 ▶ croak 'cannot use name() as a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 111 111
181  
182 111 return $self->parent->name;
183 }
184  
185 1231 ▶sub nested_name {
Subroutine nested_name: called CC=2 SCAR=7.1
Pod undocumented
186 1231 my $self = shift;
187  
188 1231 ▶ croak 'cannot use nested_name() as a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1231 1231
189  
190 1231 return $self->parent->nested_name;
191 }
192  
193 0 ▶sub nested_names {
Subroutine nested_names: not called CC=2 SCAR=17.9
Pod undocumented
194 0 my $self = shift;
195  
196 0 ▶ croak 'cannot use nested_names() as a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
197  
198 0 return $self->parent->nested_names;
199 }
200  
201 5979 ▶sub stash {
Subroutine stash: called CC=5 SCAR=16.1
Pod undocumented
202 5979 my $self = shift;
203  
204 5979 ▶ $self->{stash} = {} if not exists $self->{stash};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"stash"} 2032 3947 5979
205 5979 ▶ return $self->{stash} if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1839 4140 5979
206  
207 4140 ▶ my %attrs = ( @_ == 1 ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4140 0 4140
208  
209 4140 $self->{stash}->{$_} = $attrs{$_} for keys %attrs;
210  
211 4140 return $self;
212 }
213  
214 17614 ▶sub parent {
Subroutine parent: called CC=2 SCAR=6.9
Pod undocumented
215 17614 my $self = shift;
216  
217 17614 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3277 14337 17614
218 3277 $self->{parent} = shift;
219  
220 3277 weaken( $self->{parent} );
221  
222 3277 return $self;
223 }
224  
225 14337 return $self->{parent};
226 }
227  
228 70 ▶sub get_parent {
Subroutine get_parent: called CC=7 SCAR=19.5
Pod undocumented
229 70 my $self = shift;
230  
231 70 ▶ return $self->parent
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 69 70
232 if !@_;
233  
234 69 my %args = _parse_args(@_);
235  
236 69 ▶ while ( defined( my $parent = $self->parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $self->parent)) 140 2 142
237  
238 140 for my $name ( keys %args ) {
239 140 my $value;
240  
241 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
242 && defined( $value = $parent->$name )
243 && $value eq $args{$name} )
244 {
245 67 return $parent;
246 }
247 }
248  
249 73 $self = $parent;
250 }
251  
252 2 return;
253 }
254  
255 0 ▶sub _string_equals {
Subroutine _string_equals: not called CC=2 SCAR=17.9
256 0 my ( $a, $b ) = @_;
257  
258 0 ▶ return blessed($b)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($b) ? : 0 0 0
259 ? ( refaddr($a) eq refaddr($b) )
260 : ( "$a" eq "$b" );
261 }
262  
263 0 ▶sub _object_equals {
Subroutine _object_equals: not called CC=2 SCAR=17.9
264 0 my ( $a, $b ) = @_;
265  
266 0 ▶ return blessed($b)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($b) ? : 0 0 0
267 ? ( refaddr($a) eq refaddr($b) )
268 : undef;
269 }
270  
271 525 1;
272  
273 __END__
« lib/HTML/FormFu/Role/Element/SingleValueField.pm ↑ Summary lib/HTML/FormFu/Deflator/CompoundDateTime.pm »