JSON/_ClassInfo.pm

filter: Statementstmt 80.8% 55 / 68 Branchbran 31.2% 5 / 16 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 2 totaltotal 67.6% 71 / 105 CC 12 SCAR 16.8
CC
12
Cov
69%
HANDLE_ENCODE_ERROR
12.0
__ANON__
11.1
ACCEPT_CONTEXT
3.1
CRAP 5.4
« JSON.pm ↑ Summary
1 package Catalyst::View::Base::JSON::_ClassInfo;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $DEFAULT_JSON_CLASS = 'JSON::MaybeXS';
8 1 our $DEFAULT_CONTENT_TYPE = 'application/json';
9 1 our %JSON_INIT_ARGS = (
10 utf8 => 1,
11 convert_blessed => 1);
12  
13 1 has [qw/_original_args _instance_class _fields/] => (is=>'ro', required=>1);
14  
15 has json => (
16 is=>'ro',
17 required=>1,
18 init_arg=>undef,
19 lazy=>1,
20 1 ▶ default=>sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
21 1 my $self = shift;
22 1 ▶ eval "use ${\$self->json_class}; 1" ||
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use ${\$self->json_class;}; 1" or die "Can't use ${\$self->json_class;}, $@" 1 0 1
23 0 die "Can't use ${\$self->json_class}, $@";
24  
25 1 return $self->json_class->new(
26 $self->json_init_args);
27 1 });
28  
29 1 has content_type => (
30 is=>'ro',
31 required=>1,
32 default=>$DEFAULT_CONTENT_TYPE);
33  
34 1 has returns_status => (
35 is=>'ro',
36 predicate=>'has_returns_status');
37  
38 0 ▶sub HANDLE_ENCODE_ERROR {
Subroutine HANDLE_ENCODE_ERROR: not called CC=3 SCAR=24.8
Pod undocumented
39 0 my ($self, $view, $unencodable_ref, $err) = @_;
40 0 ▶ if($self->has_handle_encode_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_handle_encode_error) { } 0 0 0
41 0 ▶ $view->ctx->log->error($@) if $view->ctx->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $view->ctx->debug 0 0 0
42 0 $self->has_handle_encode_error->($view, $unencodable_ref, $err);
43 } else {
44 0 die $err;
45 }
46 }
47  
48 1 has handle_encode_error => (
49 is=>'ro',
50 predicate=>'has_handle_encode_error');
51  
52 has json_class => (
53 is=>'ro',
54 require=>1,
55 1 ▶ default=>sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 return $DEFAULT_JSON_CLASS;
57 1 });
58  
59 has json_init_args => (
60 is=>'ro',
61 required=>1,
62 lazy=>1,
63 1 ▶ default=>sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
64 1 my $self = shift;
65 my %init = (%JSON_INIT_ARGS, $self->has_json_extra_init_args ?
66 0 ▶ %{$self->json_extra_init_args} : ());
Branch: 1 branch
BranchTrueFalseTotal
$self->has_json_extra_init_args ? : 0 1 1
67 1 return \%init;
68 1 });
69  
70 1 has json_extra_init_args => (
71 is=>'ro',
72 predicate=>'has_json_extra_init_args');
73  
74 1 has callback_param => ( is=>'ro', predicate=>'has_callback_param');
75  
76 1 ▶my $get_stash_key = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
77 1 my $self = shift;
78 1 ▶ my $key = Scalar::Util::blessed($self) ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 1 0 1
79 Scalar::Util::refaddr($self) :
80 $self;
81 1 return "__Pure_${key}";
82 1 };
83  
84 1 ▶my $prepare_args = sub {
Subroutine __ANON__: called CC=5 SCAR=24.1
85 1 my ($self, @args) = @_;
86 1 my %args = ();
87 1 ▶ if(scalar(@args) % 2) { # odd args means first one is an object.
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@args) % 2) 0 1 1
88 0 my $proto = shift @args;
89 0 ▶ foreach my $field (@{$self->_fields||[]}) {
Condition0%0/2
$self->_fields || []
l!l
00
MC/DC0%0/1
$self->_fields || []
$self->_fields
Truth table0%0/2
A
A= $self->_fields
Aresult
11
00
90 0 ▶ if(my $cb = $proto->can($field)) { # match object methods to available fields
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $proto->can($field)) 0 0 0
91 0 $args{$field} = $proto->$field;
92 }
93 }
94 }
95 1 %args = (%args, @args);
96 1 return Catalyst::Utils::merge_hashes($self->_original_args, \%args);
97 1 };
98  
99 1 ▶sub ACCEPT_CONTEXT {
Subroutine ACCEPT_CONTEXT: called CC=3 SCAR=11.3
Pod undocumented
100 1 my ($self, $c, @args) = @_;
101 0 ▶ die "View ${\$self->_instance_class->catalyst_component_name} can only be called with a context"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($c) 0 1 1
102 unless Scalar::Util::blessed($c);
103  
104 1 my $stash_key = $self->$get_stash_key;
105 1 ▶ $c->stash->{$stash_key} ||= do {
Condition33%1/3
$c->stash->{$stash_key} ||= do { my $args = $self->$prepare_args(@args); my $new = $self->_instance_class->new(%{$args;}, %{$c->stash;}); $new->{"__class_info"} = $self; $new->{"__ctx"} = $c; $new }
l!l&&r!l&&!r
010
MC/DC0%0/2
$c->stash->{$stash_key} ||= do { my $args = $self->$prepare_args(@args); my $new = $self->_instance_class->new(%{$args;}, %{$c->stash;}); $new->{"__class_info"} = $self; $new->{"__ctx"} = $c; $new }
$c->stash->{$stash_key} do { my $args = $self->$prepare_args(@args); my $new = $self->_instance_class->new(%{$args;}, %{$c->stash;}); $new->{"__class_info"} = $self; $new->{"__ctx"} = $c; $new }
Truth table33%1/3
A ||= B
A= $c->stash->{$stash_key}B= do { my $args = $self->$prepare_args(@args); my $new = $self->_instance_class->new(%{$args;}, %{$c->stash;}); $new->{"__class_info"} = $self; $new->{"__ctx"} = $c; $new }
ABresult
1X1
011
000
106 1 my $args = $self->$prepare_args(@args);
107 my $new = $self->_instance_class->new(
108 1 %{$args},
109 1 %{$c->stash},
110 );
111 1 $new->{__class_info} = $self;
112 1 $new->{__ctx} = $c;
113 1 $new;
114 };
115 1 return $c->stash->{$stash_key};
116 }
117  
118 1 1;
119  
120 =head1 NAME
121  
122 Catalyst::View::Base::JSON::_ClassInfo - Application Level Info for your View
123  
124 =head1 SYNOPSIS
125  
126 NA - Internal use only.
127  
128 =head1 DESCRIPTION
129  
130 This is used by the main class L<Catalyst::View::JSON::PerRequest> to hold
131 application level information, mostly configuration and a few computations you
132 would rather do once.
133  
134 No real public reusably bits here, just for your inspection.
135  
136 =head1 ATTRIBUTES
137  
138 This View defines the following attributes that can be set during configuration
139  
140 =head2 content_type
141  
142 Sets the response content type. Defaults to 'application/json'.
143  
144 =head2 returns_status
145  
146 An optional arrayref of L<HTTP::Status> codes that the view is allowed to generate.
147 Setting this will injection helper methods into your view:
148  
149 $view->http_ok;
150 $view->202;
151  
152 Both 'friendly' names and numeric codes are generated (I recommend you stick with one
153 style or the other in a project to avoid confusion. Helper methods return the view
154 object to make common chained calls easier:
155  
156 $view->http_bad_request->detach;
157  
158 =head2 callback_param
159  
160 Optional. If set, we use this to get a method name for JSONP from the query parameters.
161  
162 For example if 'callback_param' is 'callback' and the request is:
163  
164 localhost/foo/bar?callback=mymethod
165  
166 Then the JSON response will be wrapped in a function call similar to:
167  
168 mymethod({
169 'foo': 'bar',
170 'baz': 'bin});
171  
172 Which is a common technique for overcoming some cross-domain restrictions of
173 XMLHttpRequest.
174  
175 There are some restrictions to the value of the callback method, for security.
176 For more see: L<http://ajaxian.com/archives/jsonp-json-with-padding>
177  
178 =head2 json_class
179  
180 The class used to perform JSON encoding. Default is L<JSON::MaybeXS>
181  
182 =head2 json_init_args
183  
184 Arguments used to initialize the L</json_class>. Defaults to:
185  
186 our %JSON_INIT_ARGS = (
187 utf8 => 1,
188 convert_blessed => 1);
189  
190 =head2 json_extra_init_args
191  
192 Allows you to 'tack on' some arguments to the JSON initialization without
193 messing with the defaults. Unless you really need to override the defaults
194 this is the method you should use.
195  
196 =head2 handle_encode_error
197  
198 A reference to a subroutine that is called when there is a failure to encode
199 the data given into a JSON format. This can be used globally as an attribute
200 on the defined configuration for the view, and you can set it or overide the
201 global settings on a context basis.
202  
203 Setting this optional attribute will capture and handle error conditions. We
204 will NOT bubble the error up to the global L<Catalyst> error handling (we don't
205 set $c->error for example). If you want that you need to set it yourself in
206 a custom handler, or don't define one.
207  
208 The subroutine receives three arguments: the view object, the original reference
209 that failed to encode and the exception. You must setup a new, valid response.
210 For example:
211  
212 package MyApp::View::JSON;
213  
214 use Moo;
215 extends 'Catalyst::View::Base::JSON';
216  
217 package MyApp;
218  
219 use Catalyst;
220  
221 MyApp->config(
222 default_view =>'JSON',
223 'View::JSON' => {
224 handle_encode_error => sub {
225 my ($view, $original_bad_ref, $err) = @_;
226 $view->response(400, { error => "$err"})->detach;
227 },
228 },
229 );
230  
231 MyApp->setup;
232  
233  
234 B<NOTE> If you mess up the return value (you return something that can't be
235 encoded) a second exception will occur which will NOT be handled and will then
236 bubble up to the main application.
237  
238 B<NOTE> We define a rational default for this to get you started:
239  
240 sub HANDLE_ENCODE_ERROR {
241 my ($view, $orginal_bad_ref, $err) = @_;
242 $view->response(400, { error => "$err"})->detach;
243 }
244  
245 =head1 SEE ALSO
246  
247 L<Catalyst>, L<Catalyst::View>, L<Catalyst::View::JSON>,
248 L<JSON::MaybeXS>
249  
250 =head1 AUTHOR
251
252 See L<Catalyst::View::Base::JSON>
253  
254 =head1 COPYRIGHT & LICENSE
255
256 See L<Catalyst::View::Base::JSON>
257  
258 =cut
« JSON.pm ↑ Summary