File Coverage

blib/lib/Plack/App/Tags/HTML.pm
Criterion Covered Total %
statement 73 74 98.6
branch 22 28 78.5
condition n/a
subroutine 12 12 100.0
pod n/a
total 107 114 93.8


line stmt bran cond sub pod time code
1             package Plack::App::Tags::HTML;
2              
3 10     10   517808 use base qw(Plack::Component::Tags::HTML);
  10         21  
  10         5286  
4 10     10   722464 use strict;
  10         18  
  10         227  
5 10     10   48 use warnings;
  10         19  
  10         517  
6              
7 10     10   54 use English;
  10         24  
  10         73  
8 10     10   4483 use Error::Pure qw(err);
  10         28  
  10         585  
9 10     10   59 use Plack::Util::Accessor qw(component constructor_args data data_css data_init data_prepare);
  10         15  
  10         82  
10 10     10   5208 use Symbol::Get;
  10         17612  
  10         5705  
11              
12             our $VERSION = 0.19;
13              
14             sub _css {
15 9     9   6263 my ($self, $env) = @_;
16              
17 9         17 my @data_css;
18 9 100       35 if (defined $self->data_css) {
19 1         6 push @data_css, @{$self->data_css};
  1         16  
20             }
21              
22 9         121 $self->{'_component'}->process_css(@data_css);
23              
24 9         200 return;
25             }
26              
27             sub _loaded_component {
28 11     11   31 my ($self, $component) = @_;
29              
30 11         26 my @names = eval {
31 11         72 Symbol::Get::get_names($component);
32             };
33 11 100       645 if ($EVAL_ERROR) {
34 9         48 return 0;
35             }
36              
37 2         10 return 1;
38             }
39              
40             sub _prepare_app {
41 11     11   2470284 my $self = shift;
42              
43 11         80 $self->SUPER::_prepare_app();
44              
45 11         5152 my %p = (
46             'css' => $self->css,
47             'tags' => $self->tags,
48             );
49              
50 11         158 my $component = $self->component;
51 11 100       83 if (! $self->_loaded_component($component)) {
52 9         678 eval "require $component;";
53 9 100       3334 if ($EVAL_ERROR) {
54 2         12 err "Cannot load component '$component'.",
55             'Error', $EVAL_ERROR;
56             }
57             }
58             $self->{'_component'} = $component->new(
59             %p,
60             defined $self->constructor_args ? (
61 9 100       67 %{$self->constructor_args},
  1         9  
62             ) : (),
63             );
64 9 50       937 if (! $self->{'_component'}->isa('Tags::HTML')) {
65 0         0 err "Component must be a instance of 'Tags::HTML' class.";
66             }
67              
68             # Init prepared data.
69 9 50       103 if ($self->{'_component'}->can('prepare')) {
70 9         20 my @data = ();
71 9 100       38 if (defined $self->data_prepare) {
72 1         8 push @data, @{$self->data_prepare};
  1         3  
73             }
74 9         84 $self->{'_component'}->prepare(@data);
75             }
76              
77             # Copy CSS links from component to main object.
78 9 50       166 if ($self->{'_component'}->can('css_src')) {
79 9         48 $self->css_src($self->{'_component'}->css_src);
80             }
81              
82             # Copy Javascript links from component to main object.
83 9 50       143 if ($self->{'_component'}->can('script_js_src')) {
84 9         42 $self->script_js_src($self->{'_component'}->script_js_src);
85             }
86              
87 9         112 return;
88             }
89              
90             sub _process_actions {
91 9     9   1421 my ($self, $env) = @_;
92              
93 9 50       99 if ($self->{'_component'}->can('init')) {
94 9 100       38 if (defined $self->data_init) {
95 1         11 $self->{'_component'}->init(@{$self->data_init});
  1         6  
96             } else {
97 8         73 $self->{'_component'}->init;
98             }
99             }
100              
101             # Copy Javascript code from component to main object.
102 9 50       184 if ($self->{'_component'}->can('script_js')) {
103 9         43 $self->script_js($self->{'_component'}->script_js);
104             }
105              
106             # Init begin of page.
107 9         142 $self->SUPER::_process_actions($env);
108              
109 9         62 return;
110             }
111              
112             sub _tags_middle {
113 9     9   14452 my ($self, $env) = @_;
114              
115 9         22 my @data;
116 9 100       53 if (defined $self->data) {
117 2         12 push @data, @{$self->data};
  2         13  
118             }
119 9         186 $self->{'_component'}->process(@data);
120              
121 9         10422 return;
122             }
123              
124             1;
125              
126             __END__