File Coverage

blib/lib/Mojolicious/Plugin/ExposeControllerMethod.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 25 25 100.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ExposeControllerMethod;
2 1     1   100125 use Mojo::Base 'Mojolicious::Plugin';
  1         1  
  1         6  
3              
4 1     1   495 use Mojolicious::Plugin::ExposeControllerMethod::Proxy;
  1         2  
  1         6  
5              
6             our $VERSION = '1.000001';
7             my $PROXY_BASE_CLASS = 'Mojolicious::Plugin::ExposeControllerMethod::Proxy';
8              
9             sub register {
10 1     1 1 28 my ( $self, $app ) = @_;
11              
12             $app->helper(
13             'ctrl',
14             sub {
15 3     3   13512 my $c = shift;
16              
17 3         7 my $proxy_class = $PROXY_BASE_CLASS . '::' . ref $c;
18 3         5 my $isa_name = $proxy_class . '::ISA';
19             {
20             ## no critic (TestingAndDebugging::ProhibitNoStrict)
21 1     1   3 no strict 'refs';
  1         1  
  1         77  
  3         3  
22 3         4 *{$isa_name} = [$PROXY_BASE_CLASS]
  3         33  
23             ## use critic
24             }
25              
26 3         26 return bless \$c, $proxy_class;
27             }
28 1         6 );
29              
30 1         14 return;
31             }
32              
33             1;
34              
35             =head1 NAME
36              
37             Mojolicious::Plugin::ExposeControllerMethod - expose controller method
38              
39             =head1 SYNOPSIS
40              
41             # in your app
42             $app->plugin('ExposeControllerMethod');
43              
44             # Then in a template:
45             Hi <%= ctrl->name %>
46              
47             =head1 DESCRIPTION
48              
49             This module is for advanced use. C<$c>/C<$self> are already made available in
50             templates and are likely sufficient for the majority of use cases. This module
51             was created in order to expose L attributes in a way where you don't
52             have to stash them every single time you want to use them.
53              
54             This module exposes I methods from the current controller to
55             Mojolicious templates via the C helper.
56              
57             In order to expose methods to Mojolicious templates your controller must
58             implement the C method which will be passed the name of
59             the method Mojolicious wishes to call on the controller. This method should
60             return either false (if the method cannot be called), or the name the method
61             that should be called ( which is probably the same as the name of the method
62             passed in.)
63              
64             For example:
65              
66             package MyApp::Controller::Example;
67             use Mojo::Base 'Mojolicious::Controller';
68              
69             sub name { return "Mark Fowler" }
70             sub any_other_name { return "Still smells sweet" }
71             sub reverse { my $self = shift; return scalar reverse join '', @_ }
72              
73             sub controller_method_name {
74             my $self = shift;
75             my $what = shift;
76              
77             return $what if $what =~ /\A(test1|reverse)\z/;
78             return 'any_other_name' if $what eq 'rose';
79             return;
80             }
81              
82             ...
83              
84             The results of C are expected to be consistent for
85             a given Mojolicious Controller class for a given method name (this module
86             is optimized on this assumption, caching method name calculations.)
87              
88             =head1 SUPPORT
89              
90             Please report all issues with this code using the GitHub issue tracker at
91             L.
92              
93             =head1 SEE ALSO
94              
95             L - uses this mechanism to expose
96             attributes marked with a trait from Moose Mojolicious controllers
97              
98             L