File Coverage

blib/lib/Apache/Voodoo/View/JSON.pm
Criterion Covered Total %
statement 16 74 21.6
branch 2 26 7.6
condition 0 3 0.0
subroutine 5 11 45.4
pod 0 5 0.0
total 23 119 19.3


line stmt bran cond sub pod time code
1             package Apache::Voodoo::View::JSON;
2              
3             $VERSION = "3.0200";
4              
5 2     2   1121 use strict;
  2         3  
  2         72  
6 2     2   12 use warnings;
  2         3  
  2         60  
7              
8 2     2   1873 use JSON::DWIW;
  2         204246  
  2         22  
9              
10 2     2   383 use base("Apache::Voodoo::View");
  2         5  
  2         2423  
11              
12             sub init {
13 3     3 0 5 my $self = shift;
14 3         5 my $config = shift;
15              
16 3         22 $self->content_type('application/javascript');
17              
18 3 100       46 $self->{json} = JSON::DWIW->new({
19             'bad_char_policy' => 'convert',
20             'pretty' => ($config->{dynamic_loading})?1:0
21             });
22             }
23              
24             sub params {
25 0     0 0   my $self = shift;
26              
27 0 0         if (defined($_[0])) {
28 0           $self->{data} = shift;
29             }
30             }
31              
32             sub exception {
33 0     0 0   my $self = shift;
34 0           my $e = shift;
35              
36 0           my $d;
37              
38 0 0         if ($e->isa("Exception::Class::DBI")) {
    0          
39 0           $d = {
40             "description" => "Database Error",
41             "message" => $e->errstr,
42             "package" => $e->package,
43             "line" => $e->line,
44             "query" => $self->_format_query($e->statement)
45             };
46             }
47             elsif ($e->isa("Apache::Voodoo::Exception::RunTime")) {
48 0           $d = {
49             "description" => $e->description,
50             "message" => $e->error,
51             "stack" => $self->_stack_trace($e->trace())
52             };
53             }
54             else {
55 0           $d = {
56             "description" => ref($e),
57             "message" => "$e"
58             };
59             }
60              
61 0           $d->{'success'} = 0;
62 0           $d->{'error'} = 1;
63              
64 0           $self->{data} = $d;
65             }
66              
67             sub output {
68 0     0 0   my $self = shift;
69              
70 0           return scalar($self->{json}->to_json($self->{data}));
71             }
72              
73             sub finish {
74 0     0 0   my $self = shift;
75              
76 0           $self->{data} = {};
77             }
78              
79             sub _stack_trace {
80 0     0     my $self = shift;
81 0           my $trace = shift;
82              
83 0 0         unless (ref($trace) eq "Devel::StackTrace") {
84 0           return [];
85             }
86              
87 0           my @trace;
88 0           my $i = 1;
89 0           while (my $frame = $trace->frame($i++)) {
90 0 0         last if ($frame->package =~ /^Apache::Voodoo::Engine/);
91 0 0         next if ($frame->package =~ /^Apache::Voodoo/);
92 0 0         next if ($frame->package =~ /(eval)/);
93              
94 0           my $f = {
95             'class' => $frame->package,
96             'function' => $trace->frame($i)->subroutine,
97             'file' => $frame->filename,
98             'line' => $frame->line,
99             };
100 0           $f->{'function'} =~ s/^$f->{'class'}:://;
101              
102 0           my @a = $trace->frame($i)->args;
103             # if the first item is a reference to same class, then this was a method call
104 0 0         if (ref($a[0]) eq $f->{'class'}) {
105 0           shift @a;
106 0           $f->{'type'} = '->';
107             }
108             else {
109 0           $f->{'type'} = '::';
110             }
111 0           $f->{'args'} = \@a;
112              
113 0           push(@trace,$f);
114              
115             }
116 0           return \@trace;
117             }
118              
119             sub _format_query {
120 0     0     my $self = shift;
121 0           my $query = shift;
122              
123 0           my $leading = undef;
124 0           my @lines;
125 0           foreach my $line (split(/\n/,$query)) {
126 0           $line =~ s/[\r\n]//g;
127 0           $line =~ s/(?
128              
129 0 0         if (!defined($leading)) {
130 0 0         next if $line =~ /^\s*$/;
131 0           my $l = $line;
132 0           $l =~ s/\S.*$//;
133 0 0         if (length($l)) {
134 0           $leading = length($l);
135             }
136             }
137             else {
138 0           my $l = $line;
139 0           $l =~ s/\S.*$//;
140 0 0 0       if (length($l) and length($l) < $leading) {
141 0           $leading = length($l);
142             }
143             }
144 0           push (@lines,$line);
145             }
146              
147 0           return join(
148             "\n",
149             map {
150 0           $_ =~ s/^ {$leading}//;
151 0           $_;
152             } @lines
153             );
154             }
155              
156             1;
157              
158             ################################################################################
159             # Copyright (c) 2005-2010 Steven Edwards (maverick@smurfbane.org).
160             # All rights reserved.
161             #
162             # You may use and distribute Apache::Voodoo under the terms described in the
163             # LICENSE file include in this package. The summary is it's a legalese version
164             # of the Artistic License :)
165             #
166             ################################################################################