summaryrefslogtreecommitdiffstats
path: root/lib/Spreadsheet/Template.pm
blob: 21e432c6232429175bed99b351fa5498d8036b66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package Spreadsheet::Template;
use Moose;
# ABSTRACT: generate spreadsheets from a template

use Class::Load 'load_class';
use JSON;

has processor_class => (
    is      => 'ro',
    isa     => 'Str',
    default => 'Spreadsheet::Template::Processor::Xslate',
);

has processor_options => (
    is      => 'ro',
    isa     => 'HashRef',
    default => sub { {} },
);

has writer_class => (
    is      => 'ro',
    isa     => 'Str',
    default => 'Spreadsheet::Template::Writer::XLSX',
);

has writer_options => (
    is      => 'ro',
    isa     => 'HashRef',
    default => sub { {} },
);

has processor => (
    is      => 'ro',
    does    => 'Spreadsheet::Template::Processor',
    handles => 'Spreadsheet::Template::Processor',
    lazy    => 1,
    default => sub {
        my $self = shift;
        my $class = $self->processor_class;
        load_class($class);
        return $class->new($self->processor_options);
    },
);

has writer => (
    is      => 'ro',
    does    => 'Spreadsheet::Template::Writer',
    handles => 'Spreadsheet::Template::Writer',
    lazy    => 1,
    default => sub {
        my $self = shift;
        my $class = $self->writer_class;
        load_class($class);
        return $class->new($self->writer_options);
    },
);

sub render {
    my $self = shift;
    my ($template, $vars) = @_;
    my $contents = $self->process($template, $vars);
    # not decode_json, since we expect that we are already being handed a
    # character string (decode_json also decodes utf8)
    my $data = from_json($contents);
    return $self->write($data);
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;