| 1 | package App::DuckPAN::TemplateSet; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:DDG'; | |||||||||
| 3 | # ABSTRACT: Set of templates an Instant Answer | ||||||||||
| 4 | 2 | $App::DuckPAN::TemplateSet::VERSION = '1021'; | |||||||||
| 5 | # A group of templates is a template set. Conceptually this represents a | ||||||||||
| 6 | # sub-type of an instant answer type. For example, a Goodie can be a standard | ||||||||||
| 7 | # Goodie or a Cheat Sheet goodie, each of which corresponds to a template set. | ||||||||||
| 8 | # | ||||||||||
| 9 | # Each template set can have required and optional templates. 'required' templates | ||||||||||
| 10 | # are always used to generate output files, while the user's confirmation is | ||||||||||
| 11 | # needed before each optional template is processed. | ||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 2 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use List::Util qw(all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Algorithm::Combinatorics qw(combinations); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 2 | has name => ( | |||||||||
| 22 | is => 'ro', | ||||||||||
| 23 | required => 1, | ||||||||||
| 24 | doc => 'Name of the template set', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 2 | has description => ( | |||||||||
| 28 | is => 'ro', | ||||||||||
| 29 | required => 1, | ||||||||||
| 30 | doc => 'Description of the template set', | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 2 | has subdir_support => ( | |||||||||
| 34 | is => 'ro', | ||||||||||
| 35 | default => 1, | ||||||||||
| 36 | doc => 'Does this template set support creation of Instant Answers inside sub-directories? ' . | ||||||||||
| 37 | 'For example, Cheat Sheet Instant Answers do not support it, while Standard Goodie ' . | ||||||||||
| 38 | 'and Spice ones do have support.', | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 2 | has required_templates => ( | |||||||||
| 42 | is => 'ro', | ||||||||||
| 43 | required => 1, | ||||||||||
| 44 | doc => 'Arrayref of App::DuckPAN::Template instances that represent mandatory templates', | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | 2 | has optional_templates => ( | |||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | required => 1, | ||||||||||
| 50 | doc => 'Arrayref of App::DuckPAN::Template instances that represent optional templates', | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 2 | has optional_template_combinations => ( | |||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | lazy => 1, | ||||||||||
| 56 | builder => 1, | ||||||||||
| 57 | init_arg => undef, | ||||||||||
| 58 | doc => 'Arrayref of possible optional template combinations, ' . | ||||||||||
| 59 | 'which themselves are arrayrefs of templates.', | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | # All possible template combinations are generated from the list in the | ||||||||||
| 63 | # 'optional_templates' attribute. They are sorted by the following rules: | ||||||||||
| 64 | # | ||||||||||
| 65 | # 1. Combinations are sorted by length (ascending) | ||||||||||
| 66 | # 2. In each combination, the templates are sorted in the same order that | ||||||||||
| 67 | # they appear in the 'optional_templates' attribute | ||||||||||
| 68 | # 3. Combinations of the same length are then sorted based on the same rule | ||||||||||
| 69 | 1 | ▶ | sub _build_optional_template_combinations { | ||||||||
|
Subroutine
_build_optional_template_combinations: called CC=4 SCAR=13.9
| |||||||||||
| 70 | 1 | my $self = shift; | |||||||||
| 71 | 1 | my @templates = @{$self->optional_templates}; | |||||||||
| 72 | |||||||||||
| 73 | # Map of template -> position in template list | ||||||||||
| 74 | 1 | my %template_pos = map { ($templates[$_] => $_) } 0..$#templates; | |||||||||
| 75 | |||||||||||
| 76 | # All combinations of all lengths; sorted | ||||||||||
| 77 | 1 | my @template_combinations; | |||||||||
| 78 | |||||||||||
| 79 | 1 | for my $length (1..@templates) { | |||||||||
| 80 | # Generate all combinations of length $length | ||||||||||
| 81 | 3 | my @combinations = combinations(\@templates, $length); | |||||||||
| 82 | |||||||||||
| 83 | # Sort the tempates in each combination | ||||||||||
| 84 | 3 | for my $combination (@combinations) { | |||||||||
| 85 | 7 | @$combination = sort { $template_pos{$a} <=> $template_pos{$b} } @$combination; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | # Sort the array of combinations | ||||||||||
| 89 | @combinations = sort { | ||||||||||
| 90 | # The comparison function compares two arrayrefs of templates. | ||||||||||
| 91 | # Templates from both the arrayrefs are compared one by one and the | ||||||||||
| 92 | # function returns the value when it finds a difference. | ||||||||||
| 93 | 3 | for my $i (0..$length-1) { | |||||||||
| 94 | 6 | my $cmp = $template_pos{$a->[$i]} <=> $template_pos{$b->[$i]}; | |||||||||
| 95 | |||||||||||
| 96 | 6 | return $cmp; | |||||||||
| 97 | } | ||||||||||
| 98 | } @combinations; | ||||||||||
| 99 | |||||||||||
| 100 | 3 | push @template_combinations, @combinations; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 1 | return \@template_combinations; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # check if all templates in the array @templates are optional | ||||||||||
| 107 | 3 | ▶ | sub _are_templates_optional { | ||||||||
|
Subroutine
_are_templates_optional: called CC=1 SCAR=0.0
| |||||||||||
| 108 | 3 | my ($self, @templates) = @_; | |||||||||
| 109 | 3 | my %optional_templates = map { ($_ => 1) } @{$self->optional_templates}; | |||||||||
| 110 | |||||||||||
| 111 | 3 | ▶ | return all { $optional_templates{$_} } @templates; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | # Use the template to generate necessary files. Takes 2 parameters: | ||||||||||
| 115 | # $vars: variables to substitute in the templates | ||||||||||
| 116 | # $optional_templates: arrayref of template instances taken from $self->optional_templates | ||||||||||
| 117 | 3 | ▶ | sub generate { | ||||||||
|
Subroutine
generate: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 118 | 3 | my ($self, $vars, $optional_templates) = @_; | |||||||||
| 119 | 3 | my @created_files; | |||||||||
| 120 | my $error; | ||||||||||
| 121 | |||||||||||
| 122 | # Verify that $optional_templates has templates from within $self->optional_templates | ||||||||||
| 123 | 3 | ▶ | $self->_are_templates_optional(@$optional_templates) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | or die "Unknown template(s) in \$optional_templates"; | ||||||||||
| 125 | |||||||||||
| 126 | 2 | for my $template (@{$self->required_templates}, @$optional_templates) { | |||||||||
| 127 | 5 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 128 | 5 | push @created_files, $template->generate($vars); | |||||||||
| 129 | 1 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 130 | 1 | $error = $_; | |||||||||
| 131 | 5 | }; | |||||||||
| 132 | |||||||||||
| 133 | 5 | ▶ | last if $error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 2 | return (created_files => [ @created_files ], error => $error); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 2 | 1; | |||||||||
| 140 | |||||||||||
| 141 | __END__ | ||||||||||