File Coverage

blib/lib/Data/Tubes/Plugin/Renderer.pm
Criterion Covered Total %
statement 82 83 98.8
branch 26 30 86.6
condition 18 28 64.2
subroutine 17 17 100.0
pod 1 1 100.0
total 144 159 90.5


line stmt bran cond sub pod time code
1             package Data::Tubes::Plugin::Renderer;
2 6     6   2105 use strict;
  6         13  
  6         202  
3 6     6   32 use warnings;
  6         13  
  6         183  
4 6     6   31 use English qw< -no_match_vars >;
  6         11  
  6         44  
5             our $VERSION = '0.738';
6              
7 6     6   2670 use Log::Log4perl::Tiny qw< :easy :dead_if_first >;
  6         19  
  6         69  
8              
9 6     6   2226 use Data::Tubes::Util qw< normalize_args shorter_sub_names >;
  6         14  
  6         349  
10 6     6   35 use Data::Tubes::Util qw< read_file_maybe >;
  6         12  
  6         6734  
11             my %global_defaults = (
12             input => 'structured',
13             output => 'rendered',
14             );
15              
16             sub _resolve_template {
17 27     27   48 my $args = shift;
18 27         107 my $template = read_file_maybe($args->{template});
19 27 50       198 $template = read_file_maybe($template->($args))
20             if ref($template) eq 'CODE';
21 27 50       80 LOGDIE 'undefined template' unless defined $template;
22 27 100       122 $template = $args->{template_perlish}->compile($template)
23             unless ref $template;
24 27 50       34471 return $template if ref($template) eq 'HASH';
25 0         0 LOGDIE 'invalid template of type ' . ref($template);
26             } ## end sub _resolve_template
27              
28             sub _create_tp {
29 19     19   40 my $args = shift;
30 19         1701 require Template::Perlish;
31             return Template::Perlish->new(
32 57         211 map { $_ => $args->{$_} }
33 19         8318 grep { defined $args->{$_} } qw< start stop variables >
  57         156  
34             );
35             } ## end sub _create_tp
36              
37             sub _rwtp_ntp_nt {
38 12     12   25 my $args = shift;
39 12         26 my $input = $args->{input};
40 12         28 my $output = $args->{output};
41 12         22 my $tp = $args->{template_perlish};
42 12   33     45 my $template = _resolve_template($args) // LOGDIE 'undefined template';
43             return sub {
44 12     12   98 my $record = shift;
45             $record->{$output} =
46 12   50     77 $tp->evaluate($template, $record->{$input} // {});
47 12         23224 return $record;
48 12         108 };
49             } ## end sub _rwtp_ntp_nt
50              
51             sub _rwtp_ntp_t {
52 2     2   4 my $args = shift;
53 2         4 my $itf = $args->{template_input};
54 2         4 my $input = $args->{input};
55 2         3 my $output = $args->{output};
56 2         3 my $tp = $args->{template_perlish};
57             my $ctmpl =
58 2 100       6 defined($args->{template}) ? _resolve_template($args) : undef;
59             return sub {
60 4     4   2653 my $record = shift;
61             my $template =
62             defined($record->{$itf})
63             ? _resolve_template(
64             {
65             template_perlish => $tp,
66 4 100 100     26 template => $record->{$itf}
67             }
68             )
69             : ($ctmpl
70             // die {message => 'undefined template', record => $record});
71             $record->{$output} =
72 3   50     19 $tp->evaluate($template, $record->{$input} // {});
73 3         13374 return $record;
74 2         11 };
75             } ## end sub _rwtp_ntp_t
76              
77             sub _rwtp_tp_nt {
78 3     3   6 my $args = shift;
79 3         5 my $itpf = $args->{template_perlish_input};
80 3         5 my $input = $args->{input};
81 3         4 my $output = $args->{output};
82 3         4 my $ctp = $args->{template_perlish};
83 3   66     11 my $ctmpl = $args->{template} // LOGDIE 'undefined template';
84 2 50       5 my $pctmpl = _resolve_template($args) if defined $ctmpl;
85             return sub {
86 2     2   16 my $record = shift;
87 2   66     15 my $tp = $record->{$itpf} // $ctp;
88             my $template =
89 2 100       9 defined($record->{$itpf})
90             ? _resolve_template({template_perlish => $tp, template => $ctmpl})
91             : $pctmpl;
92             $record->{$output} =
93 2   50     10 $tp->evaluate($template, $record->{$input} // {});
94 2         13097 return $record;
95 2         12 };
96             } ## end sub _rwtp_tp_nt
97              
98             sub _rwtp_tp_t {
99 8     8   15 my $args = shift;
100 8         14 my $itpf = $args->{template_perlish_input};
101 8         11 my $itf = $args->{template_input};
102 8         14 my $input = $args->{input};
103 8         9 my $output = $args->{output};
104 8         12 my $ctp = $args->{template_perlish};
105 8         12 my $ctmpl = $args->{template};
106 8 100       18 my $pctmpl = defined($ctmpl) ? _resolve_template($args) : undef;
107             return sub {
108 8     8   132 my $record = shift;
109 8   66     37 my $tp = $record->{$itpf} // $ctp;
110             my $template =
111             defined($record->{$itf}) ? _resolve_template(
112             {
113             template_perlish => $tp,
114             template => $record->{$itf}
115             }
116             )
117             : (!defined($ctmpl))
118             ? die({message => 'undefined template', record => $record})
119 8 100       45 : defined($record->{$itpf})
    100          
    100          
120             ? _resolve_template({template_perlish => $tp, template => $ctmpl})
121             : $pctmpl;
122             $record->{$output} =
123 6   50     65 $tp->evaluate($template, $record->{$input} // {});
124 6         15286 return $record;
125 8         45 };
126             } ## end sub _rwtp_tp_t
127              
128             sub render_with_template_perlish {
129 25     25 1 36753 my %args = normalize_args(
130             @_,
131             [
132             {
133             %global_defaults,
134             start => '[%',
135             stop => '%]',
136             variables => {},
137             name => 'render with Template::Perlish',
138             },
139             'template'
140             ]
141             );
142 25         126 my $name = $args{name};
143              
144 25   66     143 $args{template_perlish} //= _create_tp(\%args);
145              
146 25         1002 my $tpi = defined $args{template_perlish_input};
147 25         57 my $ti = defined $args{template_input};
148             return
149 25 100 100     161 ($tpi && $ti) ? _rwtp_tp_t(\%args)
    100          
    100          
150             : $tpi ? _rwtp_tp_nt(\%args)
151             : $ti ? _rwtp_ntp_t(\%args)
152             : _rwtp_ntp_nt(\%args);
153             } ## end sub render_with_template_perlish
154              
155             shorter_sub_names(__PACKAGE__, 'render_');
156              
157             1;