JSON.pm

filter: Statementstmt 90.6% 58 / 64 Branchbran 91.6% 11 / 12 Conditioncond 69.2% 9 / 13 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 85.7% 12 / 14 Podpod 40.0% 2 / 5 totaltotal 82.6% 95 / 115 CC 13 SCAR 12.6
CC
13
Cov
88%
wrap_data
6.0
deflator
5.3
build_render_method
4.0
CRAP 3.5
« JSONmulti.pm ↑ Summary JavaScript.pm »
1 package HTML::FormHandlerX::Field::JSON;
2 # ABSTRACT: a script tag which sets a var using JSON C<data>, encoded from perl data supplied via field for L<HTML::FormHandler>.
3 2 $HTML::FormHandlerX::Field::JSON::VERSION = '0.003';
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 extends 'HTML::FormHandler::Field::NoValue';
7 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JavaScript::Minifier::XS qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 has 'data_key' => ( is => 'rw', isa => 'Str', builder => 'build_data_key', lazy => 1 );
13 3 ▶sub build_data_key { HTML::FormHandler::Field::convert_full_name( shift->full_name ) }
Subroutine build_data_key: called CC=1 SCAR=0.0
Pod undocumented
14  
15 2 has 'data' => (
16 is => 'rw',
17 # isa => 'Str',
18 builder => 'build_data',
19 lazy => 1
20 );
21 0 ▶sub build_data { '' }
Subroutine build_data: not called CC=1 SCAR=6.9
Pod undocumented
22 2 has 'set_data' => ( isa => 'Str', is => 'ro' );
23  
24 2 has 'do_minify' => ( isa => 'Bool', is => 'rw', default => 0 );
25 2 has '+do_label' => ( default => 0 );
26  
27 2 has 'render_method' => (
28 traits => ['Code'],
29 is => 'ro',
30 isa => 'CodeRef',
31 lazy => 1,
32 predicate => 'does_render_method',
33 handles => { 'render' => 'execute_method' },
34 builder => 'build_render_method',
35 );
36  
37 sub build_render_method {
38 8 ▶ my $self = shift;
Subroutine build_render_method: called CC=4 SCAR=13.9
Pod undocumented
39  
40 8 my $set_data = $self->set_data;
41 8 ▶ $set_data ||= "data_" . HTML::FormHandler::Field::convert_full_name( $self->full_name );
Condition100%2/2
$set_data ||= 'data_' . HTML::FormHandler::Field::convert_full_name($self->full_name)
l!l
44
MC/DC100%1/1
$set_data ||= 'data_' . HTML::FormHandler::Field::convert_full_name($self->full_name)
$set_data
Truth table100%2/2
A
A= $set_data
Aresult
11
00
42 5 ▶ return sub { my $self = shift; $self->wrap_data( $self->form->$set_data($self) ); }
Subroutine __ANON__: called CC=1 SCAR=0.0
43 8 ▶ if ( $self->form && $self->form->can($set_data) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->form and $self->form->can($set_data) 5 3 8
Condition66%2/3
$self->form and $self->form->can($set_data)
!ll&&!rl&&r
035
MC/DC50%1/2
$self->form and $self->form->can($set_data)
$self->form $self->form->can($set_data)
Truth table66%2/3
A and B
A= $self->formB= $self->form->can($set_data)
ABresult
0X0
100
111
44 return sub {
45 5 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
46 5 return $self->wrap_data( $self->data );
47 3 };
48 } ## end sub build_render_method
49  
50 sub _result_from_object {
51 0 ▶ my ( $self, $result, $value ) = @_;
Subroutine _result_from_object: not called CC=1 SCAR=6.9
52 0 $self->_set_result($result);
53 0 $self->value($value);
54 0 $result->_set_field_def($self);
55 0 return $result;
56 }
57  
58 sub wrap_data {
59 10 ▶ my $self = shift;
Subroutine wrap_data: called CC=6 SCAR=17.9
Pod documented
60  
61 10 ▶ my $json = $self->deflator( @_ > 1 ? [@_] : $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 10 10
62 10 chomp $json;
63  
64 10 my $data_key = $self->data_key;
65  
66 10 my $javascript = '';
67 10 ▶ if ( $data_key =~ m/.+\..+/ ) { # key contains 'dot' properties, so don't create a var, just set property
Branch: 3 branches
BranchTrueFalseTotal
if ($data_key =~ /.+\..+/) { } 1 9 10
elsif ($data_key =~ /.+\.$/) { } 1 8 9
elsif ($data_key =~ /^\..+/) { } 1 7 8
68 1 $javascript .= qq{\n $data_key = $json;};
69 } elsif ( $data_key =~ m/.+\.$/ ) { # key ends with 'dot', so assume data_key is object and field_name is property, don't create a var, just set property
70 1 my $property_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
71 1 $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 1 my $object_key = HTML::FormHandler::Field::convert_full_name( $self->full_name );
75 1 $javascript .= qq{\n $object_key$data_key = $json;};
76 } else {
77 7 $javascript .= qq{\n var $data_key = $json;};
78 }
79  
80 10 my $output = qq{\n<script type="text/javascript">};
81 10 ▶ $output .= $self->do_minify ? JavaScript::Minifier::XS::minify($javascript) : $javascript;
Branch: 1 branch
BranchTrueFalseTotal
$self->do_minify ? : 6 4 10
82 10 $output .= qq{\n</script>};
83  
84 10 return $output;
85 } ## end sub wrap_data
86  
87  
88 has "json_opts" => (
89 is => "rw",
90 traits => ['Hash'],
91 isa => "HashRef",
92 7 ▶ default => sub { { pretty => undef, relaxed => undef, canonical => undef } },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 2 handles => {
94 set_json_opt => 'set',
95 get_json_opt => 'get',
96 },
97 );
98  
99 sub deflator {
100 10 ▶ my ( $self, $value ) = @_;
Subroutine deflator: called CC=5 SCAR=16.7
Pod documented
101 10 ▶ my $pretty = $self->get_json_opt('pretty') // 1;
Condition100%2/2
$self->get_json_opt("pretty") // 1
l!l
19
MC/DC100%1/1
$self->get_json_opt("pretty") // 1
$self->get_json_opt("pretty")
Truth table100%2/2
A
A= $self->get_json_opt("pretty")
Aresult
11
00
102 10 ▶ my $relaxed = $self->get_json_opt('relaxed') // 1;
Condition50%1/2
$self->get_json_opt("relaxed") // 1
l!l
010
MC/DC0%0/1
$self->get_json_opt("relaxed") // 1
$self->get_json_opt("relaxed")
Truth table50%1/2
A
A= $self->get_json_opt("relaxed")
Aresult
11
00
103 10 ▶ my $canonical = $self->get_json_opt('canonical') // 1;
Condition50%1/2
$self->get_json_opt("canonical") // 1
l!l
010
MC/DC0%0/1
$self->get_json_opt("canonical") // 1
$self->get_json_opt("canonical")
Truth table50%1/2
A
A= $self->get_json_opt("canonical")
Aresult
11
00
104 10 ▶ return JSON->new
Condition50%1/2
&JSON()->new->utf8->allow_nonref->pretty($pretty)->relaxed($relaxed)->canonical($canonical)->encode($value) || ''
l!l
100
MC/DC0%0/1
&JSON()->new->utf8->allow_nonref->pretty($pretty)->relaxed($relaxed)->canonical($canonical)->encode($value) || ''
&JSON()->new->utf8->allow_nonref->pretty($pretty)->relaxed($relaxed)->canonical($canonical)->encode($value)
Truth table50%1/2
A
A= &JSON()->new->utf8->allow_nonref->pretty($pretty)->relaxed($relaxed)->canonical($canonical)->encode($value)
Aresult
11
00
105 ->utf8
106 ->allow_nonref
107 ->pretty($pretty)
108 ->relaxed($relaxed)
109 ->canonical($canonical)
110 ->encode($value)
111 || '';
112 } ## end sub deflator
113  
114  
115 2 __PACKAGE__->meta->make_immutable;
116 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 2 1;
118  
119 __END__
120  
121 =pod
122  
123 =encoding UTF-8
124  
125 =head1 NAME
126  
127 HTML::FormHandlerX::Field::JSON - a script tag which sets a var using JSON C<data>, encoded from perl data supplied via field for L<HTML::FormHandler>.
128  
129 =head1 VERSION
130  
131 version 0.003
132  
133 =head1 SYNOPSIS
134  
135 This class can be used for fields that need to supply JSON data for use
136 by scripts in the form. It will JSONify and render the value returned by
137 a form's C<data_E<lt>field_nameE<gt>> method, or the field's C<data> attribute.
138  
139 has_field 'user_addresses' => ( type => 'JSON',
140 data => { john => 'john@example.com', sarah => 'sarah@example.com' } );
141  
142 or using a method:
143  
144 has_field 'user_addresses' => ( type => 'JSON' );
145 sub data_user_addresses {
146 my ( $self, $field ) = @_;
147 if( $field->value == 'foo' ) {
148 return { john => 'john@example.com', sarah => 'sarah@example.com' };
149 } else {
150 return [ 'john@example.com', 'sarah@example.com' ];
151 }
152 }
153 #----
154 has_field 'usernames' => ( type => 'JSON' );
155 sub data_usernames {
156 my ( $self, $field ) = @_;
157 return [ qw'john sarah' ];
158 }
159  
160 or set the name of the data generation method:
161  
162 has_field 'user_addresses' => ( type => 'JSON', set_data => 'my_user_addresses' );
163 sub my_user_addresses {
164 ....
165 }
166  
167 or provide a 'render_method':
168  
169 has_field 'user_addresses' => ( type => 'JSON', render_method => \&render_user_addresses );
170 sub render_user_addresses {
171 my $self = shift;
172 ....
173 return q(
174 <script type="text/javascript">
175 // JSON assignment here
176 var myVar = 'abc';
177 </script>);
178 }
179  
180 The data generation methods should return a scalar (hashref or
181 arrayref), which will be encoded as JSON, given a variable assignment,
182 and wrapped in script tags. If you supply your own 'render_method' then
183 you are responsible for calling C<$self-E<gt>deflator> or
184 C<$self-E<gt>wrap_data> yourself.
185  
186 =head1 FIELD OPTIONS
187  
188 We support the following additional field options, over what is
189 inherited from L<HTML::FormHandler::Field>
190  
191 =over
192  
193 =item data
194  
195 Scalar (hashref or arrayref) holding the data to be encoded as JSON.
196  
197 =item set_data
198  
199 Name of method that gets called to generate the data.
200  
201 =item data_key
202  
203 Name of JavaScript variable that will be assigned the JSON object. See
204 L</"JavaScript variable names">
205  
206 =item do_minify
207  
208 Boolean to indicate whether code should be minified using
209 L<JavaScript::Minifier::XS>
210  
211 =item json_opts
212  
213 Hashref with 3 possible keys; C<pretty>, C<relaxed>, C<canonical>. The
214 values are passed to L<JSON> when encoding the data.
215  
216 =back
217  
218 =head1 FIELD METHODS
219  
220 The following methods can be called on the field.
221  
222 =over
223  
224 =item deflator
225  
226 The C<deflator> method is called to encode the C<data> as JSON. The
227 C<json_opts> attribute is used to control options for JSON encode.
228  
229 =item wrap_data
230  
231 The C<wrap_data> method calls C<$self-E<gt>deflator>, sets the variable
232 assignment using the JSON object, minifies the code, and wraps the code
233 in script tags.
234  
235 =back
236  
237 =head1 JavaScript variable names
238  
239 By default, the name of the variable being assigned is same as the field
240 name. The variable name can be changed with the data_key attribute. If
241 the field name (or data_key value) is a simple string (no dot separator)
242 then the variable will be defined with C<var varName;>:
243  
244 has_field 'user_addresses' => ( type => 'JSON',
245 data => [ qw'john@acme.org sarah@acme.org' ],
246 );
247  
248 will render as:
249  
250 <script type="text/javascript">
251 var user_addresses = [ "john@acme.org", "sarah@acme.org" ];
252 </script>);
253  
254 Otherwise it is assumed the variable is already defined:
255  
256 has_field 'user_addresses' => ( type => 'JSON',
257 data_key => 'user_addresses.names',
258 data => [ qw'john sarah' ],
259 );
260  
261 will render as:
262  
263 <script type="text/javascript">
264 user_addresses.names = [ "john", "sarah" ];
265 </script>);
266  
267 The data_key can begin or end with a dot, in which case the field name
268 is either appended or prepended to the data_key.
269  
270 has_field 'user_addresses' => ( type => 'JSON',
271 data_key => '.email',
272 data => [ qw'john@acme.org sarah@acme.org' ],
273 );
274  
275 Will render as:
276  
277 <script type="text/javascript">
278 user_addresses.email = [ "john@acme.org", "sarah@acme.org" ];
279 </script>);
280  
281 =head1 AUTHOR
282  
283 Charlie Garrison <garrison@zeta.org.au>
284  
285 =head1 COPYRIGHT AND LICENSE
286  
287 This software is copyright (c) 2014 by Charlie Garrison.
288  
289 This is free software; you can redistribute it and/or modify it under
290 the same terms as the Perl 5 programming language system itself.
291  
292 =cut
« JSONmulti.pm ↑ Summary JavaScript.pm »