File Coverage

blib/lib/HTML/TableContent/Template.pm
Criterion Covered Total %
statement 137 139 98.5
branch 12 14 85.7
condition 3 3 100.0
subroutine 44 45 97.7
pod n/a
total 196 201 97.5


line stmt bran cond sub pod time code
1             package HTML::TableContent::Template;
2              
3 3     3   304149 use strict;
  3         11  
  2         78  
4 2     2   10 use warnings;
  3         9  
  3         111  
5 3     3   14 use Carp qw/croak/;
  3         6  
  3         98  
6 3     3   1012 use MooX::ReturnModifiers;
  3         1510  
  3         117  
7 2     3   1010 use HTML::TableContent::Table;
  2         8  
  2         67  
8 2     3   13 use HTML::TableContent::Table::Caption;
  2         4  
  2         37  
9 2     2   8 use HTML::TableContent::Table::Header;
  2         5  
  2         242  
10              
11             our $VERSION = '1.01';
12              
13             my %TABLE = (
14             caption => 'HTML::TableContent::Table::Caption',
15             header => 'HTML::TableContent::Table::Header',
16             row => 'HTML::TableContent::Table::Row',
17             cell => 'HTML::TableContent::Table::Row::Cell',
18             );
19              
20             sub import {
21 45     45   75983 my ( $self, @import ) = @_;
22            
23 45         124 my $target = caller;
24            
25 45         175 my %modifiers = return_modifiers($target);
26              
27 45         3239 my @target_isa;
28              
29 45 50       386 return if $target->can('_table');
30              
31 2     2   10 { no strict 'refs'; @target_isa = @{"${target}::ISA"} };
  2         4  
  2         1130  
  45         87  
  45         77  
  45         205  
32              
33 45 50       113 if (@target_isa) {
34 45     2   11504 eval '{
  2     2   10  
  2     2   10  
  2     2   8  
  2     2   9  
  2     2   11  
  2     3   11  
  2     2   5  
  2     2   5  
  2     2   8  
  2     2   11  
  2     2   6  
  2     2   8  
  3     2   11  
  3     2   11  
  2     2   6  
  2     2   8  
  2     2   6  
  2     2   8  
  2     3   8  
  2     2   8  
  2     2   9  
  2     2   9  
  2     2   6  
  2     2   8  
  2     2   9  
  2     2   11  
  2         8  
  2         154  
  2         8  
  2         25  
  2         8  
  2         11  
  2         9  
  2         9  
  2         7  
  2         10  
  2         7  
  2         10  
  3         13  
  3         12  
  2         8  
  2         7  
  2         9  
  2         11  
  2         9  
  2         11  
  2         10  
  2         11  
  2         9  
  2         13  
  2         7  
  2         8  
  2         8  
  2         8  
35             package ' . $target . ';
36              
37             sub _caption_spec {
38             my ($class, @meta) = @_;
39             return $class->maybe::next::method(@meta);
40             }
41              
42             sub _header_spec {
43             my ($class, @meta) = @_;
44             return $class->maybe::next::method(@meta);
45             }
46            
47             sub _row_spec {
48             my ($class, @meta) = @_;
49             return $class->maybe::next::method(@meta);
50             }
51            
52             sub _cell_spec {
53             my ($class, @meta) = @_;
54             return $class->maybe::next::method(@meta);
55             }
56            
57             1;
58             }';
59             }
60              
61 45     1   400 $modifiers{has}->( 'package' => ( is => 'ro', lazy => 1, default => sub { return $target; } ) );
  0         0  
62              
63             my $apply_modifiers = sub {
64 45     46   161 $modifiers{with}->('HTML::TableContent::Template::Base');
65 45         64261 };
66              
67 45         195 for my $element (keys %TABLE) {
68 180         311 my @element = ();
69             my $option = sub {
70 217     218   1604 my ( $name, %attributes ) = @_;
71            
72 217         444 my $element_data = { };
73 217         731 my %filtered_attributes = _filter_attributes($name, $element, %attributes);
74              
75 217 100       1385 if ( $element =~ m{caption|header}ixms ){
76 180         866 $modifiers{has}->( $name => %filtered_attributes );
77             }
78              
79 217         174542 delete $filtered_attributes{default};
80              
81 217         610 $element_data->{$name} = \%filtered_attributes;
82            
83 217         553 my $spec = sprintf('_%s_spec', $element);
84 217 100       1201 if ( $element =~ m{row|cell}ixms ) {
85             $modifiers{around}->(
86             $spec => sub {
87 36     37   783 my ( $orig, $self ) = ( shift, shift );
88 36         975 return $self->$orig(@_), %$element_data;
89             }
90 37         229 );
91             } else {
92 180         411 push @element, $element_data;
93             $modifiers{around}->(
94             $spec => sub {
95 180     181   3326 my ( $orig, $self ) = ( shift, shift );
96 180         4532 return $self->$orig(@_), \@element;
97             }
98 180         1194 );
99             }
100 217         84670 return;
101 180         773 };
102              
103 2     2   14 { no strict 'refs'; *{"${target}::$element"} = $option; }
  2         4  
  2         811  
  180         259  
  180         250  
  180         840  
104             }
105              
106 45         121 $apply_modifiers->();
107              
108 45         59715 return;
109             }
110              
111             sub _filter_attributes {
112 218     218   680 my ($name, $element, %attributes) = @_;
113              
114 218         548 $attributes{template_attr} = $name;
115              
116 218 100       696 if ( ! exists $attributes{text} ) {
117 161         330 $attributes{text} = $name;
118             }
119              
120 218 100 100     625 if ( $element eq 'cell' && exists $attributes{alternate_classes} ) {
121 4         11 my @classes = @{ $attributes{alternate_classes} };
  4         15  
122 4         13 $attributes{oac} = \@classes;
123             }
124              
125 218         852 my %tattr = %attributes;
126 218         534 $attributes{is} = 'ro';
127 218         421 $attributes{lazy} = 1;
128 218 100       1172 if ( $element =~ m{row|cell}ixms ) {
129 37     0   260 $attributes{default} = sub { return \%tattr; };
  0         0  
130             } else {
131 180         381 my $class = $TABLE{$element};
132 180     180   1061 $attributes{default} = sub { my %aah = %tattr; return $class->new(\%aah); };
  180         2813  
  180         4188  
133             }
134              
135 217         1535 return %attributes;
136             }
137              
138             1;
139              
140             __END__