Skip to content

Commit

Permalink
Singularize things
Browse files Browse the repository at this point in the history
execute_hook register_hook replace_hook
there is still one plural: compile_hooks
(for discussion check issue 58)
  • Loading branch information
ambs committed Jul 25, 2012
1 parent aac651b commit 499f257
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 37 deletions.
8 changes: 4 additions & 4 deletions lib/Dancer/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ around add_hook => sub {

};

around execute_hooks => sub {
around execute_hook => sub {
my ($orig, $self) = (shift, shift);
my ($hook, @args) = @_;
unless ($self->has_hook($hook)) {
foreach my $cand ($self->hook_candidates) {
return $cand->execute_hooks(@_) if $cand->has_hook($hook);
return $cand->execute_hook(@_) if $cand->has_hook($hook);
}
}

Expand Down Expand Up @@ -312,7 +312,7 @@ sub send_file {

for my $h (keys %{ $self->route_handlers->{File}->hooks } ) {
my $hooks = $self->route_handlers->{File}->hooks->{$h};
$file_handler->replace_hooks($h, $hooks);
$file_handler->replace_hook($h, $hooks);
}

$self->context->request->path_info($path);
Expand Down Expand Up @@ -384,7 +384,7 @@ sub compile_hooks {

push @{$compiled_hooks}, $compiled;
}
$self->replace_hooks($position, $compiled_hooks);
$self->replace_hook($position, $compiled_hooks);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Dancer/Core/Dispatcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ sub dispatch {
# go to the next route if no match
next if !$match;
my $content;
$app->execute_hooks('core.app.before_request', $context);
$app->execute_hook('core.app.before_request', $context);

if (! $context->response->is_halted) {
eval { $content = $route->execute($context) };
Expand Down Expand Up @@ -106,7 +106,7 @@ sub dispatch {
next;
}

$app->execute_hooks('core.app.after_request', $response);
$app->execute_hook('core.app.after_request', $response);
$app->context(undef);
return $response;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Dancer/Core/Error.pm
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ has context => (

sub BUILD {
my ($self) = @_;
$self->execute_hooks('core.error.init', $self);
$self->execute_hook('core.error.init', $self);
}

has exception => (
Expand All @@ -126,10 +126,10 @@ has exception => (
sub render {
my $self = shift;

$self->execute_hooks('core.error.before', $self);
$self->execute_hook('core.error.before', $self);
my $response = $self->_render_html();
$self->execute_hooks('core.error.after', $response);
$self->execute_hook('core.error.after', $response);

return $response;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Dancer/Core/Hook.pm
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ Add a new hook name, so application developers can insert some code at this poin
Test if a hook with this name has already been registered.
=head2 execute_hooks
=head2 execute_hook
Execute a list of hooks for some position
Execute a hooks
=head2 get_hooks_for
Expand Down
4 changes: 2 additions & 2 deletions lib/Dancer/Core/Role/Hookable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ sub add_hook {
# allows the caller to replace the current list of hooks at the given position
# this is useful if the object where this role is composed wants to compile the
# hooks.
sub replace_hooks {
sub replace_hook {
my ($self, $position, $hooks) = @_;

croak "Hook '$position' must be installed first"
Expand All @@ -114,7 +114,7 @@ sub has_hook {
}

# Exectue the hook at the given position
sub execute_hooks {
sub execute_hook {
my ($self, $name, @args) = @_;

croak "execute_hook needs a hook name"
Expand Down
4 changes: 2 additions & 2 deletions lib/Dancer/Core/Role/Serializer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ around serialize => sub {
my ($orig, $self) = (shift, shift);
my ($data) = @_;

$self->execute_hooks('engine.serializer.before', $data);
$self->execute_hook('engine.serializer.before', $data);
my $serialized = $self->$orig($data);
$self->execute_hooks('engine.serializer.after', $serialized);
$self->execute_hook('engine.serializer.after', $serialized);

return $serialized;
};
Expand Down
8 changes: 4 additions & 4 deletions lib/Dancer/Core/Role/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ sub apply_renderer {
$view = $self->view($view) if ! ref $view;
$tokens = $self->_prepare_tokens_options($tokens);

$self->execute_hooks('engine.template.before_render', $tokens);
$self->execute_hook('engine.template.before_render', $tokens);

my $content = $self->render($view, $tokens);
$self->execute_hooks('engine.template.after_render', \$content);
$self->execute_hook('engine.template.after_render', \$content);

# make sure to avoid ( undef ) in list context return
defined $content and return $content;
Expand All @@ -102,12 +102,12 @@ sub apply_layout {
defined $content or return;
defined $layout or return $content;

$self->execute_hooks('engine.template.before_layout_render', $tokens, \$content);
$self->execute_hook('engine.template.before_layout_render', $tokens, \$content);

my $full_content =
$self->render_layout($layout, $tokens, $content);

$self->execute_hooks('engine.template.after_layout_render', \$full_content);
$self->execute_hook('engine.template.after_layout_render', \$full_content);

# make sure to avoid ( undef ) in list context return
defined $full_content and return $full_content;
Expand Down
6 changes: 3 additions & 3 deletions lib/Dancer/Handler/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ sub code {
}

my $file_path = path($self->public_dir, @tokens);
$self->execute_hooks('handler.file.before_render', $file_path);
$self->execute_hook('handler.file.before_render', $file_path);

if (! -f $file_path) {
$ctx->response->has_passed(1);
Expand All @@ -107,7 +107,7 @@ sub code {
}

my @stat = stat $file_path;

$ctx->response->header('Content-Type') or
$ctx->response->header('Content-Type', $content_type);

Expand All @@ -118,7 +118,7 @@ sub code {
$ctx->response->header('Last-Modified', HTTP::Date::time2str($stat[9]));

$ctx->response->content($content);
$self->execute_hooks('handler.file.after_render', $ctx->response);
$self->execute_hook('handler.file.after_render', $ctx->response);
return ($ctx->request->method eq 'GET') ? $content : '';
};
}
Expand Down
14 changes: 7 additions & 7 deletions lib/Dancer/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ sub plugin_setting {
=method register_hook
Allows a plugin to delcare a list of supported hooks. Any hook declared like so
can be executed by the plugin with C<execute_hooks>.
can be executed by the plugin with C<execute_hook>.
register_hook 'foo';
register_hook 'foo', 'bar', 'baz';
Expand Down Expand Up @@ -232,26 +232,26 @@ sub register_hook {
}
}

=method execute_hooks
=method execute_hook
Allows a plugin to execute the hooks attached at the given position
execute_hooks 'some_hook';
execute_hook 'some_hook';
Arguments can be passed which will be received by handlers attached to that
hook:
execute_hooks 'some_hook', $some_args, ... ;
execute_hook 'some_hook', $some_args, ... ;
The hook must have been registered by the plugin first, with C<register_hook>.
=cut

sub execute_hooks {
sub execute_hook {
my $position = shift;
my $dsl = _get_dsl();
croak "No DSL object found" if !defined $dsl;
$dsl->execute_hooks($position, @_);
$dsl->execute_hook($position, @_);
}

# private
Expand All @@ -262,7 +262,7 @@ sub import {

# First, export Dancer::Plugins symbols
my @export = qw(
execute_hooks
execute_hook
register_hook
register_plugin
register
Expand Down
2 changes: 1 addition & 1 deletion t/app.t
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ like(
'before filter nonexistent method failure',
);

$app->replace_hooks('core.app.before_request', [ sub { 1 } ]);
$app->replace_hook('core.app.before_request', [ sub { 1 } ]);
$app->compile_hooks;
$env = {
REQUEST_METHOD => 'GET',
Expand Down
2 changes: 1 addition & 1 deletion t/lib/Hookee.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ register_hook 'start_hookee', 'stop_hookee';
register_hook 'third_hook';

register some_keyword => sub {
execute_hooks('start_hookee');
execute_hook('start_hookee');
};

register some_other => sub {
Expand Down
10 changes: 5 additions & 5 deletions t/roles/hook.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ is $h->code->(), 'BT';
my $f = Foo->new;

like(
exception { $f->execute_hooks() },
exception { $f->execute_hook() },
qr{execute_hook needs a hook name},
'execute_hook needs a hook name',
);
Expand Down Expand Up @@ -49,16 +49,16 @@ like(
'Unsupported hook cannot be installed',
);

$f->execute_hooks('foobar');
$f->execute_hook('foobar');
is $count, 1;

like(
exception { $f->replace_hooks( 'doesnotexist', [] ) },
exception { $f->replace_hook( 'doesnotexist', [] ) },
qr{Hook 'doesnotexist' must be installed first},
'Nonexistent hook fails',
);

my $new_hooks = [ sub {$count--}, sub {$count--}, sub {$count--} ];
$f->replace_hooks('foobar',$new_hooks);
$f->execute_hooks('foobar');
$f->replace_hook('foobar', $new_hooks);
$f->execute_hook('foobar');
is $count, -2, 'replaced hooks were installed and executed';

0 comments on commit 499f257

Please sign in to comment.