File Coverage

blib/lib/Tags/HTML/Element/Utils.pm
Criterion Covered Total %
statement 38 38 100.0
branch 18 18 100.0
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 68 68 100.0


line stmt bran cond sub pod time code
1             package Tags::HTML::Element::Utils;
2              
3 54     54   173845 use base qw(Exporter);
  54         156  
  54         6612  
4 54     54   344 use strict;
  54         115  
  54         3027  
5 54     54   266 use warnings;
  54         168  
  54         2826  
6              
7 54     54   4233 use Readonly;
  54         32752  
  54         30789  
8              
9             Readonly::Array our @EXPORT_OK => qw(tags_boolean tags_data tags_label tags_value);
10              
11             our $VERSION = 0.15;
12              
13             sub tags_boolean {
14 49     49 1 357190 my ($self, $element, $method) = @_;
15              
16 49 100       165 if ($element->$method) {
17 8         120 return (['a', $method, $method]);
18             }
19              
20 41         371 return ();
21             }
22              
23             sub tags_data {
24 20     20 1 435215 my ($self, $object) = @_;
25              
26             # Plain content.
27 20 100       104 if ($object->data_type eq 'plain') {
    100          
28             $self->{'tags'}->put(
29 13         293 map { (['d', $_]) } @{$object->data},
  6         231  
  13         56  
30             );
31              
32             # Tags content.
33             } elsif ($object->data_type eq 'tags') {
34 4         209 $self->{'tags'}->put(@{$object->data});
  4         40  
35              
36             # Callback.
37             } else {
38 3         207 foreach my $cb (@{$object->data}) {
  3         13  
39 3         170 $cb->($self);
40             }
41             }
42              
43 20         1256 return;
44             }
45              
46             sub tags_label {
47 12     12 1 304127 my ($self, $object) = @_;
48              
49             # CSS for required span.
50 12         25 my $css_required = '';
51 12 100       63 if (defined $object->css_class) {
52 3         72 $css_required .= $object->css_class.'-';
53             }
54 12         312 $css_required .= 'required';
55              
56 12 100       46 $self->{'tags'}->put(
    100          
    100          
57             defined $object->label ? (
58             ['b', 'label'],
59             $object->id ? (
60             ['a', 'for', $object->id],
61             ) : (),
62             ['d', $object->label],
63             $object->required ? (
64             ['b', 'span'],
65             ['a', 'class', $css_required],
66             ['d', '*'],
67             ['e', 'span'],
68             ) : (),
69             ['e', 'label'],
70             ) : (),
71             );
72              
73 12         1502 return;
74             }
75              
76             sub tags_value {
77 132     132 1 336279 my ($self, $element, $method, $method_rewrite) = @_;
78              
79 132 100       420 if (defined $element->$method) {
80             return ([
81 43 100       503 'a',
82             defined $method_rewrite ? $method_rewrite : $method,
83             $element->$method,
84             ]);
85             }
86              
87 89         711 return ();
88             }
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding utf8
97              
98             =head1 NAME
99              
100             Tags::HTML::Element::Utils - Tags::HTML::Element utilities.
101              
102             =head1 SYNOPSIS
103              
104             use Tags::HTML::Element::Utils qw(tags_boolean tags_data tags_label tags_value);
105              
106             tags_boolean($self, $element, $method);
107             tags_data($self, $object);
108             tags_label($self, $object);
109             tags_value($self, $element, $method, $method_rewrite);
110              
111             =head1 DESCRIPTION
112              
113             Utilities for L<Tags::HTML::Element> classes.
114              
115             =head1 SUBROUTINES
116              
117             =head2 C<tags_boolean>
118              
119             tags_boolean($self, $element, $method);
120              
121             Get L<Tags> structure for element attribute, which is boolean if C<$method>
122             exists.
123              
124             Returns array of L<Tags> structure.
125              
126             =head2 C<tags_data>
127              
128             tags_data($self, $object);
129              
130             Get or process C<$object-E<gt>data> defined by C<$object-E<gt>data_type>
131             method.
132              
133             Possible C<data_type> values are:
134              
135             =over
136              
137             =item plain
138              
139             Convert plain text data in C<$object-E<gt>data> to L<Tags> data structure and
140             put to C<$self-E<gt>{'tags'}> method.
141              
142             =item tags
143              
144             Put L<Tags> data structure in C<$object-E<gt>data> and put to C<$self-E<gt>{'tags'}>
145             method.
146              
147             =item cb
148              
149             Call C<$object-E<gt>data> callback.
150              
151             =back
152              
153             =head2 C<tags_label>
154              
155             tags_label($self, $object);
156              
157             Process L<Tags> structure for element label, which is before form item element.
158              
159             Returns undef.
160              
161             =head2 C<tags_value>
162              
163             tags_value($self, $element, $method, $method_rewrite);
164              
165             Get L<Tags> structure for element attribute, which is value if C<$method>
166             exists. C<$method_rewrite> is value for key of attribute, when it's different
167             than C<$method> name.
168              
169             Returns array of L<Tags> structure.
170              
171             =head1 EXAMPLE1
172              
173             =for comment filename=tags_boolean.pl
174              
175             use strict;
176             use warnings;
177              
178             use Data::Printer;
179             use Tags::HTML::Element::Utils qw(tags_boolean);
180             use Test::MockObject;
181              
182             my $self = {};
183             my $obj = Test::MockObject->new;
184             $obj->set_true('foo');
185              
186             # Process $obj->foo.
187             my @tags = tags_boolean($self, $obj, 'foo');
188              
189             # Print out.
190             p $tags[0];
191              
192             # Output (like attribute <element foo="foo">):
193             # [
194             # [0] "a",
195             # [1] "foo",
196             # [2] "foo"
197             # ]
198              
199             =head1 DEPENDENCIES
200              
201             L<Exporter>,
202             L<Readonly>.
203              
204             =head1 REPOSITORY
205              
206             L<https://github.com/michal-josef-spacek/Tags-HTML-Element>
207              
208             =head1 AUTHOR
209              
210             Michal Josef Špaček L<mailto:skim@cpan.org>
211              
212             L<http://skim.cz>
213              
214             =head1 LICENSE AND COPYRIGHT
215              
216             © 2022-2024 Michal Josef Špaček
217              
218             BSD 2-Clause License
219              
220             =head1 VERSION
221              
222             0.15
223              
224             =cut