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   2289 use strict;
  6         11  
  6         183  
3 6     6   29 use warnings;
  6         11  
  6         158  
4 6     6   25 use English qw< -no_match_vars >;
  6         10  
  6         36  
5             our $VERSION = '0.737';
6              
7 6     6   2257 use Log::Log4perl::Tiny qw< :easy :dead_if_first >;
  6         18  
  6         65  
8              
9 6     6   1956 use Data::Tubes::Util qw< normalize_args shorter_sub_names >;
  6         12  
  6         333  
10 6     6   33 use Data::Tubes::Util qw< read_file_maybe >;
  6         11  
  6         5813  
11             my %global_defaults = (
12             input => 'structured',
13             output => 'rendered',
14             );
15              
16             sub _resolve_template {
17 27     27   41 my $args = shift;
18 27         73 my $template = read_file_maybe($args->{template});
19 27 50       169 $template = read_file_maybe($template->($args))
20             if ref($template) eq 'CODE';
21 27 50       57 LOGDIE 'undefined template' unless defined $template;
22 27 100       118 $template = $args->{template_perlish}->compile($template)
23             unless ref $template;
24 27 50       28128 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   28 my $args = shift;
30 19         968 require Template::Perlish;
31             return Template::Perlish->new(
32 57         151 map { $_ => $args->{$_} }
33 19         7168 grep { defined $args->{$_} } qw< start stop variables >
  57         127  
34             );
35             } ## end sub _create_tp
36              
37             sub _rwtp_ntp_nt {
38 12     12   16 my $args = shift;
39 12         18 my $input = $args->{input};
40 12         19 my $output = $args->{output};
41 12         16 my $tp = $args->{template_perlish};
42 12   33     24 my $template = _resolve_template($args) // LOGDIE 'undefined template';
43             return sub {
44 12     12   72 my $record = shift;
45             $record->{$output} =
46 12   50     45 $tp->evaluate($template, $record->{$input} // {});
47 12         17407 return $record;
48 12         67 };
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         4 my $tp = $args->{template_perlish};
57             my $ctmpl =
58 2 100       5 defined($args->{template}) ? _resolve_template($args) : undef;
59             return sub {
60 4     4   2832 my $record = shift;
61             my $template =
62             defined($record->{$itf})
63             ? _resolve_template(
64             {
65             template_perlish => $tp,
66 4 100 100     23 template => $record->{$itf}
67             }
68             )
69             : ($ctmpl
70             // die {message => 'undefined template', record => $record});
71             $record->{$output} =
72 3   50     15 $tp->evaluate($template, $record->{$input} // {});
73 3         11169 return $record;
74 2         11 };
75             } ## end sub _rwtp_ntp_t
76              
77             sub _rwtp_tp_nt {
78 3     3   5 my $args = shift;
79 3         5 my $itpf = $args->{template_perlish_input};
80 3         4 my $input = $args->{input};
81 3         5 my $output = $args->{output};
82 3         4 my $ctp = $args->{template_perlish};
83 3   66     12 my $ctmpl = $args->{template} // LOGDIE 'undefined template';
84 2 50       7 my $pctmpl = _resolve_template($args) if defined $ctmpl;
85             return sub {
86 2     2   14 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     12 $tp->evaluate($template, $record->{$input} // {});
94 2         11696 return $record;
95 2         12 };
96             } ## end sub _rwtp_tp_nt
97              
98             sub _rwtp_tp_t {
99 8     8   12 my $args = shift;
100 8         12 my $itpf = $args->{template_perlish_input};
101 8         11 my $itf = $args->{template_input};
102 8         11 my $input = $args->{input};
103 8         11 my $output = $args->{output};
104 8         8 my $ctp = $args->{template_perlish};
105 8         12 my $ctmpl = $args->{template};
106 8 100       16 my $pctmpl = defined($ctmpl) ? _resolve_template($args) : undef;
107             return sub {
108 8     8   119 my $record = shift;
109 8   66     32 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       38 : defined($record->{$itpf})
    100          
    100          
120             ? _resolve_template({template_perlish => $tp, template => $ctmpl})
121             : $pctmpl;
122             $record->{$output} =
123 6   50     58 $tp->evaluate($template, $record->{$input} // {});
124 6         12947 return $record;
125 8         38 };
126             } ## end sub _rwtp_tp_t
127              
128             sub render_with_template_perlish {
129 25     25 1 55515 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         95 my $name = $args{name};
143              
144 25   66     117 $args{template_perlish} //= _create_tp(\%args);
145              
146 25         754 my $tpi = defined $args{template_perlish_input};
147 25         48 my $ti = defined $args{template_input};
148             return
149 25 100 100     120 ($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;