File Coverage

blib/lib/HTML/Blitz/Template.pm
Criterion Covered Total %
statement 85 87 97.7
branch 31 62 50.0
condition 6 15 40.0
subroutine 14 14 100.0
pod 7 8 87.5
total 143 186 76.8


line stmt bran cond sub pod time code
1             package HTML::Blitz::Template 0.1001;
2 11     11   83 use HTML::Blitz::pragma;
  11         24  
  11         106  
3 11     11   16790 use HTML::Blitz::CodeGen ();
  11         49  
  11         599  
4 11     11   103 use Carp qw(croak);
  11         22  
  11         1010  
5             use constant {
6 11         2918 _REPR_VERSION => 0,
7 11     11   79 };
  11         23  
8              
9 240 50 33 240 0 749 method new($class: :$_codegen) {
  240 50       1478  
  240 50       486  
  240 50       941  
  240         896  
  240         1912  
  240         356  
10 240         2569 bless {
11             _codegen => $_codegen,
12             }, $class
13             }
14              
15 7 50   7   28 method _codegen() {
  7 50       21  
  7         17  
  7         13  
16             $self->{_codegen}
17 7         175 }
18              
19 2 50   2 1 20729 method FREEZE($model) {
  2 50       8  
  2         8  
  2         9  
  2         4  
20 2         27 _REPR_VERSION, $self->{_codegen}->FREEZE($model)
21             }
22              
23 2 50   2 1 2640 method THAW($class: $model, $repr_version, @components) {
  2         6  
  2         26  
  2         5  
24 2         3 our $VERSION;
25 2 50       10 $repr_version <= _REPR_VERSION
26             or croak "Cannot deserialize data format $repr_version with $class v$VERSION, which only supports data format " . _REPR_VERSION;
27 2         23 my $codegen = HTML::Blitz::CodeGen->THAW($model, @components);
28 2         12 $class->new(_codegen => $codegen)
29             }
30              
31 239 50   239   675 method _perl_src() {
  239 50       640  
  239         419  
  239         359  
32             $self->{_perl_src} //= $self->{_codegen}->assemble(
33 239   66     4653 data_format => 'sigil',
34             data_format_mapping => {
35             array => '',
36             bool => '',
37             func => '',
38             html => '',
39             str => '',
40             },
41             )
42             }
43              
44 1 50   1 1 12 method compile_to_string() {
  1 50       5  
  1         3  
  1         2  
45 1         5 $self->_perl_src
46             }
47              
48 2 50   2 1 1285 method compile_to_fh($fh, $name = $fh) {
  2 50       8  
  2 100       5  
  2         10  
  2         4  
49 2 50       7 print $fh $self->_perl_src
50             or croak "Can't write to $name: $!";
51             }
52              
53 1 50 33 1 1 1811 method compile_to_file($file, :$do_sync = undef) {
  1 50       5  
  1 50       3  
  1         4  
  1         3  
  1         6  
  1         3  
54 1 50       120 open my $fh, '>:encoding(UTF-8)', $file
55             or croak "Can't open $file: $!";
56 1         106 $self->compile_to_fh($fh, $file);
57 1 50       4 if ($do_sync) {
58 0 0       0 $fh->flush
59             or croak "Can't flush $file: $!";
60 0 0       0 $fh->sync
61             or croak "Can't sync $file: $!";
62             }
63 1 50       239 close $fh
64             or croak "Can't close $file: $!";
65             }
66              
67 236 50   236 1 3547 method compile_to_sub() {
  236 50       636  
  236         496  
  236         350  
68 236   33     2267 $self->{_sub} //= do {
69 236         429 my $err;
70 236         794 my $src = $self->_perl_src;
71 236         8075 my $fn;
72             {
73 236         519 local $@;
  236         401  
74 236         51244 $fn = eval $src;
75 236         108201 $err = $@;
76             }
77 236   33     2837 $fn // croak $err
78             }
79             }
80              
81 233 50   233 1 7364 method process($data = {}) {
  233 50       624  
  233 100       419  
  233         3317  
  233         373  
82 233         873 $self->compile_to_sub->($data)
83             }
84              
85             1
86             __END__