File Coverage

blib/lib/App/DuckPAN/TemplateDefinitions.pm
Criterion Covered Total %
statement 57 59 96.6
branch 3 4 75.0
condition 4 4 100.0
subroutine 14 15 93.3
pod 0 4 0.0
total 78 86 90.7


line stmt bran cond sub pod time code
1             package App::DuckPAN::TemplateDefinitions;
2             our $AUTHORITY = 'cpan:DDG';
3             # ABSTRACT: Parse the template definitions file to create templates and template sets
4             $App::DuckPAN::TemplateDefinitions::VERSION = '1019';
5 2     2   66513 use Moo;
  2         8677  
  2         9  
6              
7 2     2   1890 use Try::Tiny;
  2         1042  
  2         85  
8 2     2   12 use Path::Tiny;
  2         4  
  2         82  
9 2     2   748 use YAML::XS qw(LoadFile);
  2         4013  
  2         90  
10              
11 2     2   642 use App::DuckPAN::Template;
  2         7  
  2         51  
12 2     2   685 use App::DuckPAN::TemplateSet;
  2         6  
  2         50  
13              
14 2     2   12 use namespace::clean;
  2         4  
  2         8  
15              
16             has templates_yml => (
17             is => 'ro',
18             required => 1,
19             default => sub { path('template', 'templates.yml') },
20             doc => 'Path to the YAML file with template definitions',
21             );
22              
23             has _templates_data => (
24             is => 'rwp',
25             init_arg => undef,
26             doc => 'Raw template definitions read from the template definitions file',
27             );
28              
29             has _template_map => (
30             is => 'ro',
31             lazy => 1,
32             builder => 1,
33             init_arg => undef,
34             doc => 'Hashref of tempate name => App::DuckPAN::Template instances ' .
35             'built from the templates_yml file',
36             );
37              
38             sub _build__template_map {
39 1     1   10 my $self = shift;
40 1         5 my $template_root = path($self->templates_yml)->parent;
41 1         135 my $data = $self->_templates_data->{templates};
42 1         2 my %template_map;
43              
44 1         5 for my $name (keys %$data) {
45 7         14 my $template_data = $data->{$name};
46              
47             my $template = App::DuckPAN::Template->new(
48             name => $name,
49             label => $template_data->{label},
50             input_file => path($template_root, $template_data->{input}),
51 7         20 output_file => path($template_data->{output}),
52             );
53              
54 7         1787 $template_map{$name} = $template;
55             }
56              
57 1         7 return \%template_map;
58             }
59              
60             has _template_sets => (
61             is => 'ro',
62             builder => 1,
63             lazy => 1,
64             doc => 'hashref of template set name to App::DuckPAN::TemplateSet instances ' .
65             'built from the templates_yml file',
66             );
67              
68             sub _build__template_sets {
69 1     1   10 my $self = shift;
70 1         4 my $sets_data = $self->_templates_data->{template_sets};
71 1         3 my %template_sets;
72              
73 1         4 for my $name (keys %$sets_data) {
74 5         9 my $data = $sets_data->{$name};
75 5   100     9 my @required = @{$data->{required} // []};
  5         20  
76 5   100     7 my @optional = @{$data->{optional} // []};
  5         25  
77 5         10 my $subdir_support = $data->{subdir_support};
78              
79             # check if all templates in this set are defined
80 5         11 for my $template_name (@required, @optional) {
81             die "Template '$template_name' not defined in " . $self->templates_yml
82 11 50       180 unless $self->_template_map->{$template_name};
83             }
84              
85             my $template_set = App::DuckPAN::TemplateSet->new(
86             name => $name,
87             description => $data->{description},
88 5         62 required_templates => [ @{$self->_template_map}{@required} ],
89 5 100       41 optional_templates => [ @{$self->_template_map}{@optional} ],
  5         89  
90             defined($subdir_support) ? (subdir_support => $subdir_support) : (),
91             );
92              
93 5         1431 $template_sets{$name} = $template_set;
94             }
95              
96 1         6 return \%template_sets;
97             }
98              
99             # Get a template set by name
100             sub get_template_set {
101 0     0 0 0 my ($self, $name) = @_;
102              
103 0         0 return $self->_template_sets->{$name};
104             }
105              
106             # Get all available template set names
107             sub get_template_sets {
108 1     1 0 3722 my ($self) = @_;
109              
110 1         2 return values %{$self->_template_sets};
  1         22  
111             }
112              
113             # Get a list of all templates
114             sub get_templates {
115 1     1 0 970 my ($self) = @_;
116              
117 1         3 return values %{$self->_template_map};
  1         22  
118             }
119              
120             sub BUILD {
121 2     2 0 5034 my $self = shift;
122              
123             try {
124 2     2   190 $self->_set__templates_data(LoadFile($self->templates_yml));
125             } catch {
126 1     1   113 die "Error loading template definitions file " . $self->templates_yml . ": $_";
127 2         21 };
128             }
129              
130             1;
131              
132             __END__
133              
134             =pod
135              
136             =head1 NAME
137              
138             App::DuckPAN::TemplateDefinitions - Parse the template definitions file to create templates and template sets
139              
140             =head1 VERSION
141              
142             version 1019
143              
144             =head1 AUTHOR
145              
146             DuckDuckGo <open@duckduckgo.com>, Zach Thompson <zach@duckduckgo.com>, Zaahir Moolla <moollaza@duckduckgo.com>, Torsten Raudssus <torsten@raudss.us> L<https://raudss.us/>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is Copyright (c) 2013 by DuckDuckGo, Inc. L<https://duckduckgo.com/>.
151              
152             This is free software, licensed under:
153              
154             The Apache License, Version 2.0, January 2004
155              
156             =cut