line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ModeHelpers; |
2
|
6
|
|
|
6
|
|
4126
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
39
|
|
3
|
6
|
|
|
6
|
|
1070
|
use Carp (); |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
72
|
|
4
|
6
|
|
|
6
|
|
24
|
use Mojo::Util (); |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
3033
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
38
|
|
|
38
|
1
|
16430
|
my (undef, $app, $config) = @_; |
10
|
38
|
|
|
|
|
148
|
$config = { |
11
|
|
|
|
|
|
|
prod_helper_name => 'in_prod', |
12
|
|
|
|
|
|
|
dev_helper_name => 'in_dev', |
13
|
|
|
|
|
|
|
modes => [], |
14
|
|
|
|
|
|
|
%$config, |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
|
17
|
38
|
|
|
|
|
122
|
my %config_copy = %$config; |
18
|
38
|
|
|
|
|
102
|
delete @config_copy{qw(prod_helper_name dev_helper_name modes)}; |
19
|
38
|
100
|
|
|
|
85
|
Carp::croak "Unknown config options provided: @{[join ', ', sort keys %config_copy]}" if %config_copy; |
|
2
|
|
|
|
|
33
|
|
20
|
|
|
|
|
|
|
|
21
|
36
|
100
|
100
|
|
|
165
|
if (not defined $config->{prod_helper_name} or $config->{prod_helper_name} eq '') { |
22
|
2
|
|
|
|
|
32
|
Carp::croak 'prod_helper_name must not be empty'; |
23
|
|
|
|
|
|
|
} |
24
|
34
|
100
|
100
|
|
|
115
|
if (not defined $config->{dev_helper_name} or $config->{dev_helper_name} eq '') { |
25
|
2
|
|
|
|
|
16
|
Carp::croak 'dev_helper_name must not be empty'; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$app->helper($config->{prod_helper_name} => sub { |
29
|
11
|
|
|
11
|
|
2421
|
state $in_prod = $_[0]->app->mode eq 'production'; |
30
|
32
|
|
|
|
|
264
|
}); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$app->helper($config->{dev_helper_name} => sub { |
33
|
11
|
|
|
11
|
|
3647
|
state $in_dev = $_[0]->app->mode ne 'production'; |
34
|
32
|
|
|
|
|
2284
|
}); |
35
|
|
|
|
|
|
|
|
36
|
32
|
|
|
|
|
1525
|
my $modes = $config->{modes}; |
37
|
32
|
100
|
|
|
|
131
|
Carp::croak 'modes must be an arrayref' unless ref $modes eq 'ARRAY'; |
38
|
|
|
|
|
|
|
|
39
|
28
|
|
|
|
|
73
|
for my $mode (@$modes) { |
40
|
22
|
|
|
|
|
123
|
my $helper_name; |
41
|
22
|
|
|
|
|
36
|
my $mode_ref = ref $mode; |
42
|
22
|
100
|
|
|
|
50
|
if (not $mode_ref) { |
|
|
100
|
|
|
|
|
|
43
|
9
|
100
|
100
|
|
|
46
|
Carp::croak 'empty value for mode' unless defined $mode and $mode ne ''; |
44
|
7
|
|
|
|
|
20
|
$helper_name = Mojo::Util::slugify($mode); |
45
|
7
|
|
|
|
|
204
|
$helper_name =~ tr/-/_/; |
46
|
7
|
|
|
|
|
13
|
$helper_name = "in_$helper_name"; |
47
|
|
|
|
|
|
|
} elsif ($mode_ref eq 'HASH') { |
48
|
12
|
100
|
|
|
|
46
|
Carp::croak 'helper name and mode pair must be a hashref with exactly one key and one value' |
49
|
|
|
|
|
|
|
unless keys %$mode == 1; |
50
|
|
|
|
|
|
|
|
51
|
10
|
|
|
|
|
23
|
($helper_name, $mode) = (keys %$mode, values %$mode); |
52
|
10
|
100
|
66
|
|
|
45
|
Carp::croak 'empty value for helper name in key-value pair' unless defined $helper_name and $helper_name ne ''; |
53
|
9
|
100
|
100
|
|
|
41
|
Carp::croak 'empty value for mode in key-value pair' unless defined $mode and $mode ne ''; |
54
|
|
|
|
|
|
|
} else { |
55
|
1
|
|
|
|
|
9
|
Carp::croak 'mode must be a scalar (valid subroutine name) or a hashref with one key-value pair'; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$app->helper($helper_name => sub { |
59
|
21
|
|
|
21
|
|
5722
|
state $in_mode = $_[0]->app->mode eq $mode; |
60
|
14
|
|
|
|
|
56
|
}); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
__END__ |