rename ActionRole::Serialize -> ActionRole::SerializeFormat
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / ActionRole / SerializeFormat.pm
1 package Catalyst::ActionRole::SerializeFormat;
2 use Moose::Role;
3 use Catalyst::ControllerRole::SerializeConfig;
4 use Moose::Util qw(does_role);
5 use namespace::clean -except => 'meta';
6 requires 'serialize';
7
8 around execute => sub {
9   my $next = shift;
10   my ($self, $controller, $c, $arg) = @_;
11
12   # XXX is ignoring the return value here correct? the original serialize
13   # actions never even called their body.
14   $self->$next($controller, $c, $arg)
15     if blessed $self;
16
17   Catalyst::ControllerRole::SerializeConfig->meta->apply($controller)
18     unless does_role($controller, 'Catalyst::ControllerRole::SerializeConfig');
19       
20   my $stash_key = $controller->serialize_config->{stash_key} || 'rest';
21
22   my $output;
23   eval {
24     $output = $self->serialize(
25       $c->stash->{$stash_key},
26       $c,
27       $arg,
28     );
29   };
30   return $@ if $@;
31   # horrible, but the best I can do given the existing magic return value
32   # conventions.
33   return $output if $output eq '0';
34   $c->response->body($output) unless $c->response->body;
35   return 1;
36 };
37
38 1;