summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-07-01 18:37:00 -0400
committerJesse Luehrs <doy@tozt.net>2013-07-01 18:37:00 -0400
commit4bf506082b34dccdc2e6ab4af61e8dbac0f12e77 (patch)
tree2e895e8d418ac1218a7427443eab34e8bc9683b8
parentfcec9f409fccb38d7bcff0cbdbc7f27e4a6b05bd (diff)
downloadcatalyst-view-spreadsheet-template-4bf506082b34dccdc2e6ab4af61e8dbac0f12e77.tar.gz
catalyst-view-spreadsheet-template-4bf506082b34dccdc2e6ab4af61e8dbac0f12e77.zip
initial implementation
-rw-r--r--lib/Catalyst/View/Spreadsheet/Template.pm131
1 files changed, 131 insertions, 0 deletions
diff --git a/lib/Catalyst/View/Spreadsheet/Template.pm b/lib/Catalyst/View/Spreadsheet/Template.pm
index e69de29..229264a 100644
--- a/lib/Catalyst/View/Spreadsheet/Template.pm
+++ b/lib/Catalyst/View/Spreadsheet/Template.pm
@@ -0,0 +1,131 @@
+package Catalyst::View::Spreadsheet::Template;
+use Moose;
+use namespace::autoclean;
+
+use Path::Class::File;
+use Try::Tiny;
+
+use Spreadsheet::Template;
+
+extends 'Catalyst::View';
+
+has renderer => (
+ is => 'rw',
+ isa => 'Spreadsheet::Template',
+);
+
+has path => (
+ traits => ['Array'],
+ isa => 'ArrayRef[Path::Class::Dir]',
+ writer => 'set_path',
+ predicate => 'has_path',
+ handles => {
+ path => 'elements',
+ },
+);
+
+has processor_class => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'Spreadsheet::Template::Processor::Xslate',
+);
+
+has writer_class => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'Spreadsheet::Template::Writer::XLSX',
+);
+
+has template_extension => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'json',
+);
+
+has catalyst_var => (
+ is => 'ro',
+ isa => 'Str',
+ default => 'c',
+);
+
+sub ACCEPT_CONTEXT {
+ my $self = shift;
+ my ($c) = @_;
+
+ $self->renderer(
+ Spreadsheet::Template->new(
+ processor_class => $self->processor_class,
+ writer_class => $self->writer_class,
+ )
+ );
+
+ $self->set_path([ $c->path_to('root') ]) unless $self->has_path;
+
+ return $self;
+}
+
+my %content_types = (
+ xlsx => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+ xls => 'application/vnd.ms-excel',
+);
+
+sub process {
+ my $self = shift;
+ my ($c) = @_;
+
+ return try {
+ my $content = $self->render($c);
+ $c->response->content_type($content_types{$self->_extension});
+ $c->response->body($content);
+ 1;
+ }
+ catch {
+ my $error = "Couldn't render template: $_";
+ $c->log->error($error);
+ $c->error($error);
+ 0;
+ };
+}
+
+sub render {
+ my $self = shift;
+ my ($c) = @_;
+
+ $self->renderer->render(
+ scalar($self->template_file($c)->slurp),
+ {
+ %{ $c->stash },
+ $self->catalyst_var => $c,
+ }
+ );
+}
+
+sub template_file {
+ my $self = shift;
+ my ($c) = @_;
+
+ my $file = $c->stash->{template}
+ || $c->action . '.' . $self->template_extension;
+
+ for my $dir ($self->path) {
+ my $full_path = $dir->file($file);
+ if (-e $full_path) {
+ return $full_path;
+ }
+ }
+
+ die "Couldn't find template file $file in " . join(", ", $self->path);
+}
+
+sub _extension {
+ my $self = shift;
+
+ (my $extension = lc($self->writer_class)) =~ s/.*:://;
+
+ return $extension;
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;