Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow helper methods to be called from the application
  • Loading branch information
kraih committed Oct 13, 2010
1 parent a123468 commit e30ca27
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
30 changes: 29 additions & 1 deletion lib/Mojolicious.pm
Expand Up @@ -36,6 +36,34 @@ __PACKAGE__->attr(types => sub { MojoX::Types->new });
our $CODENAME = 'Hot Beverage';
our $VERSION = '0.999930';

our $AUTOLOAD;

sub AUTOLOAD {
my $self = shift;

# Method
my ($method) = $AUTOLOAD =~ /(\w+)$/;

# Helper
Carp::croak(qq/Helper "$method" not found/)
unless my $helper = $self->renderer->helper->{$method};

# Load controller class
my $class = $self->controller_class;
if (my $e = Mojo::Loader->load($class)) {
$self->log->error(
ref $e
? qq/Can't load controller class "$class": $e/
: qq/Controller class "$class" doesn't exist./
);
}

# Run
return $class->new(app => $self)->$helper(@_);
}

sub DESTROY { }

# I personalized each of your meals.
# For example, Amy: you're cute, so I baked you a pony.
sub new {
Expand Down Expand Up @@ -551,7 +579,7 @@ Note that this method is EXPERIMENTAL and might change without warning!
# Helper
$app->helper(add => sub { $_[1] + $_[2] });
# Controller
# Controller/Application
my $result = $self->add(2, 3);
# Template
Expand Down
9 changes: 6 additions & 3 deletions t/mojolicious/lite_app.t
Expand Up @@ -14,7 +14,7 @@ use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 661;
plan tests => 663;

# Pollution
123 =~ m/(\d+)/;
Expand Down Expand Up @@ -55,8 +55,11 @@ plugin 'PluginWithTemplate';
app->defaults(default => 23);

# Test helpers
app->helper(test_helper => sub { shift->param(@_) });
app->helper(dead => sub { die $_[1] || 'works!' });
app->helper(test_helper => sub { shift->param(@_) });
app->helper(test_helper2 => sub { shift->app->controller_class });
app->helper(dead => sub { die $_[1] || 'works!' });
is app->test_helper('foo'), undef, 'no value yet';
is app->test_helper2, 'Mojolicious::Controller', 'right value';

# Test renderer
app->renderer->add_handler(dead => sub { die 'renderer works!' });
Expand Down

0 comments on commit e30ca27

Please sign in to comment.