blib/lib/Dancer2/Plugin/Ajax.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 90.3% 47 / 52 CC 7 SCAR 11.3
CC
7
Cov
93%
ajax
5.0
__ANON__
3.0
__ANON__
2.0
CRAP 3.1
↑ Summary
1 package Dancer2::Plugin::Ajax;
2 # ABSTRACT: a plugin for adding Ajax route handlers
3 2 $Dancer2::Plugin::Ajax::VERSION = '0.400000';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Dancer2::Core::Types 'Str';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has content_type => (
10 is => 'ro',
11 isa => Str,
12 0 ▶ from_config => sub { 'text/xml' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 2 );
14  
15 2 plugin_keywords 'ajax';
16  
17 4 ▶sub ajax {
Subroutine ajax: called CC=5 SCAR=16.1
Pod undocumented
18 4 my ( $plugin, $pattern, @rest ) = @_;
19  
20 4 my @default_methods = ( 'get', 'post' );
21  
22 # If the given pattern is an ArrayRef, we override the default methods
23 4 ▶ if( ref($pattern) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "ARRAY") 1 3 4
24 1 @default_methods = @$pattern;
25 1 $pattern = shift(@rest);
26 }
27  
28 4 my $code;
29 4 ▶ for my $e (@rest) { $code = $e if ( ref($e) eq 'CODE' ) }
Branch: 1 branch
BranchTrueFalseTotal
if ref $e eq "CODE" 4 0 4
30  
31 8 ▶ my $ajax_route = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
32  
33 # # must be an XMLHttpRequest
34 8 ▶ if ( not $plugin->app->request->is_ajax ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin->app->request->is_ajax) 2 6 8
35 2 $plugin->app->pass;
36 }
37  
38 # Default response content type is either what's defined in the
39 # plugin setting or text/xml
40 6 ▶ $plugin->app->response->header('Content-Type')
Branch: 1 branch
BranchTrueFalseTotal
$plugin->app->response->header("Content-Type") or $plugin->app->response->content_type($plugin->content_type) 0 6 6
41 or $plugin->app->response->content_type( $plugin->content_type );
42  
43 # disable layout
44 6 my $layout = $plugin->app->config->{'layout'};
45 6 $plugin->app->config->{'layout'} = undef;
46 6 my $response = $code->(@_);
47 6 $plugin->app->config->{'layout'} = $layout;
48 6 return $response;
49 4 };
50  
51 4 foreach my $method ( @default_methods ) {
52 9 $plugin->app->add_route(
53 method => $method,
54 regexp => $pattern,
55 code => $ajax_route,
56 );
57 }
58 }
59  
60 2 1;
61  
62 __END__
↑ Summary