File Coverage

blib/lib/HTML/FormHandlerX/Field/JSONmulti.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


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.004';
4              
5 1     1   1805 use Moose;
  0            
  0            
6             extends 'HTML::FormHandlerX::Field::JSON';
7             use namespace::autoclean;
8              
9             use JavaScript::Minifier::XS qw();
10              
11             sub wrap_data {
12             my $self = shift;
13             my @data_args = @_;
14              
15             my $data_key = $self->data_key;
16              
17             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             if ( @data_args > 1 ) {
23             my $idx = 0;
24             if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property, and assume property is an array
25             $javascript .= qq{\n ${data_key} = [];};
26             foreach my $data (@data_args) {
27             my $json = $self->deflator($data);
28             chomp $json;
29              
30             $javascript .= qq{\n ${data_key}[$idx] = $json;};
31             $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             my $property_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
36             $javascript .= qq{\n $data_key${property_key} = [];};
37             foreach my $data (@data_args) {
38             my $json = $self->deflator($data);
39             chomp $json;
40             $javascript .= qq{\n $data_key${property_key}[$idx] = $json;};
41             $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             my $object_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
46             $javascript .= qq{\n $object_key${data_key} = [];};
47             foreach my $data (@data_args) {
48             my $json = $self->deflator($data);
49             chomp $json;
50             $javascript .= qq{\n $object_key${data_key}[$idx] = $json;};
51             $idx++;
52             }
53             } else {
54             foreach my $data (@data_args) {
55             my $json = $self->deflator($data);
56             chomp $json;
57             $javascript .= qq{\n var ${data_key}_$idx = $json;};
58             $idx++;
59             }
60             } ## end else [ if ( $data_key =~ m/.+\..+/ ) ]
61              
62             } else {
63             my $json = $self->deflator( $data_args[0] );
64             chomp $json;
65              
66             if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property
67             $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             my $property_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
71             $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             my $object_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
75             $javascript .= qq{\n $object_key$data_key = $json;};
76             } else {
77             $javascript .= qq{\n var $data_key = $json;};
78             }
79              
80             } ## end else [ if ( @data_args > 1 ) ]
81              
82             my $output = qq{\n<script type="text/javascript">};
83             $output .= $self->do_minify ? JavaScript::Minifier::XS::minify($javascript) : $javascript;
84             $output .= qq{\n</script>};
85              
86             return $output;
87             } ## end sub wrap_data
88              
89              
90              
91             __PACKAGE__->meta->make_immutable;
92             use namespace::autoclean;
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.004
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