File Coverage

blib/lib/Data/HTML/Button.pm
Criterion Covered Total %
statement 44 48 91.6
branch 15 20 75.0
condition n/a
subroutine 11 12 91.6
pod 0 1 0.0
total 70 81 86.4


line stmt bran cond sub pod time code
1              
2             use strict;
3 3     3   84009 use warnings;
  3         18  
  3         94  
4 3     3   17  
  3         5  
  3         96  
5             use Error::Pure qw(err);
6 3     3   1244 use List::Util qw(none);
  3         21561  
  3         55  
7 3     3   200 use Mo qw(build default is);
  3         6  
  3         427  
8 3     3   1615 use Mo::utils qw(check_bool check_required);
  3         1325  
  3         17  
9 3     3   10068 use Readonly;
  3         4128  
  3         53  
10 3     3   238  
  3         6  
  3         1398  
11             Readonly::Array our @DATA_TYPES => qw(plain tags);
12             Readonly::Array our @ENCTYPES => (
13             'application/x-www-form-urlencoded',
14             'multipart/form-data',
15             'text/plain',
16             );
17             Readonly::Array our @FORM_METHODS => qw(get post);
18             Readonly::Array our @TYPES => qw(button reset submit);
19              
20             our $VERSION = 0.03;
21              
22             has autofocus => (
23             ro => 1,
24             );
25              
26             has css_class => (
27             ro => 1,
28             );
29              
30             has data => (
31             default => [],
32             ro => 1,
33             );
34              
35             has data_type => (
36             ro => 1,
37             );
38              
39             has disabled => (
40             ro => 1,
41             );
42              
43             has form => (
44             ro => 1,
45             );
46              
47             has form_enctype => (
48             ro => 1,
49             );
50              
51             has form_method => (
52             ro => 1,
53             );
54              
55             has id => (
56             ro => 1,
57             );
58              
59             has label => (
60             ro => 1,
61             );
62              
63             has name => (
64             ro => 1,
65             );
66              
67             has type => (
68             ro => 1,
69             );
70              
71             has value => (
72             ro => 1,
73             );
74              
75             my $self = shift;
76              
77 5     5 0 19471 # Check autofocus.
78             if (! defined $self->{'autofocus'}) {
79             $self->{'autofocus'} = 0;
80 5 100       19 }
81 3         7 check_bool($self, 'autofocus');
82              
83 5         16 # Check data type.
84             if (! defined $self->{'data_type'}) {
85             $self->{'data_type'} = 'plain';
86 4 100       78 }
87 2         4 if (none { $self->{'data_type'} eq $_ } @DATA_TYPES) {
88             err "Parameter 'data_type' has bad value.";
89 4 100   5   29 }
  5         73  
90 1         3  
91             # Check disabled.
92             if (! defined $self->{'disabled'}) {
93             $self->{'disabled'} = 0;
94 3 100       14 }
95 2         6 check_bool($self, 'disabled');
96              
97 3         8 # Check form_enctype.
98             if (defined $self->{'form_enctype'}) {
99             if (none { $self->{'form_enctype'} eq $_ } @ENCTYPES) {
100 3 50       41 err "Parameter 'form_enctype' has bad value.",
101 0 0   0   0 'Value', $self->{'form_enctype'},
  0         0  
102             ;
103 0         0 }
104             }
105              
106             # Check form_method.
107             if (! defined $self->{'form_method'}) {
108             $self->{'form_method'} = 'get';
109 3 50       8 }
110 3         5 if (none { $self->{'form_method'} eq $_ } @FORM_METHODS) {
111             err "Parameter 'form_method' has bad value.";
112 3 50   3   15 }
  3         39  
113 0         0  
114             # Check type.
115             if (! defined $self->{'type'}) {
116             $self->{'type'} = 'button';
117 3 100       12 }
118 1         2 if (none { $self->{'type'} eq $_ } @TYPES) {
119             err "Parameter 'type' has bad value.";
120 3 100   7   11 }
  7         50  
