Renderer/JavaScript/Anon.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Renderer/JavaScript.pm ↑ Summary Renderer/UrlEncoded.pm »
1 package Any::Renderer::JavaScript::Anon;
2  
3 # $Id: Anon.pm,v 1.9 2006/08/21 08:30:24 johna Exp $
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use vars qw($VERSION %Formats);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Data::JavaScript::Anon;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 $VERSION = sprintf"%d.%03d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/;
10 %Formats = map {$_ => 1} @{available_formats()};
11  
12 sub new
13 {
14 my ( $class, $format, $options ) = @_;
15 die("The format '$format' isn't supported") unless($Formats{$format});
16  
17 my $self = {
18 'format' => $format,
19 'options' => $options,
20 };
21  
22 bless $self, $class;
23  
24 return $self;
25 }
26  
27 sub render
28 {
29 my ( $self, $data ) = @_;
30  
31 if ($self->{'format'} eq 'JSON') {
32 TRACE ( "Rendering to JSON" );
33 DUMP ( $data );
34  
35 return Data::JavaScript::Anon->anon_dump ( $data );
36 } else {
37 TRACE ( "Rendering to Data::JavaScript::Anon" );
38 DUMP ( $data );
39  
40 my $variable_name = $self->{ 'options' }->{ 'VariableName' } || 'script_output';
41 return Data::JavaScript::Anon->var_dump ( $variable_name, $data );
42 }
43 }
44  
45 sub requires_template
46 {
47 return 0;
48 }
49  
50 sub available_formats
51 {
52 return [ "JavaScript::Anon", "Javascript::Anon", "JSON" ];
53 }
54  
55 sub TRACE {}
56 sub DUMP {}
57  
58 1;
59  
60 =head1 NAME
61  
62 Any::Renderer::JavaScript::Anon - renders anonymous JavaScript data structure
63  
64 =head1 SYNOPSIS
65  
66 use Any::Renderer;
67  
68 my %options = ( 'VariableName' => 'myvariable' );
69 my $format = "JavaScript::Anon";
70 my $r = new Any::Renderer ( $format, \%options );
71  
72 my $data_structure = [...]; # arbitrary structure code
73 my $string = $r->render ( $data_structure );
74  
75 You can get a list of all formats that this module handles using the following syntax:
76  
77 my $list_ref = Any::Renderer::JavaScript::Anon::available_formats ();
78  
79 Also, determine whether or not a format requires a template with requires_template:
80  
81 my $bool = Any::Renderer::JavaScript::Anon::requires_template ( $format );
82  
83 =head1 DESCRIPTION
84  
85 Any::Renderer::JavaScript::Anon renders any Perl data structure passed to it
86 as a JavaScript anonymous data structure.
87  
88 =head1 FORMATS
89  
90 =over 4
91  
92 =item JavaScript::Anon (aka Javascript::Anon)
93  
94 A more compact equivalent to the JavaScript format, using anonymous structures in the assignment.
95  
96 perl -MAny::Renderer -e "print Any::Renderer->new('Javascript::Anon')->render({a => 1, b => [2,3]})"
97  
98 results in:
99  
100 var script_output = { a: 1, b: [ 2, 3 ] };
101  
102 =item JSON
103  
104 Use the format 'JSON' to return completely anonymous data structures - i.e. with no leading
105 "var script_output = " and no trailing ";"
106  
107 perl -MAny::Renderer -e "print Any::Renderer->new('JSON')->render({a => 1, b => [2,3]})"
108
109 results in:
110  
111 { a: 1, b: [ 2, 3 ] }
112  
113 =back
114  
115 =head1 METHODS
116  
117 =over 4
118  
119 =item $r = new Any::Renderer::JavaScript::Anon($format,\%options)
120  
121 See L</FORMATS> for a description of valid values for C<$format>.
122 See L</OPTIONS> for a description of valid C<%options>.
123  
124 =item $scalar = $r->render($data_structure)
125  
126 The main method.
127  
128 =item $bool = Any::Renderer::JavaScript::Anon::requires_template($format)
129  
130 False in this case.
131  
132 =item $list_ref = Any::Renderer::JavaScript::Anon::available_formats()
133  
134 See L</FORMATS> for a list.
135  
136 =back
137  
138 =head1 OPTIONS
139  
140 =over 4
141  
142 =item VariableName
143  
144 Name of the javascript variable that the new data structure is to be assigned to. Defaults to C<script_output>.
145 Does not apply when using C<JSON> format.
146  
147 =back
148  
149 =head1 SEE ALSO
150  
151 L<Data::JavaScript::Anon>, L<Any::Renderer>
152  
153 =head1 VERSION
154  
155 $Revision: 1.9 $ on $Date: 2006/08/21 08:30:24 $ by $Author: johna $
156  
157 =head1 AUTHOR
158  
159 Matt Wilson E<lt>matthew.wilson@bbc.co.ukE<gt>
160  
161 =head1 COPYRIGHT
162  
163 (c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
164  
165 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
166  
167 =cut
« Renderer/JavaScript.pm ↑ Summary Renderer/UrlEncoded.pm »