blib/lib/Tags/HTML/Messages.pm

filter: Statementstmt 100% 66 / 66 Branchbran 100% 18 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 100% 105 / 105 CC 15 SCAR 15.4
CC
15
Cov
100%
_process
8.0
_check_messages
5.0
_process_css
4.0
CRAP 4.7
↑ Summary
1 package Tags::HTML::Messages;
2  
3 5 ▶use base qw(Tags::HTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Class::Utils qw(set_params split_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = 0.08;
12  
13 # Constructor.
14 sub new {
15 22 ▶ my ($class, @params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16  
17 # Create object.
18 22 my ($object_params_ar, $other_params_ar) = split_params(
19 ['css_messages', 'flag_no_messages'], @params);
20 22 my $self = $class->SUPER::new(@{$other_params_ar});
21  
22 # CSS class.
23 20 $self->{'css_messages'} = 'messages';
24  
25 # Flag for no messages.
26 20 $self->{'flag_no_messages'} = 1;
27  
28 # Process params.
29 20 set_params($self, @{$object_params_ar});
30  
31 # Object.
32 20 return $self;
33 }
34  
35 sub _check_messages {
36 10 ▶ my ($self, $message_ar) = @_;
Subroutine _check_messages: called CC=5 SCAR=16.1
37  
38 10 ▶ if (ref $message_ar ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message_ar ne "ARRAY") 1 9 10
39 1 err "Bad list of messages.";
40 }
41 9 foreach my $message (@{$message_ar}) {
42 8 ▶ if (! blessed($message) || ! $message->isa('Data::Message::Simple')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($message) and $message->isa("Data::Message::Simple")) 2 6 8
Condition100%3/3
builtin::blessed($message) and $message->isa("Data::Message::Simple")
!ll&&!rl&&r
116
MC/DC100%2/2
builtin::blessed($message) and $message->isa("Data::Message::Simple")
builtin::blessed($message) $message->isa("Data::Message::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($message)B= $message->isa("Data::Message::Simple")
ABresult
0X0
100
111
43  
44 2 err 'Bad message data object.';
45 }
46 }
47  
48 7 return;
49 }
50  
51 # Process 'Tags'.
52 sub _process {
53 11 ▶ my ($self, $message_ar) = @_;
Subroutine _process: called CC=8 SCAR=20.8
54  
55 11 ▶ if (! defined $message_ar) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $message_ar) 1 10 11
56 1 return;
57 }
58  
59 10 $self->_check_messages($message_ar);
60  
61 # No messages.
62 7 ▶ if (! $self->{'flag_no_messages'} && ! @{$message_ar}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"flag_no_messages"} or @{$message_ar;}) 1 6 7
Condition100%3/3
$self->{"flag_no_messages"} or @{$message_ar;}
l!l&&r!l&&!r
511
MC/DC100%2/2
$self->{"flag_no_messages"} or @{$message_ar;}
$self->{"flag_no_messages"} @{$message_ar;}
Truth table100%3/3
A or B
A= $self->{"flag_no_messages"}B= @{$message_ar;}
ABresult
1X1
011
000
63 1 return;
64 }
65  
66 6 my $num = 0;
67 $self->{'tags'}->put(
68 ['b', 'div'],
69 6 ['a', 'class', $self->{'css_messages'}],
70 );
71 6 ▶ if (@{$message_ar}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$message_ar;}) { } 5 1 6
72 5 foreach my $message (@{$message_ar}) {
73 6 ▶ if ($num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num) 1 5 6
74 1 $self->{'tags'}->put(
75 ['b', 'br'],
76 ['e', 'br'],
77 );
78 }
79 6 ▶ $self->{'tags'}->put(
Branch: 1 branch
BranchTrueFalseTotal
defined $message->lang ? : 1 5 6
80 ['b', 'span'],
81 ['a', 'class', $message->type],
82 defined $message->lang
83 ? (['a', 'lang', $message->lang])
84 : (),
85 ['d', $message->text],
86 ['e', 'span'],
87 );
88 6 $num++;
89 }
90 } else {
91 1 $self->{'tags'}->put(
92 ['d', 'No messages'],
93 );
94 }
95 6 $self->{'tags'}->put(
96 ['e', 'div'],
97 );
98  
99 6 return;
100 }
101  
102 # Process 'CSS::Struct'.
103 sub _process_css {
104 4 ▶ my ($self, $message_types_hr) = @_;
Subroutine _process_css: called CC=4 SCAR=13.9
105  
106 4 ▶ if (! defined $message_types_hr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $message_types_hr) 1 3 4
107 1 return;
108 }
109 3 ▶ if (ref $message_types_hr ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message_types_hr ne "HASH") 1 2 3
110 1 err 'Message types must be a hash reference.';
111 }
112  
113 2 foreach my $message_type (keys %{$message_types_hr}) {
114 $self->{'css'}->put(
115 ['s', '.'.$message_type],
116 1 ['d', 'color', $message_types_hr->{$message_type}],
117 ['e'],
118 );
119 }
120  
121 2 return;
122 }
123  
124 5 1;
125  
126 __END__
127  
128 =pod
129  
130 =encoding utf8
131  
132 =head1 NAME
133  
134 Tags::HTML::Messages - Tags helper for HTML messages.
135  
136 =head1 SYNOPSIS
137  
138 use Tags::HTML::Messages;
139  
140 my $obj = Tags::HTML::Messages->new(%params);
141 $obj->process($message_ar);
142 $obj->process_css($type, $color);
143  
144 =head1 METHODS
145  
146 =head2 C<new>
147  
148 my $obj = Tags::HTML::Messages->new(%params);
149  
150 Constructor.
151  
152 =over 8
153  
154 =item * C<css>
155  
156 'CSS::Struct::Output' object for L<process_css> processing.
157  
158 Default value is undef.
159  
160 =item * C<css_messages>
161  
162 CSS class for main messages div block.
163  
164 Default value is 'messages'.
165  
166 =item * C<flag_no_messages>
167  
168 Flag for no messages printing.
169  
170 Possible values:
171  
172 0 - Print nothing
173 1 - Print message box with 'No messages.' text.
174  
175 Default value is 1.
176  
177 =item * C<tags>
178  
179 'Tags::Output' object.
180  
181 Default value is undef.
182  
183 =back
184  
185 =head2 C<process>
186  
187 $obj->process($message_ar);
188  
189 Process Tags structure for output.
190  
191 Reference to array with message objects C<$message_ar> must be a instance of
192 L<Data::Message::Simple> object.
193  
194 Returns undef.
195  
196 =head2 C<process_css>
197  
198 $obj->process_css($message_types_hr);
199  
200 Process CSS::Struct structure for output.
201  
202 Variable C<$message_type_hr> is reference to hash with keys for message type and value for color in CSS style.
203 Possible message types are info and error now. Types are defined in L<Data::Message::Simple>.
204  
205 Returns undef.
206  
207 =head1 ERRORS
208  
209 new():
210 From Class::Utils::set_params():
211 Unknown parameter '%s'.
212 Parameter 'css' must be a 'CSS::Struct::Output::*' class.
213 Parameter 'tags' must be a 'Tags::Output::*' class.
214  
215 process():
216 Bad list of messages.
217 Bad message data object.
218  
219 process_css():
220 Message types must be a hash reference.
221  
222 =head1 EXAMPLE1
223  
224 =for comment filename=html_page_with_messages.pl
225  
226 use strict;
227 use warnings;
228  
229 use CSS::Struct::Output::Indent;
230 use Data::Message::Simple;
231 use Tags::HTML::Page::Begin;
232 use Tags::HTML::Page::End;
233 use Tags::HTML::Messages;
234 use Tags::Output::Indent;
235  
236 # Object.
237 my $tags = Tags::Output::Indent->new(
238 'preserved' => ['style'],
239 'xml' => 1,
240 );
241 my $css = CSS::Struct::Output::Indent->new;
242 my $begin = Tags::HTML::Page::Begin->new(
243 'css' => $css,
244 'lang' => {
245 'title' => 'Tags::HTML::Messages example',
246 },
247 'generator' => 'Tags::HTML::Messages',
248 'tags' => $tags,
249 );
250 my $end = Tags::HTML::Page::End->new(
251 'tags' => $tags,
252 );
253 my $messages = Tags::HTML::Messages->new(
254 'css' => $css,
255 'tags' => $tags,
256 );
257  
258 # Error structure.
259 my $message_ar = [
260 Data::Message::Simple->new(
261 'text' => 'Error #1',
262 'type' => 'error',
263 ),
264 Data::Message::Simple->new(
265 'text' => 'Error #2',
266 'type' => 'error',
267 ),
268 Data::Message::Simple->new(
269 'lang' => 'en',
270 'text' => 'Ok #1',
271 ),
272 Data::Message::Simple->new(
273 'text' => 'Ok #2',
274 ),
275 ];
276  
277 # Process page.
278 $messages->process_css({
279 'error' => 'red',
280 'info' => 'green',
281 });
282 $begin->process;
283 $messages->process($message_ar);
284 $end->process;
285  
286 # Print out.
287 print $tags->flush;
288  
289 # Output:
290 # <!DOCTYPE html>
291 # <html lang="en">
292 # <head>
293 # <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
294 # <meta name="generator" content="Tags::HTML::Messages" />
295 # <meta name="viewport" content="width=device-width, initial-scale=1.0" />
296 # <title>
297 # Tags::HTML::Messages example
298 # </title>
299 # <style type="text/css">
300 # .error {
301 # color: red;
302 # }
303 # .info {
304 # color: green;
305 # }
306 # </style>
307 # </head>
308 # <body>
309 # <div class="messages">
310 # <span class="error">
311 # Error #1
312 # </span>
313 # <br />
314 # <span class="error">
315 # Error #2
316 # </span>
317 # <br />
318 # <span class="info" lang="en">
319 # Ok #1
320 # </span>
321 # <br />
322 # <span class="info">
323 # Ok #2
324 # </span>
325 # </div>
326 # </body>
327 # </html>
328  
329 =head1 DEPENDENCIES
330  
331 L<Class::Utils>,
332 L<Error::Pure>,
333 L<Scalar::Util>,
334 L<Tags::HTML>.
335  
336 =head1 REPOSITORY
337  
338 L<https://github.com/michal-josef-spacek/Tags-HTML-Messages>
339  
340 =head1 AUTHOR
341  
342 Michal Josef Špaček L<mailto:skim@cpan.org>
343  
344 L<http://skim.cz>
345  
346 =head1 LICENSE AND COPYRIGHT
347  
348 © Michal Josef Špaček 2020-2023
349  
350 BSD 2-Clause License
351  
352 =head1 VERSION
353  
354 0.08
355  
356 =cut
↑ Summary