lib/Dancer/Plugin/Ajax.pm

filter: Statementstmt 68.8% 31 / 45 Branchbran 20.0% 2 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 60.0% 6 / 10 Podpod n/a totaltotal 57.3% 39 / 68 CC 9 SCAR 20.0
CC
9
Cov
58%
__ANON__
12.0
__ANON__
12.0
ajax
10.4
CRAP 7.4
« lib/Dancer/Serializer/XML.pm ↑ Summary bin/dancer »
1 package Dancer::Plugin::Ajax;
2 3 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: a plugin for adding Ajax route handlers
4 3 $Dancer::Plugin::Ajax::VERSION = '1.3522';
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Dancer::Exception ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 register 'ajax' => \&ajax;
13  
14 0 ▶hook before => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
15 0 ▶ if (request->is_ajax) {
Branch: 1 branch
BranchTrueFalseTotal
if (request()->is_ajax) 0 0 0
16 0 ▶ content_type( plugin_setting->{content_type} || 'text/xml' );
Condition0%0/2
plugin_setting()->{'content_type'} || 'text/xml'
l!l
00
MC/DC0%0/1
plugin_setting()->{'content_type'} || 'text/xml'
plugin_setting()->{'content_type'}
Truth table0%0/2
A
A= plugin_setting()->{'content_type'}
Aresult
11
00
17 }
18 3 };
19  
20 1 ▶sub ajax {
Subroutine ajax: called CC=5 SCAR=23.4
21 1 my ($pattern, @rest) = @_;
22  
23 1 my $code;
24 1 ▶ for my $e (@rest) { $code = $e if (ref($e) eq 'CODE') }
Branch: 1 branch
BranchTrueFalseTotal
if ref $e eq "CODE" 1 0 1
25  
26 0 ▶ my $ajax_route = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
27 # must be an XMLHttpRequest
28 0 ▶ if (not request->is_ajax) {
Branch: 1 branch
BranchTrueFalseTotal
unless (request()->is_ajax) 0 0 0
29 0 ▶ pass and return 0;
Branch: 1 branch
BranchTrueFalseTotal
pass() and return 0 0 0 0
30 }
31  
32 # disable layout
33 0 my $layout = setting('layout');
34 0 setting('layout' => undef);
35 0 ▶ my $response = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 $code->();
37 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 my $e = $_;
39 0 setting('layout' => $layout);
40 0 die $e;
41 0 };
42 0 setting('layout' => $layout);
43 0 return $response;
44 1 };
45  
46 # rebuild the @rest array with the compiled route handler
47 1 my @compiled_rest;
48 1 for my $e (@rest) {
49 1 ▶ if (ref($e) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'CODE') { } 1 0 1
50 1 push @compiled_rest, {ajax => 1}, $ajax_route;
51 }
52 else {
53 0 push @compiled_rest, {ajax => 1}, $e;
54 }
55 }
56  
57 1 any ['get', 'post'] => $pattern, @compiled_rest;
58 }
59  
60 3 register_plugin;
61 3 1;
62  
63 __END__
« lib/Dancer/Serializer/XML.pm ↑ Summary bin/dancer »