File Coverage

blib/lib/Daje/Document/Builder.pm
Criterion Covered Total %
statement 20 54 37.0
branch 0 12 0.0
condition n/a
subroutine 7 9 77.7
pod 1 1 100.0
total 28 76 36.8


line stmt bran cond sub pod time code
1             package Daje::Document::Builder;
2 1     1   156181 use Mojo::Base -base;
  1         14641  
  1         8  
3 1     1   315 use v5.42;
  1         6  
4              
5              
6             # NAME
7             # ====
8             #
9             # Daje::Document::Builder - It's the document builder
10             #
11             # Synopsis
12             # ========
13             #
14             # my $error = Daje::Workflow::Errors::Error->new()
15             #
16             # my $builder = Daje::Document::Builder->new(
17             # source => 'Template class',
18             # data_sections => 'sections',
19             # data => $data,
20             # error => $error
21             # );
22             #
23             # $builder->process();
24             #
25             # if($builder->error->has_error()) {
26             # say $builder->error->error()
27             # } else {
28             # my $documents = $builder->output();
29             # }
30             #
31             #
32             #
33             #
34             # REQUIRES
35             # ========
36             #
37             # use Mojo::Base;
38             # use Template;
39             #
40             # DESCRIPTION
41             # ===========
42             #
43             # Daje::Document::Builder Builds documents based on Template Toolkit
44             #
45             # LICENSE
46             # =======
47             #
48             # Copyright (C) janeskil1525.
49             #
50             # This library is free software; you can redistribute it and/or modify
51             # it under the same terms as Perl itself.
52             #
53             # AUTHOR
54             # ======
55             #
56             # janeskil1525 Ejaneskil1525@gmail.comE
57             #
58             #
59              
60 1     1   886 use Template;
  1         30609  
  1         68  
61 1     1   666 use Mojo::Loader qw {load_class};
  1         298105  
  1         110  
62 1     1   627 use Class::Load qw(is_class_loaded);
  1         26662  
  1         117  
63 1     1   2882 use Daje::Document::Load::Template;
  1         4  
  1         7  
64 1     1   59 use Data::Dumper;
  1         2  
  1         944  
65              
66             our $VERSION = "0.15";
67              
68             has 'data';
69             has 'source';
70             has 'template';
71             has 'data_sections';
72             has 'error';
73             has 'output';
74              
75 0     0 1   sub process($self) {
  0            
  0            
76 0           my $templates = Daje::Document::Load::Template->new(
77             source => $self->source(),
78             error => $self->error(),
79             data_sections => $self->data_sections(),
80             );
81 0           $templates->load_templates();
82              
83 0 0         unless ($templates->error->has_error()) {
84 0           try {
85 0           my $documents;
86 0           $self->_prepare_data();
87 0           my $tt = Template->new();
88 0           my @data_sec = split(',', $self->data_sections) ;
89 0           my $length = scalar @data_sec;
90 0           for (my $i = 0; $i < $length; $i++) {
91 0           my $output = "";
92 0           $tt->process(\$templates->{template}->{data_sec}->{$data_sec[$i]}, $self->data(), \$output);
93 0 0         say $tt->error if $tt->error();
94 0 0         $self->error->add_error($tt->error) if (defined $tt->error);
95 0           my $section->{name} = $data_sec[$i];
96 0           $section->{document} = $output;
97 0           push @{$documents}, $section;
  0            
98             }
99 0 0         $self->error->add_error($tt->error) if (defined $tt->error);
100 0 0         say $tt->error if (defined $tt->error);
101 0           $self->output($documents);
102             } catch($e) {
103 0           say $e;
104 0           $self->error->add_error($e);
105             }
106             } else {
107 0           $self->error->add_error($templates->error->error());
108             }
109 0           return 1;
110             }
111              
112 0     0     sub _prepare_data($self) {
  0            
  0            
113 0 0         if(!is_class_loaded($self->source())) {
114 0           my $c = load_class($self->source());
115             }
116 0           my $temp = $self->source()->new()->add_subs($self->data);
117             }
118              
119             1;
120             __END__