summaryrefslogtreecommitdiffstats
path: root/lib/Spreadsheet/Template/Generator.pm
blob: c751e9974b190f7e5440c3c11d2d49b83bae78fa (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
package Spreadsheet::Template::Generator;
use Moose;
# ABSTRACT: create new templates from existing spreadsheets

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

has parser_class => (
    is      => 'ro',
    isa     => 'Str',
    default => 'Spreadsheet::Template::Generator::Parser::XLSX',
);

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

has parser => (
    is   => 'ro',
    does => 'Spreadsheet::Template::Generator::Parser',
    lazy => 1,
    default => sub {
        my $self = shift;
        my $class = $self->parser_class;
        load_class($class);
        return $class->new(
            %{ $self->parser_options }
        );
    },
);

sub generate {
    my $self = shift;
    my ($filename) = @_;
    my $data = $self->parser->parse($filename);
    return JSON->new->pretty->canonical->encode($data);
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;