Skip to content

Instantly share code, notes, and snippets.

/nested.diff Secret

Created August 7, 2014 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/86ec9740af9758a35a27 to your computer and use it in GitHub Desktop.
Save anonymous/86ec9740af9758a35a27 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious/Controller.pm b/lib/Mojolicious/Controller.pm
index 1322e45..802bcd1 100644
--- a/lib/Mojolicious/Controller.pm
+++ b/lib/Mojolicious/Controller.pm
@@ -2,7 +2,8 @@ package Mojolicious::Controller;
use Mojo::Base -base;
# No imports, for security reasons!
-use Carp ();
+use Carp ();
+use List::Util ();
use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Transaction::HTTP;
@@ -31,10 +32,17 @@ sub AUTOLOAD {
Carp::croak "Undefined subroutine &${package}::$method called"
unless Scalar::Util::blessed $self && $self->isa(__PACKAGE__);
- # Call helper with current controller
+ my $helpers = $self->app->renderer->helpers;
+ my $helper = $helpers->{$method};
+ my $found
+ = !!$helper || !!List::Util::first { $_ =~ /^$method\./ } keys %$helpers;
Carp::croak qq{Can't locate object method "$method" via package "$package"}
- unless my $helper = $self->app->renderer->helpers->{$method};
- return $self->$helper(@_);
+ unless $found;
+ return $self->$helper(@_) if $helper;
+ return Mojolicious::Controller::_Proxy->new(
+ namespace => $method,
+ controller => $self
+ );
}
sub continue { $_[0]->app->routes->continue($_[0]) }
@@ -428,6 +436,23 @@ sub _fallbacks {
return $self->render_maybe(%$options, inline => $inline, handler => 'ep');
}
+package Mojolicious::Controller::_Proxy;
+use Mojo::Base -base;
+
+sub AUTOLOAD {
+ my $self = shift;
+
+ my ($package, $method) = split /::(\w+)$/, our $AUTOLOAD;
+ Carp::croak "Undefined subroutine &${package}::$method called"
+ unless Scalar::Util::blessed $self && $self->isa(__PACKAGE__);
+
+ my $c = $self->{controller};
+ my $namespace = $self->{namespace};
+ Carp::croak qq{Can't locate object method "$method" via package "$package"}
+ unless my $helper = $c->app->renderer->helpers->{"$namespace.$method"};
+ return $c->$helper(@_);
+}
+
1;
=encoding utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment