File Coverage

blib/lib/Dancer/Plugin/MobileDevice.pm
Criterion Covered Total %
statement 13 13 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package Dancer::Plugin::MobileDevice;
2             {
3             $Dancer::Plugin::MobileDevice::VERSION = '0.05';
4             }
5             #ABSTRACT: make a Dancer app mobile-aware
6              
7 5     5   976195 use strict;
  5         14  
  5         176  
8 5     5   26 use warnings;
  5         10  
  5         146  
9              
10 5     5   2666 use Dancer ':syntax';
  5         665268  
  5         31  
11 5     5   6938 use Dancer::Plugin;
  5         7920  
  5         1779  
12              
13             register 'is_mobile_device' => sub {
14 24 100   24   1253 return request->user_agent =~
15             /(?:iP(?:ad|od|hone)|Android|BlackBerry|Mobile|Palm)/
16             ? 1 : 0;
17             };
18              
19             hook before => sub {
20             # If we don't have a mobile layout declared, do nothing.
21             my $settings = plugin_setting || {};
22              
23             if (my $mobile_layout = $settings->{mobile_layout}) {
24             # OK, remember the original layout setting (so we can restore it
25             # after the request), and override it with the specified mobile layout.
26             if (is_mobile_device()) {
27             var orig_layout => setting('layout');
28             setting layout => $mobile_layout;
29             }
30             }
31             };
32              
33             hook after => sub {
34             my $settings = plugin_setting || {};
35             if ( $settings->{mobile_layout} && is_mobile_device() ) {
36             setting layout => delete vars->{orig_layout};
37             }
38             };
39              
40             hook before_template => sub {
41             my $tokens = shift;
42             $tokens->{'is_mobile_device'} = is_mobile_device();
43             };
44              
45             register_plugin for_versions => [ 1, 2 ];
46              
47             1;
48              
49             __END__