App-DuckPAN
view release on metacpan or search on metacpan
lib/App/DuckPAN/TemplateSet.pm view on Meta::CPAN
package App::DuckPAN::TemplateSet;
our $AUTHORITY = 'cpan:DDG';
# ABSTRACT: Set of templates an Instant Answer
$App::DuckPAN::TemplateSet::VERSION = '1021';
# A group of templates is a template set. Conceptually this represents a
# sub-type of an instant answer type. For example, a Goodie can be a standard
# Goodie or a Cheat Sheet goodie, each of which corresponds to a template set.
#
# Each template set can have required and optional templates. 'required' templates
# are always used to generate output files, while the user's confirmation is
# needed before each optional template is processed.
use Moo;
use Try::Tiny;
use List::Util qw(all);
use Algorithm::Combinatorics qw(combinations);
use namespace::clean;
has name => (
is => 'ro',
required => 1,
doc => 'Name of the template set',
);
has description => (
is => 'ro',
required => 1,
doc => 'Description of the template set',
);
has subdir_support => (
is => 'ro',
default => 1,
doc => 'Does this template set support creation of Instant Answers inside sub-directories? ' .
'For example, Cheat Sheet Instant Answers do not support it, while Standard Goodie ' .
'and Spice ones do have support.',
);
has required_templates => (
is => 'ro',
required => 1,
doc => 'Arrayref of App::DuckPAN::Template instances that represent mandatory templates',
);
has optional_templates => (
is => 'ro',
required => 1,
doc => 'Arrayref of App::DuckPAN::Template instances that represent optional templates',
);
has optional_template_combinations => (
is => 'ro',
lazy => 1,
builder => 1,
init_arg => undef,
doc => 'Arrayref of possible optional template combinations, ' .
'which themselves are arrayrefs of templates.',
);
# All possible template combinations are generated from the list in the
# 'optional_templates' attribute. They are sorted by the following rules:
#
# 1. Combinations are sorted by length (ascending)
# 2. In each combination, the templates are sorted in the same order that
# they appear in the 'optional_templates' attribute
# 3. Combinations of the same length are then sorted based on the same rule
sub _build_optional_template_combinations {
my $self = shift;
my @templates = @{$self->optional_templates};
# Map of template -> position in template list
my %template_pos = map { ($templates[$_] => $_) } 0..$#templates;
( run in 0.460 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )