File Coverage

blib/lib/Data/Record/Serialize/Encode/html.pm
Criterion Covered Total %
statement 35 35 100.0
branch 7 14 50.0
condition 6 17 35.2
subroutine 11 11 100.0
pod 0 4 0.0
total 59 81 72.8


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Encode::html;
2              
3             # ABSTRACT: encode a record as html
4              
5 2     2   1088695 use v5.10;
  2         9  
6              
7 2     2   680 use Moo::Role;
  2         20184  
  2         21  
8              
9             our $VERSION = '0.02';
10              
11 2     2   1132 use warnings::register;
  2         7  
  2         119  
12              
13 2     2   679 use namespace::clean;
  2         20528  
  2         16  
14              
15             has _html => (
16             is => 'lazy',
17             init_arg => undef,
18             builder => sub {
19 1     1   644 require HTML::Tiny;
20 1         3023 HTML::Tiny->new( mode => 'html' );
21             },
22             );
23              
24             has _closed => (
25             is => 'rwp',
26             init_arg => undef,
27             default => 1,
28             );
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54              
55              
56             has [ 'table_class', 'thead_class', 'tbody_class', 'td_class', 'tr_class', 'th_class' ] => (
57             is => 'ro',
58             predicate => 1,
59             );
60              
61             has _class => (
62             is => 'lazy',
63             init_args => undef,
64             builder => sub {
65 1     1   11 my $self = shift;
66             {
67 1 50       40 table => $self->has_table_class ? { class => $self->table_class } : undef,
    50          
    50          
    50          
    50          
68             thead => $self->has_thead_class ? { class => $self->thead_class } : undef,
69             th => $self->has_th_class ? { class => $self->th_class } : undef,
70             td => $self->has_td_class ? { class => $self->td_class } : undef,
71             tr => $self->has_tr_class ? { class => $self->tr_class } : undef,
72             };
73             },
74             );
75              
76 1     1   599 sub _needs_eol { 0 }
77              
78             sub encode {
79 1     1 0 6 my $self = shift;
80             $self->_html->tr( [
81             $self->_html->td(
82             $self->_class->{td} // (),
83 1   33     18 map { $_ // q{} } @{ $_[0] }{ @{ $self->output_fields } } ) ] );
  2   50     8  
  1         9  
  1         47  
84             }
85              
86             sub setup {
87 1     1 0 42709 my $self = shift;
88 1         20 my $html = $self->_html;
89 1   33     87 $self->say( $html->open( 'table', $self->_class->{table} // () ) );
90              
91             $self->say(
92             $html->thead( [
93             $html->tr(
94             $self->_class->{'tr'} // (),
95 1   33     78 [ $html->th( $self->_class->{th} // (), @{ $self->output_fields } ) ],
  1   33     48  
96             ),
97             ],
98             ),
99             );
100 1   33     351 $self->say( $html->open( 'tbody', $self->_class->{tbody} // () ) );
101 1         34 $self->_set__closed( 0 );
102             }
103              
104             sub finalize {
105 1     1 0 263 my $self = shift;
106 1 50       7 return if $self->_closed;
107 1         18 $self->say( $self->_html->close( 'tbody' ) );
108 1         29 $self->say( $self->_html->close( 'table' ) );
109 1         32 $self->_set__closed( 1 );
110             }
111              
112              
113             sub DEMOLISH {
114 1     1 0 9095 my $self = shift;
115              
116 1 50       30 warnings::warnif( 'Data::Record::Serialize::Encode::html',
117             __PACKAGE__ . ': html table is not closed' )
118             unless $self->_closed;
119             }
120              
121             with 'Data::Record::Serialize::Role::Encode';
122              
123             1;
124              
125             #
126             # This file is part of Data-Record-Serialize-Encode-html
127             #
128             # This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory.
129             #
130             # This is free software, licensed under:
131             #
132             # The GNU General Public License, Version 3, June 2007
133             #
134              
135             __END__