| 1 | package App::DuckPAN::TemplateDefinitions; | ||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:DDG'; | |||||||||||
| 3 | # ABSTRACT: Parse the template definitions file to create templates and template sets | ||||||||||||
| 4 | 2 | $App::DuckPAN::TemplateDefinitions::VERSION = '1021'; | |||||||||||
| 5 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Path::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use YAML::XS qw(LoadFile); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 2 | ▶ | use App::DuckPAN::Template; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use App::DuckPAN::TemplateSet; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 2 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | has templates_yml => ( | ||||||||||||
| 17 | is => 'ro', | ||||||||||||
| 18 | required => 1, | ||||||||||||
| 19 | 0 | ▶ | default => sub { path('template', 'templates.yml') }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 20 | 2 | doc => 'Path to the YAML file with template definitions', | |||||||||||
| 21 | ); | ||||||||||||
| 22 | |||||||||||||
| 23 | 2 | has _templates_data => ( | |||||||||||
| 24 | is => 'rwp', | ||||||||||||
| 25 | init_arg => undef, | ||||||||||||
| 26 | doc => 'Raw template definitions read from the template definitions file', | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | 2 | 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 | 1 | ▶ | sub _build__template_map { | ||||||||||
|
Subroutine
_build__template_map: called CC=2 SCAR=6.9
| |||||||||||||
| 39 | 1 | my $self = shift; | |||||||||||
| 40 | 1 | my $template_root = path($self->templates_yml)->parent; | |||||||||||
| 41 | 1 | my $data = $self->_templates_data->{templates}; | |||||||||||
| 42 | 1 | my %template_map; | |||||||||||
| 43 | |||||||||||||
| 44 | 1 | for my $name (keys %$data) { | |||||||||||
| 45 | 7 | 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 | output_file => path($template_data->{output}), | |||||||||||
| 52 | ); | ||||||||||||
| 53 | |||||||||||||
| 54 | 7 | $template_map{$name} = $template; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 1 | return \%template_map; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 2 | 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 | 1 | ▶ | sub _build__template_sets { | ||||||||||
|
Subroutine
_build__template_sets: called CC=7 SCAR=19.5
| |||||||||||||
| 69 | 1 | my $self = shift; | |||||||||||
| 70 | 1 | my $sets_data = $self->_templates_data->{template_sets}; | |||||||||||
| 71 | 1 | my %template_sets; | |||||||||||
| 72 | |||||||||||||
| 73 | 1 | for my $name (keys %$sets_data) { | |||||||||||
| 74 | 5 | my $data = $sets_data->{$name}; | |||||||||||
| 75 | 5 | ▶ | my @required = @{$data->{required} // []}; | ||||||||||
|
Condition100%2/2
$data->{"required"} // []
MC/DC100%1/1
$data->{"required"} // []
$data->{"required"}
Truth table100%2/2
A
A= $data->{"required"}
| |||||||||||||
| 76 | 5 | ▶ | my @optional = @{$data->{optional} // []}; | ||||||||||
|
Condition100%2/2
$data->{"optional"} // []
MC/DC100%1/1
$data->{"optional"} // []
$data->{"optional"}
Truth table100%2/2
A
A= $data->{"optional"}
| |||||||||||||
| 77 | 5 | my $subdir_support = $data->{subdir_support}; | |||||||||||
| 78 | |||||||||||||
| 79 | # check if all templates in this set are defined | ||||||||||||
| 80 | 5 | for my $template_name (@required, @optional) { | |||||||||||
| 81 | die "Template '$template_name' not defined in " . $self->templates_yml | ||||||||||||
| 82 | 11 | ▶ | unless $self->_template_map->{$template_name}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | my $template_set = App::DuckPAN::TemplateSet->new( | ||||||||||||
| 86 | name => $name, | ||||||||||||
| 87 | description => $data->{description}, | ||||||||||||
| 88 | 5 | required_templates => [ @{$self->_template_map}{@required} ], | |||||||||||
| 89 | 5 | ▶ | optional_templates => [ @{$self->_template_map}{@optional} ], | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | defined($subdir_support) ? (subdir_support => $subdir_support) : (), | ||||||||||||
| 91 | ); | ||||||||||||
| 92 | |||||||||||||
| 93 | 5 | $template_sets{$name} = $template_set; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 1 | return \%template_sets; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | # Get a template set by name | ||||||||||||
| 100 | 0 | ▶ | sub get_template_set { | ||||||||||
|
Subroutine
get_template_set: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 101 | 0 | my ($self, $name) = @_; | |||||||||||
| 102 | |||||||||||||
| 103 | 0 | return $self->_template_sets->{$name}; | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | # Get all available template set names | ||||||||||||
| 107 | 1 | ▶ | sub get_template_sets { | ||||||||||
|
Subroutine
get_template_sets: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 108 | 1 | my ($self) = @_; | |||||||||||
| 109 | |||||||||||||
| 110 | 1 | return values %{$self->_template_sets}; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | # Get a list of all templates | ||||||||||||
| 114 | 1 | ▶ | sub get_templates { | ||||||||||
|
Subroutine
get_templates: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 115 | 1 | my ($self) = @_; | |||||||||||
| 116 | |||||||||||||
| 117 | 1 | return values %{$self->_template_map}; | |||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | 2 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 121 | 2 | my $self = shift; | |||||||||||
| 122 | |||||||||||||
| 123 | 2 | ▶ | try { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 124 | 2 | $self->_set__templates_data(LoadFile($self->templates_yml)); | |||||||||||
| 125 | 1 | ▶ | } catch { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 126 | 1 | die "Error loading template definitions file " . $self->templates_yml . ": $_"; | |||||||||||
| 127 | 2 | }; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 2 | 1; | |||||||||||
| 131 | |||||||||||||
| 132 | __END__ | ||||||||||||