FormHandler/Render/Util.pm

filter: Statementstmt 100% 37 / 37 Branchbran 62.5% 10 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 3 totaltotal 82.5% 52 / 63 CC 11 SCAR 17.4
CC
11
Cov
87%
process_attrs
8.2
cc_widget
3.1
ucc_widget
2.0
CRAP 5.7
« FormHandler/Field/Repeatable.pm ↑ Summary FormHandler/Widget/Wrapper/Base.pm »
1 package HTML::FormHandler::Render::Util;
2 # ABSTRACT: rendering utility
3 149 $HTML::FormHandler::Render::Util::VERSION = '0.410000';
4 149 ▶use Sub::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 149 Sub::Exporter::setup_exporter({ exports => [ 'process_attrs', 'cc_widget', 'ucc_widget' ] } );
6  
7  
8 # this is a function for processing various attribute flavors
9 sub process_attrs {
10 2231 ▶ my ($attrs) = @_;
Subroutine process_attrs: called CC=8 SCAR=21.0
Pod undocumented
11  
12 2231 my @use_attrs;
13 2231 ▶ my $javascript = delete $attrs->{javascript} || '';
Condition50%1/2
delete $attrs->{'javascript'} || ''
l!l
02231
MC/DC0%0/1
delete $attrs->{'javascript'} || ''
delete $attrs->{'javascript'}
Truth table50%1/2
A
A= delete $attrs->{'javascript'}
Aresult
11
00
14 2231 for my $attr( sort keys %$attrs ) {
15 973 my $value = '';
16 973 ▶ if( defined $attrs->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs->{$attr}) 973 0 973
17 973 ▶ if( ref $attrs->{$attr} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs->{$attr} eq 'ARRAY') { } 511 462 973
18 # we don't want class="" if no classes specified
19 511 ▶ next unless scalar @{$attrs->{$attr}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$attrs->{$attr};} 0 511 511
20 511 $value = join (' ', @{$attrs->{$attr}} );
21 }
22 else {
23 462 $value = $attrs->{$attr};
24 }
25 }
26 973 push @use_attrs, sprintf( '%s="%s"', $attr, $value );
27 }
28 2231 my $output = join( ' ', @use_attrs );
29 2231 ▶ $output = " $output" if length $output;
Branch: 1 branch
BranchTrueFalseTotal
if length $output 755 1476 2231
30 2231 ▶ $output .= " $javascript" if $javascript;
Branch: 1 branch
BranchTrueFalseTotal
if $javascript 0 2231 2231
31 2231 return $output;
32 }
33  
34 sub cc_widget {
35 4 ▶ my $widget = shift;
Subroutine cc_widget: called CC=3 SCAR=11.2
Pod undocumented
36 4 ▶ return '' unless $widget;
Branch: 1 branch
BranchTrueFalseTotal
unless $widget 0 4 4
37 4 ▶ if($widget eq lc $widget) {
Branch: 1 branch
BranchTrueFalseTotal
if ($widget eq lc $widget) 4 0 4
38 4 $widget =~ s/^(\w{1})/\u$1/g;
39 4 $widget =~ s/_(\w{1})/\u$1/g;
40 }
41 4 return $widget;
42 }
43  
44 sub ucc_widget {
45 199 ▶ my $widget = shift;
Subroutine ucc_widget: called CC=2 SCAR=7.0
Pod undocumented
46 199 ▶ if($widget ne lc $widget) {
Branch: 1 branch
BranchTrueFalseTotal
if ($widget ne lc $widget) 199 0 199
47 199 $widget =~ s/::/_/g;
48 199 $widget = ucfirst($widget);
49 199 my @parts = $widget =~ /([A-Z][a-z]*)/g;
50 199 $widget = join('_', @parts);
51 199 $widget = lc($widget);
52 }
53 199 return $widget;
54 }
55  
56  
57 149 1;
58  
59 __END__
60  
61 =pod
62  
63 =encoding UTF-8
64  
65 =head1 NAME
66  
67 HTML::FormHandler::Render::Util - rendering utility
68  
69 =head1 VERSION
70  
71 version 0.410000
72  
73 =head1 SYNOPSIS
74  
75 The 'process_attrs' takes a hashref and creates an attribute string
76 for constructing HTML.
77  
78 my $attrs => {
79 some_attr => 1,
80 placeholder => 'Enter email...",
81 class => ['help', 'special'],
82 };
83 my $string = process_attrs($attrs);
84  
85 ...will produce:
86  
87 ' some_attr="1" placeholder="Enter email..." class="help special"'
88  
89 If an arrayref is empty, it will be skipped. For a hash key of 'javascript'
90 only the value will be appended (without '$key=""');
91  
92 =head1 AUTHOR
93  
94 FormHandler Contributors - see HTML::FormHandler
95  
96 =head1 COPYRIGHT AND LICENSE
97  
98 This software is copyright (c) 2017 by Gerda Shank.
99  
100 This is free software; you can redistribute it and/or modify it under
101 the same terms as the Perl 5 programming language system itself.
102  
103 =cut
« FormHandler/Field/Repeatable.pm ↑ Summary FormHandler/Widget/Wrapper/Base.pm »