refactor serialize actions
Hans Dieter Pearcey [Mon, 13 Apr 2009 06:21:05 +0000 (02:21 -0400)]
lib/Catalyst/Action/Serialize/Data/Serializer.pm
lib/Catalyst/Action/Serialize/JSON.pm
lib/Catalyst/Action/Serialize/View.pm
lib/Catalyst/Action/Serialize/XML/Simple.pm
lib/Catalyst/Action/Serialize/YAML.pm
lib/Catalyst/Action/Serialize/YAML/HTML.pm
lib/Catalyst/ActionRole/Serialize.pm [new file with mode: 0644]

index 24d88f1..cd885c5 100644 (file)
@@ -1,46 +1,18 @@
-#
-# Catalyst::Action::Serialize::Data::Serializer
-# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
-#
-# $Id$
-
 package Catalyst::Action::Serialize::Data::Serializer;
-
-use strict;
-use warnings;
-
-use base 'Catalyst::Action';
+use Moose;
+extends 'Catalyst::Action';
+with 'Catalyst::ActionRole::Serialize';
 use Data::Serializer;
+use namespace::clean -except => 'meta';
 
-sub execute {
-    my $self = shift;
-    my ( $controller, $c, $serializer ) = @_;
-
-    my $stash_key = (
-            $controller->{'serialize'} ?
-                $controller->{'serialize'}->{'stash_key'} :
-                $controller->{'stash_key'} 
-        ) || 'rest';
-    my $sp = $serializer;
-    $sp =~ s/::/\//g;
-    $sp .= ".pm";
-    eval {
-        require $sp
-    };
-    if ($@) {
-        $c->log->info("Could not load $serializer, refusing to serialize: $@");
-        return 0;
-    }
-    my $dso = Data::Serializer->new( serializer => $serializer );
-    my $data;
-    eval {
-       $data = $dso->raw_serialize($c->stash->{$stash_key});
-    };
-    if ($@) {
-        return $@;
-    } 
-    $c->response->output( $data );
-    return 1;
+sub serialize {
+  my ($self, $data, $c, $serializer) = @_;
+  unless (eval "use $serializer; 1") {
+    $c->log->info("Could not load $serializer, refusing to serialize: $@");
+    return 0;
+  }
+  my $d = Data::Serializer->new(serializer => $serializer);
+  return $d->raw_serialize($data);
 }
 
 1;
index 561b49b..17ca446 100644 (file)
@@ -1,36 +1,10 @@
-#
-# Catlyst::Action::Serialize::JSON.pm
-# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
-# Created on: 10/12/2006 03:00:32 PM PDT
-#
-# $Id$
-
 package Catalyst::Action::Serialize::JSON;
-
-use strict;
-use warnings;
-
-use base 'Catalyst::Action';
+use Moose;
+extends 'Catalyst::Action';
+with 'Catalyst::ActionRole::Serialize';
 use JSON qw(encode_json);
+use namespace::clean -except => 'meta';
 
-sub execute {
-    my $self = shift;
-    my ( $controller, $c ) = @_;
-
-    my $stash_key = (
-            $controller->{'serialize'} ?
-                $controller->{'serialize'}->{'stash_key'} :
-                $controller->{'stash_key'} 
-        ) || 'rest';
-    my $output;
-    eval {
-        $output = encode_json( $c->stash->{$stash_key} );
-    };
-    if ($@) {
-        return $@;
-    }
-    $c->response->output( $output );
-    return 1;
-}
+sub serialize { encode_json $_[1] }
 
 1;
index 630a4e7..85d9df0 100644 (file)
@@ -1,25 +1,18 @@
 package Catalyst::Action::Serialize::View;
-use strict;
-use warnings;
+use Moose;
+extends 'Catalyst::Action';
+with 'Catalyst::ActionRole::Serialize';
+use namespace::clean -except => 'meta';
 
-use base 'Catalyst::Action';
+sub serialize {
+  my ($self, $data, $c, $view) = @_;
 
-sub execute {
-    my $self = shift;
-    my ( $controller, $c, $view ) = @_;
+  unless ($c->view($view)) {
+      $c->log->error("Could not load $view, refusing to serialize");
+      return 0;
+  }
 
-    my $stash_key = (
-            $controller->{'serialize'} ?
-                $controller->{'serialize'}->{'stash_key'} :
-                $controller->{'stash_key'} 
-        ) || 'rest';
-
-    if ( !$c->view($view) ) {
-        $c->log->error("Could not load $view, refusing to serialize");
-        return 0;
-    }
-
-    return $c->view($view)->process($c);
+  return $c->view($view)->process($c);
 }
 
 1;
index fe0b5c1..82377e6 100644 (file)
@@ -1,45 +1,14 @@
-#
-# Catlyst::Action::Serialize::XML::Simple.pm
-# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
-# Created on: 10/12/2006 03:00:32 PM PDT
-#
-# $Id$
-
 package Catalyst::Action::Serialize::XML::Simple;
-
-use strict;
-use warnings;
-
-use base 'Catalyst::Action';
-
-sub execute {
-    my $self = shift;
-    my ( $controller, $c ) = @_;
-
-    eval {
-        require XML::Simple
-    };
-    if ($@) {
-        $c->log->debug("Could not load XML::Serializer, refusing to serialize: $@")
-            if $c->debug;
-        return 0;
-    }
-    my $xs = XML::Simple->new(ForceArray => 0,);
-
-    my $stash_key = (
-            $controller->{'serialize'} ?
-                $controller->{'serialize'}->{'stash_key'} :
-                $controller->{'stash_key'} 
-        ) || 'rest';
-    my $output;
-    eval {
-        $output = $xs->XMLout({ data => $c->stash->{$stash_key} });
-    };
-    if ($@) {
-        return $@;
-    }
-    $c->response->output( $output );
-    return 1;
+use Moose;
+extends 'Catalyst::Action';
+with 'Catalyst::ActionRole::Serialize';
+use XML::Simple;
+use namespace::clean -except => 'meta';
+
+sub serialize {
+  my ($self, $data, $c) = @_;
+  my $x = XML::Simple->new(ForceArray => 0);
+  return $x->XMLout({ data => $data });
 }
 
 1;
index c943569..2bbe873 100644 (file)
@@ -1,36 +1,10 @@
-#
-# Catalyst::Action::Serialize::YAML.pm
-# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
-# Created on: 10/12/2006 03:00:32 PM PDT
-#
-# $Id$
-
 package Catalyst::Action::Serialize::YAML;
-
-use strict;
-use warnings;
-
-use base 'Catalyst::Action';
+use Moose;
+extends 'Catalyst::Action';
+with 'Catalyst::ActionRole::Serialize';
 use YAML::Syck;
+use namespace::clean -except => 'meta';
 
-sub execute {
-    my $self = shift;
-    my ( $controller, $c ) = @_;
-
-    my $stash_key = (
-            $controller->{'serialize'} ?
-                $controller->{'serialize'}->{'stash_key'} :
-                $controller->{'stash_key'} 
-        ) || 'rest';
-    my $output;
-    eval {
-        $output = Dump($c->stash->{$stash_key});
-    };
-    if ($@) {
-        return $@;
-    }
-    $c->response->output( $output );
-    return 1;
-}
+sub serialize { Dump $_[1] }
 
 1;
index 7d6fb31..4773c0b 100644 (file)
@@ -1,52 +1,33 @@
-#
-# Catlyst::Action::Serialize::YAML::HTML.pm
-# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
-# Created on: 10/12/2006 03:00:32 PM PDT
-#
-# $Id$
-
 package Catalyst::Action::Serialize::YAML::HTML;
-
-use strict;
-use warnings;
-
-use base 'Catalyst::Action';
-use YAML::Syck;
+use Moose;
+extends 'Catalyst::Action::Serialize::YAML';
 use URI::Find;
+use namespace::clean -except => 'meta';
 
-sub execute {
-    my $self = shift;
-    my ( $controller, $c ) = @_;
-
-    my $stash_key = (
-            $controller->{'serialize'} ?
-                $controller->{'serialize'}->{'stash_key'} :
-                $controller->{'stash_key'} 
-        ) || 'rest';
-    my $app = $c->config->{'name'} || '';
-    my $output = "<html>";
-    $output .= "<title>" . $app . "</title>";
-    $output .= "<body><pre>";
-    my $text = Dump($c->stash->{$stash_key});
-    # Straight from URI::Find
-    my $finder = URI::Find->new(
-                              sub {
-                                  my($uri, $orig_uri) = @_;
-                                  my $newuri;
-                                  if ($uri =~ /\?/) {
-                                      $newuri = $uri . "&content-type=text/html";
-                                  } else {
-                                      $newuri = $uri . "?content-type=text/html";
-                                  }
-                                  return qq|<a href="$newuri">$orig_uri</a>|;
-                              });
-    $finder->find(\$text);
-    $output .= $text;
-    $output .= "</pre>";
-    $output .= "</body>";
-    $output .= "</html>";
-    $c->response->output( $output );
-    return 1;
-}
+around serialize => sub {
+  my $next = shift;
+  my ($self, $data, $c) = @_;
+  my $yaml = $self->$next($data, $c);
+  my $app = $c->config->{name} || '';
+  my $finder = URI::Find->new(sub {
+    my($uri, $orig_uri) = @_;
+    my $newuri;
+    if ($uri =~ /\?/) {
+        $newuri = $uri . "&content-type=text/html";
+    } else {
+        $newuri = $uri . "?content-type=text/html";
+    }
+    return qq|<a href="$newuri">$orig_uri</a>|;
+  });
+  my $output = "<html>";
+  $output .= "<title>" . $app . "</title>";
+  $output .= "<body><pre>";
+  $finder->find(\$yaml);
+  $output .= $yaml;
+  $output .= "</pre>";
+  $output .= "</body>";
+  $output .= "</html>";
+  return $output;
+};
 
 1;
diff --git a/lib/Catalyst/ActionRole/Serialize.pm b/lib/Catalyst/ActionRole/Serialize.pm
new file mode 100644 (file)
index 0000000..c8e7ce9
--- /dev/null
@@ -0,0 +1,34 @@
+package Catalyst::ActionRole::Serialize;
+use Moose::Role;
+use Catalyst::ControllerRole::SerializeConfig;
+use Moose::Util qw(does_role);
+use namespace::clean -except => 'meta';
+requires 'serialize';
+
+around execute => sub {
+  # the original Serialize::* actions never executed their body, so this is
+  # ignored.
+  my $next = shift;
+  my ($self, $controller, $c, $arg) = @_;
+  Catalyst::ControllerRole::SerializeConfig->meta->apply($controller)
+    unless does_role($controller, 'Catalyst::ControllerRole::SerializeConfig');
+      
+  my $stash_key = $controller->serialize_config->{stash_key} || 'rest';
+
+  my $output;
+  eval {
+    $output = $self->serialize(
+      $c->stash->{$stash_key},
+      $c,
+      $arg,
+    );
+  };
+  return $@ if $@;
+  # horrible, but the best I can do given the existing magic return value
+  # conventions.
+  return $output if $output eq '0';
+  $c->response->body($output) unless $c->response->body;
+  return 1;
+};
+
+1;