File Coverage

blib/lib/Dancer2/Plugin/MobileDevice.pm
Criterion Covered Total %
statement 29 29 100.0
branch 8 8 100.0
condition 4 4 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 51 51 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::MobileDevice;
2              
3 5     5   2678784 use strict;
  5         33  
  5         135  
4 5     5   22 use warnings;
  5         36  
  5         218  
5              
6             our $VERSION = "0.000002";
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::MobileDevice - Make a Dancer2 app mobile-aware
11              
12             =cut
13              
14 5     5   2350 use Dancer2::Plugin;
  5         221742  
  5         40  
15              
16             plugin_keywords qw(is_mobile_device);
17              
18             sub is_mobile_device {
19 39     39 1 774 my $self = shift;
20 39   100     123 return (($self->app->request->user_agent || '')
21             =~ /(?:iP(?:ad|od|hone)|Android|BlackBerry|Mobile|Palm)/) || 0 ;
22             } #is_mobile_device
23              
24             sub BUILD {
25 4     4 1 7713 my $plugin = shift;
26              
27             # Change the layout setting. Save the old to reset the setting later.
28             # We check config->{mobile_layout} within the hook rather than outside
29             # so that the plugin will respond correctly if mobile_layout is
30             # changed between requests.
31             $plugin->app->add_hook(
32             Dancer2::Core::Hook->new(
33             name => 'before',
34             code => sub {
35 17 100   17   214440 return unless $plugin->config->{mobile_layout};
36 4 100       90 return unless $plugin->is_mobile_device;
37 2         378 my $mobile_layout = $plugin->config->{mobile_layout};
38 2         22 $plugin->app->log(debug => "Using mobile layout $mobile_layout");
39              
40 2         969 $plugin->app->request->var(orig_layout => $plugin->app->setting('layout'));
41 2         189 $plugin->app->setting(layout => $mobile_layout);
42             }
43             )
44 4         118 );
45              
46             # After a request, reset the layout for the benefit of future requests.
47             # Don't check the state of the request just in case the mobile_layout
48             # has changed between the beginning and the end of the request (although
49             # I'm not sure how that could happen!).
50             $plugin->app->add_hook(
51             Dancer2::Core::Hook->new(
52             name => 'after',
53             code => sub {
54 17     17   30483 my $vars = $plugin->app->request->vars;
55 17 100       93 return unless exists $vars->{orig_layout};
56             # Can't check truthiness, since undef is a valid
57             # orig_layout value.
58              
59             $plugin->app->log(debug => "resetting layout to " .
60 2   100     14 ($vars->{orig_layout} || ''));
61 2         840 $plugin->app->setting(layout => $vars->{orig_layout});
62             }
63             )
64 4         1639 );
65              
66             # Make variable 'is_mobile_device' available in templates
67             $plugin->app->add_hook(
68             Dancer2::Core::Hook->new(
69             name => 'before_template_render',
70             code => sub {
71 9     9   5414 my $tokens = shift;
72 9         46 $plugin->app->request->var(is_mobile_device => $plugin->is_mobile_device);
73 9         241 $tokens->{'is_mobile_device'} = $plugin->is_mobile_device;
74              
75 9 100       131 $plugin->app->log(debug => 'Requester ' .
76             ($plugin->is_mobile_device ? 'is' : 'is not') .
77             ' mobile device');
78             }
79             )
80 4         1284 );
81              
82             } #BUILD
83              
84             1;
85             __END__