JSONmulti.pm

filter: Statementstmt 26.2% 16 / 61 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 25.3% 21 / 83 CC 13 SCAR 49.4
CC
13
Cov
21%
wrap_data
182.0
BEGIN
1.0
BEGIN
1.0
CRAP 139.4
↑ Summary JSON.pm »
1 package HTML::FormHandlerX::Field::JSONmulti;
2 # ABSTRACT: a script tag which sets multiple vars using JSON 'data', encoded from list of perl data supplied via field for HTML::FormHandler.
3 1 $HTML::FormHandlerX::Field::JSONmulti::VERSION = '0.003';
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 extends 'HTML::FormHandlerX::Field::JSON';
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use JavaScript::Minifier::XS qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub wrap_data {
12 0 ▶ my $self = shift;
Subroutine wrap_data: not called CC=13 SCAR=52.0
Pod documented
13 0 my @data_args = @_;
14  
15 0 my $data_key = $self->data_key;
16  
17 0 my $javascript = '';
18  
19 ## This whole list bit seems a bit pointless right now, why not just create and array ref and assign the one json object?
20 ## The plan is to allow different data_key for each list element, but sensible implementation eludes me at present (IOW, $work doesn't need it)
21 ## At least we're not throwing away data if given a list; of course easier solution would have simply been to make an arrayref!!
22 0 ▶ if ( @data_args > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data_args > 1) { } 0 0 0
23 0 my $idx = 0;
24 0 ▶ if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property, and assume property is an array
Branch: 3 branches
BranchTrueFalseTotal
if ($data_key =~ /.+\..+/) { } 0 0 0
elsif ($data_key =~ /.+\.$/) { } 0 0 0
elsif ($data_key =~ /^\..+/) { } 0 0 0
25 0 $javascript .= qq{\n ${data_key} = [];};
26 0 foreach my $data (@data_args) {
27 0 my $json = $self->deflator($data);
28 0 chomp $json;
29  
30 0 $javascript .= qq{\n ${data_key}[$idx] = $json;};
31 0 $idx++;
32 }
33 } elsif ( $data_key =~ m/.+\.$/ )
34 { # key ends with 'dot', so assume data_key is object and field_name is property, don't create a var, just set property, and assume property is an array
35 0 my $property_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
36 0 $javascript .= qq{\n $data_key${property_key} = [];};
37 0 foreach my $data (@data_args) {
38 0 my $json = $self->deflator($data);
39 0 chomp $json;
40 0 $javascript .= qq{\n $data_key${property_key}[$idx] = $json;};
41 0 $idx++;
42 }
43 } elsif ( $data_key =~ m/^\..+/ )
44 { # key starts with 'dot', so assume data_key is property and field_name is object, don't create a var, just set property, and assume property is an array
45 0 my $object_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
46 0 $javascript .= qq{\n $object_key${data_key} = [];};
47 0 foreach my $data (@data_args) {
48 0 my $json = $self->deflator($data);
49 0 chomp $json;
50 0 $javascript .= qq{\n $object_key${data_key}[$idx] = $json;};
51 0 $idx++;
52 }
53 } else {
54 0 foreach my $data (@data_args) {
55 0 my $json = $self->deflator($data);
56 0 chomp $json;
57 0 $javascript .= qq{\n var ${data_key}_$idx = $json;};
58 0 $idx++;
59 }
60 } ## end else [ if ( $data_key =~ m/.+\..+/ ) ]
61  
62 } else {
63 0 my $json = $self->deflator( $data_args[0] );
64 0 chomp $json;
65  
66 0 ▶ if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property
Branch: 3 branches
BranchTrueFalseTotal
if ($data_key =~ /.+\..+/) { } 0 0 0
elsif ($data_key =~ /.+\.$/) { } 0 0 0
elsif ($data_key =~ /^\..+/) { } 0 0 0
67 0 $javascript .= qq{\n $data_key = $json;};
68 } elsif ( $data_key =~ m/.+\.$/ )
69 { # key ends with 'dot', so assume data_key is object and field_name is property, don't create a var, just set property
70 0 my $property_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
71 0 $javascript .= qq{\n $data_key$property_key = $json;};
72 } elsif ( $data_key =~ m/^\..+/ )
73 { # key starts with 'dot', so assume data_key is property and field_name is object, don't create a var, just set property, and assume property is an array
74 0 my $object_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
75 0 $javascript .= qq{\n $object_key$data_key = $json;};
76 } else {
77 0 $javascript .= qq{\n var $data_key = $json;};
78 }
79  
80 } ## end else [ if ( @data_args > 1 ) ]
81  
82 0 my $output = qq{\n<script type="text/javascript">};
83 0 ▶ $output .= $self->do_minify ? JavaScript::Minifier::XS::minify($javascript) : $javascript;
Branch: 1 branch
BranchTrueFalseTotal
$self->do_minify ? : 0 0 0
84 0 $output .= qq{\n</script>};
85  
86 0 return $output;
87 } ## end sub wrap_data
88  
89  
90  
91 1 __PACKAGE__->meta->make_immutable;
92 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 1;
94  
95 __END__
96  
97 =pod
98  
99 =encoding UTF-8
100  
101 =head1 NAME
102  
103 HTML::FormHandlerX::Field::JSONmulti - a script tag which sets multiple vars using JSON 'data', encoded from list of perl data supplied via field for HTML::FormHandler.
104  
105 =head1 VERSION
106  
107 version 0.003
108  
109 =head1 AUTHOR
110  
111 Charlie Garrison <garrison@zeta.org.au>
112  
113 =head1 COPYRIGHT AND LICENSE
114  
115 This software is copyright (c) 2014 by Charlie Garrison.
116  
117 This is free software; you can redistribute it and/or modify it under
118 the same terms as the Perl 5 programming language system itself.
119  
120 =cut
↑ Summary JSON.pm »