| 1 | package Dancer2::Plugin::MobileDevice; | ||||||||||||||||
| 2 | |||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 5 | |||||||||||||||||
| 6 | 5 | 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 | ▶ | use Dancer2::Plugin; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 15 | |||||||||||||||||
| 16 | 5 | plugin_keywords qw(is_mobile_device); | |||||||||||||||
| 17 | |||||||||||||||||
| 18 | 8 | ▶ | sub is_mobile_device { | ||||||||||||||
|
Subroutine
is_mobile_device: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||
| 19 | 8 | my $self = shift; | |||||||||||||||
| 20 | 8 | ▶ | return (($self->dsl->request->user_agent || '') | ||||||||||||||
|
Condition100%4/4
($self->dsl->request->user_agent || '') =~ /(?:iP(?:ad|od|hone)|Android|BlackBerry|Mobile|Palm)/ || 0
$self->dsl->request->user_agent || ''
MC/DC100%1/1
($self->dsl->request->user_agent || '') =~ /(?:iP(?:ad|od|hone)|Android|BlackBerry|Mobile|Palm)/ || 0
$self->dsl->request->user_agent
Truth table100%2/2
A
A= $self->dsl->request->user_agent
| |||||||||||||||||
| 21 | =~ /(?:iP(?:ad|od|hone)|Android|BlackBerry|Mobile|Palm)/) || 0 ; | ||||||||||||||||
| 22 | } #is_mobile_device | ||||||||||||||||
| 23 | |||||||||||||||||
| 24 | 4 | ▶ | sub BUILD { | ||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=3.5
Pod
documented
| |||||||||||||||||
| 25 | 4 | 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 | 8 | ▶ | sub { | ||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=20.3
| |||||||||||||||||
| 33 | 8 | ▶ | return unless $self->config->{mobile_layout}; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 34 | 0 | ▶ | return unless $self->is_mobile_device; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 35 | 0 | my $mobile_layout = $self->config->{mobile_layout}; | |||||||||||||||
| 36 | 0 | $self->dsl->debug("Using mobile layout $mobile_layout"); | |||||||||||||||
| 37 | |||||||||||||||||
| 38 | 0 | $self->dsl->request->var(orig_layout => $self->dsl->setting('layout')); | |||||||||||||||
| 39 | 0 | $self->dsl->setting(layout => $mobile_layout); | |||||||||||||||
| 40 | } | ||||||||||||||||
| 41 | 4 | ); | |||||||||||||||
| 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 | 8 | ▶ | sub { | ||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=22.0
| |||||||||||||||||
| 49 | 8 | my $vars = $self->dsl->request->vars; | |||||||||||||||
| 50 | 0 | ▶ | return unless exists $vars->{orig_layout}; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 51 | # Can't check truthiness, since undef is a valid | ||||||||||||||||
| 52 | # orig_layout value. | ||||||||||||||||
| 53 | |||||||||||||||||
| 54 | $self->dsl->log(debug => "resetting layout to " . | ||||||||||||||||
| 55 | 0 | ▶ | ($vars->{orig_layout} || '<falsy>')); | ||||||||||||||
|
Condition0%0/2
$vars->{'orig_layout'} || '<falsy>'
MC/DC0%0/1
$vars->{'orig_layout'} || '<falsy>'
$vars->{'orig_layout'}
Truth table0%0/2
A
A= $vars->{'orig_layout'}
| |||||||||||||||||
| 56 | 0 | $self->dsl->setting(layout => $vars->{orig_layout}); | |||||||||||||||
| 57 | } | ||||||||||||||||
| 58 | 4 | ); | |||||||||||||||
| 59 | |||||||||||||||||
| 60 | # Make variable 'is_mobile_device' available in templates | ||||||||||||||||
| 61 | $self->dsl->hook('before_template_render', | ||||||||||||||||
| 62 | 0 | ▶ | sub { | ||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||
| 63 | 0 | my $tokens = shift; | |||||||||||||||
| 64 | 0 | $self->dsl->request->var(is_mobile_device => $self->is_mobile_device); | |||||||||||||||
| 65 | 0 | $tokens->{'is_mobile_device'} = $self->is_mobile_device; | |||||||||||||||
| 66 | |||||||||||||||||
| 67 | 0 | ▶ | $self->dsl->log(debug => 'Requester ' . | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 68 | ($self->is_mobile_device ? 'is' : 'is not') . | ||||||||||||||||
| 69 | ' mobile device'); | ||||||||||||||||
| 70 | } | ||||||||||||||||
| 71 | 4 | ); | |||||||||||||||
| 72 | |||||||||||||||||
| 73 | } #BUILD | ||||||||||||||||
| 74 | |||||||||||||||||
| 75 | 5 | 1; | |||||||||||||||
| 76 | __END__ | ||||||||||||||||