blib/lib/Template/Plugin/JSON.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 66.6% 4 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 25.0% 1 / 4 totaltotal 81.9% 50 / 61 SCAR 19.6
CC
7
Cov
89%
BUILDARGS
4.2
_build_json_converter
3.0
__ANON__
2.0
CRAP 7.1
↑ Summary
1 #!/usr/bin/perl
2  
3 package Template::Plugin::JSON;
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends qw(Moose::Object Template::Plugin);
11  
12 1 our $VERSION = "0.06";
13  
14  
15 1 has context => (
16 isa => "Object",
17 is => "ro",
18 );
19  
20 1 has json_converter => (
21 isa => "Object",
22 is => "ro",
23 lazy_build => 1,
24 );
25  
26 has json_args => (
27 isa => "HashRef",
28 is => "ro",
29 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 1 );
31  
32 sub BUILDARGS {
33 3 ▶ my ( $class, $c, @args ) = @_;
Subroutine BUILDARGS: called CC=4 SCAR=14.3
Pod documented
34  
35 3 my $args;
36  
37 3 ▶ if ( @args == 1 and not ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and not ref $args[0]) 0 3 3
Condition66%2/3
@args == 1 and not ref $args[0]
!ll&&!rl&&r
210
MC/DC0%0/2
@args == 1 and not ref $args[0]
@args == 1 not ref $args[0]
Truth table66%2/3
A and not(B)
A= @args == 1B= not ref $args[0]
ABresult
0X0
100
111
38 0 warn "Single argument form is deprecated, this module always uses JSON/JSON::XS now";
39 }
40  
41 3 ▶ $args = ref $args[0] ? $args[0] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 1 2 3
42  
43 3 return { %$args, context => $c, json_args => $args };
44 }
45  
46 sub _build_json_converter {
47 3 ▶ my $self = shift;
Subroutine _build_json_converter: called CC=3 SCAR=11.0
48  
49 3 my $json = JSON->new->allow_nonref(1);
50  
51 3 my $args = $self->json_args;
52  
53 3 for my $method (keys %$args) {
54 1 ▶ if ( $json->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->can($method)) 1 0 1
55 1 $json->$method( $args->{$method} );
56 }
57 }
58  
59 3 return $json;
60 }
61  
62 sub json {
63 4 ▶ my ( $self, $value ) = @_;
Subroutine json: called CC=1 SCAR=0.0
Pod undocumented
64  
65 4 $self->json_converter->encode($value);
66 }
67  
68 sub json_decode {
69 1 ▶ my ( $self, $value ) = @_;
Subroutine json_decode: called CC=1 SCAR=0.0
Pod undocumented
70  
71 1 $self->json_converter->decode($value);
72 }
73  
74 sub BUILD {
75 3 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
76 3 ▶ $self->context->define_vmethod( $_ => json => sub { $self->json(@_) } ) for qw(hash list scalar);
Subroutine __ANON__: called CC=1 SCAR=0.0
77 }
78  
79 1 __PACKAGE__;
80  
81 __END__
82  
83 =pod
84  
85 =head1 NAME
86  
87 Template::Plugin::JSON - Adds a .json vmethod for all TT values.
88  
89 =head1 SYNOPSIS
90  
91 [% USE JSON ( pretty => 1 ) %];
92  
93 <script type="text/javascript">
94  
95 var foo = [% foo.json %];
96  
97 </script>
98  
99 or read in JSON
100  
101 [% USE JSON %]
102 [% data = JSON.json_decode(json) %]
103 [% data.thing %]
104  
105 =head1 DESCRIPTION
106  
107 This plugin provides a C<.json> vmethod to all value types when loaded. You
108 can also decode a json string back to a data structure.
109  
110 It will load the L<JSON> module (you probably want L<JSON::XS> installed for
111 automatic speed ups).
112  
113 Any options on the USE line are passed through to the JSON object, much like L<JSON/to_json>.
114  
115 =head1 SEE ALSO
116  
117 L<JSON>, L<Template::Plugin>
118  
119 =head1 VERSION CONTROL
120  
121 L<http://github.com/nothingmuch/template-plugin-json/>
122  
123 =head1 AUTHOR
124  
125 Yuval Kogman <nothingmuch@woobling.org>
126  
127 =head1 COPYRIGHT & LICENSE
128  
129 Copyright (c) 2006, 2008 Infinity Interactive, Yuval Kogman.
130  
131 Permission is hereby granted, free of charge, to any person obtaining a copy
132 of this software and associated documentation files (the "Software"), to deal
133 in the Software without restriction, including without limitation the rights
134 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
135 copies of the Software, and to permit persons to whom the Software is
136 furnished to do so, subject to the following conditions:
137  
138 The above copyright notice and this permission notice shall be included in
139 all copies or substantial portions of the Software.
140  
141 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
142 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
143 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
144 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
145 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
146 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
147 DEALINGS IN THE SOFTWARE.
148  
149 =cut
150  
↑ Summary