File Coverage

blib/lib/HTML/FormHandlerX/Field/JSONmulti.pm
Criterion Covered Total %
statement 12 57 21.0
branch 0 16 0.0
condition n/a
subroutine 4 5 80.0
pod 0 1 0.0
total 16 79 20.2


line stmt bran cond sub pod time code
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             $HTML::FormHandlerX::Field::JSONmulti::VERSION = '0.001'; # TRIAL
4              
5 1     1   1302 use Moose;
  1         2  
  1         6  
6             extends 'HTML::FormHandlerX::Field::JSON';
7 1     1   4247 use namespace::autoclean;
  1         1  
  1         10  
8              
9 1     1   58 use JavaScript::Minifier::XS qw();
  1         1  
  1         441  
10              
11             sub wrap_data {
12 0     0 0   my $self = shift;
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 0         if ( @data_args > 1 ) {
23 0           my $idx = 0;
24 0 0         if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property, and assume property is an array
    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 0         if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property
    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 0         $output .= $self->do_minify ? JavaScript::Minifier::XS::minify($javascript) : $javascript;
84 0           $output .= qq{\n</script>};
85              
86 0           return $output;
87             } ## end sub wrap_data
88              
89              
90              
91             __PACKAGE__->meta->make_immutable;
92 1     1   4 use namespace::autoclean;
  1         2  
  1         3  
93             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.001
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