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   3324429 use strict;
  5         36  
  5         151  
4 5     5   28 use warnings;
  5         12  
  5         238  
5              
6             our $VERSION = "0.000003";
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::MobileDevice - Make a Dancer2 app mobile-aware
11              
12             =cut
13              
14 5     5   2802 use Dancer2::Plugin;
  5         282120  
  5         44  
15              
16             plugin_keywords qw(is_mobile_device);
17              
18             sub is_mobile_device {
19 39     39 1 994 my $self = shift;
20 39   100     94 return (($self->dsl->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 8971 my $self = 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             $self->dsl->hook('before',
32             sub {
33 17 100   17   315585 return unless $self->config->{mobile_layout};
34 4 100       105 return unless $self->is_mobile_device;
35 2         596 my $mobile_layout = $self->config->{mobile_layout};
36 2         21 $self->dsl->debug("Using mobile layout $mobile_layout");
37              
38 2         1292 $self->dsl->request->var(orig_layout => $self->dsl->setting('layout'));
39 2         297 $self->dsl->setting(layout => $mobile_layout);
40             }
41 4         20 );
42              
43             # After a request, reset the layout for the benefit of future requests.
44             # Don't check the state of the request just in case the mobile_layout
45             # has changed between the beginning and the end of the request (although
46             # I'm not sure how that could happen!).
47             $self->dsl->hook('after',
48             sub {
49 17     17   36130 my $vars = $self->dsl->request->vars;
50 17 100       188 return unless exists $vars->{orig_layout};
51             # Can't check truthiness, since undef is a valid
52             # orig_layout value.
53              
54             $self->dsl->log(debug => "resetting layout to " .
55 2   100     6 ($vars->{orig_layout} || ''));
56 2         1223 $self->dsl->setting(layout => $vars->{orig_layout});
57             }
58 4         1960 );
59              
60             # Make variable 'is_mobile_device' available in templates
61             $self->dsl->hook('before_template_render',
62             sub {
63 9     9   6809 my $tokens = shift;
64 9         36 $self->dsl->request->var(is_mobile_device => $self->is_mobile_device);
65 9         337 $tokens->{'is_mobile_device'} = $self->is_mobile_device;
66              
67 9 100       186 $self->dsl->log(debug => 'Requester ' .
68             ($self->is_mobile_device ? 'is' : 'is not') .
69             ' mobile device');
70             }
71 4         1592 );
72              
73             } #BUILD
74              
75             1;
76             __END__