121 1         4  
122             return;
123             }
124 2         6  
125             1;
126              
127              
128             =pod
129              
130             =encoding utf8
131              
132             =head1 NAME
133              
134             Data::HTML::Button - Data object for HTML button element.
135              
136             =head1 SYNOPSIS
137              
138             use Data::HTML::Button;
139              
140             my $obj = Data::HTML::Button->new(%params);
141             my $autofocus = $obj->autofocus;
142             my $css_class = $obj->css_class;
143             my $data = $obj->data;
144             my $data_type = $obj->data_type;
145             my $disabled = $obj->disabled;
146             my $form = $obj->form;
147             my $form_enctype = $obj->form_enctype;
148             my $form_method = $obj->form_method;
149             my $id = $obj->id;
150             my $label = $obj->label;
151             my $name = $obj->name;
152             my $type = $obj->type;
153             my $value = $obj->value;
154              
155             =head1 METHODS
156              
157             =head2 C<new>
158              
159             my $obj = Data::HTML::Button->new(%params);
160              
161             Constructor.
162              
163             Returns instance of object.
164              
165             =over 8
166              
167             =item * C<autofocus>
168              
169             Button autofocus flag.
170              
171             Default value is 0.
172              
173             =item * C<css_class>
174              
175             Button CSS class.
176              
177             Default value is undef.
178              
179             =item * C<data>
180              
181             Button data content. It's reference to array.
182             Data type of data is described in 'data_type' parameter.
183              
184             Default value is [].
185              
186             =item * C<data_type>
187              
188             Button data type for content.
189              
190             Possible value are: plain tags
191              
192             Default value is 'plain'.
193              
194             =item * C<disabled>
195              
196             Button autofocus flag.
197              
198             Default value is 0.
199              
200             =item * C<form>
201              
202             Button form id.
203              
204             Default value is undef.
205              
206             =item * C<form_enctype>
207              
208             Button form encoding.
209             It's valuable for 'submit' type.
210              
211             Possible values are: application/x-www-form-urlencoded multipart/form-data text/plain
212              
213             Default value is undef.
214              
215             =item * C<form_method>
216              
217             Button form method.
218             It's valuable for 'submit' type.
219              
220             Possible values are: get post
221              
222             Default value is 'get'.
223              
224             =item * C<id>
225              
226             Button identifier.
227              
228             Default value is undef.
229              
230             =item * C<label>
231              
232             Button label.
233              
234             Default value is undef.
235              
236             =item * C<name>
237              
238             Button name.
239              
240             Default value is undef.
241              
242             =item * C<type>
243              
244             Button element type.
245              
246             Possible types: button reset submit
247              
248             Default value is 'button'.
249              
250             =item * C<value>
251              
252             Button value.
253              
254             Default value is undef.
255              
256             =back
257              
258             =head2 C<autofocus>
259              
260             my $autofocus = $obj->autofocus;
261              
262             Get button autofocus flag.
263              
264             Returns bool value (1/0).
265              
266             =head2 C<css_class>
267              
268             my $css_class = $obj->css_class;
269              
270             Get CSS class for button.
271              
272             Returns string.
273              
274             =head2 C<data>
275              
276             my $data = $obj->data;
277              
278             Get data inside button element.
279              
280             Returns reference to array.
281              
282             =head2 C<data_type>
283              
284             my $data_type = $obj->data_type;
285              
286             Get button data type.
287              
288             Returns string.
289              
290             =head2 C<disabled>
291              
292             my $disabled = $obj->disabled;
293              
294             Get button disabled flag.
295              
296             Returns bool value (1/0).
297              
298             =head2 C<form>
299              
300             my $form = $obj->form;
301              
302             Get button form id.
303              
304             Returns string.
305              
306             =head2 C<form_enctype>
307              
308             my $form_enctype = $obj->form_enctype;
309              
310             Get button form enctype.
311              
312             Returns string.
313              
314             =head2 C<form_method>
315              
316             my $form_method = $obj->form_method;
317              
318             Get button form method.
319              
320             Returns string.
321              
322             =head2 C<id>
323              
324             my $id = $obj->id;
325              
326             Get button identifier.
327              
328             Returns string.
329              
330             =head2 C<label>
331              
332             my $label = $obj->label;
333              
334             Get button label.
335              
336             Returns string.
337              
338             =head2 C<name>
339              
340             my $name = $obj->name;
341              
342             Get button name.
343              
344             Returns string.
345              
346             =head2 C<type>
347              
348             my $type = $obj->type;
349              
350             Get button type.
351              
352             Returns string.
353              
354             =head2 C<value>
355              
356             my $value = $obj->value;
357              
358             Get button value.
359              
360             Returns string.
361              
362             =head1 ERRORS
363              
364             new():
365             Parameter 'autofocus' must be a bool (0/1).
366             Value: %s
367             Parameter 'data_type' has bad value.
368             Parameter 'disabled' must be a bool (0/1).
369             Value: %s
370             Parameter 'form_enctype' has bad value.
371             Value: %s
372             Parameter 'form_method' has bad value.
373             Parameter 'type' has bad value.
374              
375             =head1 EXAMPLE1
376              
377             =for comment filename=button_default.pl
378              
379             use strict;
380             use warnings;
381              
382             use Data::HTML::Button;
383              
384             my $obj = Data::HTML::Button->new;
385              
386             # Print out.
387             print 'Data type: '.$obj->data_type."\n";
388             print 'Form method: '.$obj->form_method."\n";
389             print 'Type: '.$obj->type."\n";
390              
391             # Output:
392             # Data type: plain
393             # Form method: get
394             # Type: button
395              
396             =head1 EXAMPLE2
397              
398             =for comment filename=button_tags.pl
399              
400             use strict;
401             use warnings;
402              
403             use Data::HTML::Button;
404             use Tags::Output::Raw;
405              
406             my $obj = Data::HTML::Button->new(
407             # Tags(3pm) structure.
408             'data' => [
409             ['b', 'span'],
410             ['d', 'Button'],
411             ['e', 'span'],
412             ],
413             'data_type' => 'tags',
414             );
415              
416             my $tags = Tags::Output::Raw->new;
417              
418             # Serialize data to output.
419             $tags->put(@{$obj->data});
420             my $data = $tags->flush(1);
421              
422             # Print out.
423             print 'Data (serialized): '.$data."\n";
424             print 'Data type: '.$obj->data_type."\n";
425             print 'Form method: '.$obj->form_method."\n";
426             print 'Type: '.$obj->type."\n";
427              
428             # Output:
429             # Data (serialized): <span>Button</span>
430             # Data type: tags
431             # Form method: get
432             # Type: button
433              
434             =head1 EXAMPLE3
435              
436             =for comment filename=button_plain.pl
437              
438             use strict;
439             use warnings;
440              
441             use Data::HTML::Button;
442              
443             my $obj = Data::HTML::Button->new(
444             # Plain content.
445             'data' => [
446             'Button',
447             ],
448             'data_type' => 'plain',
449             );
450              
451             # Serialize data to output.
452             my $data = join ' ', @{$obj->data};
453              
454             # Print out.
455             print 'Data: '.$data."\n";
456             print 'Data type: '.$obj->data_type."\n";
457             print 'Form method: '.$obj->form_method."\n";
458             print 'Type: '.$obj->type."\n";
459              
460             # Output:
461             # Data: Button
462             # Data type: plain
463             # Form method: get
464             # Type: button
465              
466             =head1 DEPENDENCIES
467              
468             L<Error::Pure>,
469             L<List::Util>,
470             L<Mo>,
471             L<Mo::utils>,
472             L<Readonly>.
473              
474             =head1 REPOSITORY
475              
476             L<https://github.com/michal-josef-spacek/Data-HTML-Button>
477              
478             =head1 AUTHOR
479              
480             Michal Josef Špaček L<mailto:skim@cpan.org>
481              
482             L<http://skim.cz>
483              
484             =head1 LICENSE AND COPYRIGHT
485              
486             © 2022 Michal Josef Špaček
487              
488             BSD 2-Clause License
489              
490             =head1 VERSION
491              
492             0.03
493              
494             =cut