put response methods on the response
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / ActionRole / Serialize.pm
1 package Catalyst::ActionRole::Serialize;
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   # the original Serialize::* actions never executed their body, so this is
10   # ignored.
11   my $next = shift;
12   my ($self, $controller, $c, $arg) = @_;
13   Catalyst::ControllerRole::SerializeConfig->meta->apply($controller)
14     unless does_role($controller, 'Catalyst::ControllerRole::SerializeConfig');
15       
16   my $stash_key = $controller->serialize_config->{stash_key} || 'rest';
17
18   my $output;
19   eval {
20     $output = $self->serialize(
21       $c->stash->{$stash_key},
22       $c,
23       $arg,
24     );
25   };
26   return $@ if $@;
27   # horrible, but the best I can do given the existing magic return value
28   # conventions.
29   return $output if $output eq '0';
30   $c->response->body($output) unless $c->response->body;
31   return 1;
32 };
33
34 1;