Widget/Element/Block.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 82 CC 4 SCAR 12.8
CC
4
Cov
0%
block_container
6.0
block_container_class
6.0
containerize
6.0
CRAP 3.6
1 package HTML::Widget::Element::Block;
2  
3 use warnings;
4 use strict;
5 use base 'HTML::Widget::Element::NullContainer';
6 use NEXT;
7 use Carp qw/croak/;
8  
9 __PACKAGE__->mk_classaccessor(
10 block_container_class => 'HTML::Widget::BlockContainer' );
11  
12 __PACKAGE__->mk_accessors(qw/type wrap_sub/);
13  
14 =head1 NAME
15  
16 HTML::Widget::Element::Block - Block Level Element
17  
18 =head1 SYNOPSIS
19  
20 my $e = $widget->element( 'Block', 'div' );
21 $e->value('bar');
22  
23 =head1 DESCRIPTION
24  
25 Block Level Element. Base class for HTML::Widget::Element::Fieldset
26  
27 =head1 METHODS
28  
29 =head2 new
30  
31 Returns a new Block element. Not usually required, use
32 $widget->element() or $block->element() to create a new Block element
33 within an existing widget or element.
34  
35 =cut
36  
37 sub new {
38 return shift->NEXT::new(@_)->type('div');
39 }
40  
41 =head2 type
42  
43 Default value is div, to create a <div> container. Can be changed to
44 create a tag of any type.
45  
46 =head2 element
47  
48 Add a new element, nested within this Block. See L<HTML::Widget/element>
49 for full documentation.
50  
51 =head2 push_content
52  
53 Add previously-created elements to the end of this block's elements.
54  
55 =head2 unshift_content
56  
57 Add previously-created elements to the start of this block's elements.
58  
59 =head2 block_container
60  
61 Creates a new block container object of type $self->block_container_class.
62 Defaults to L<HTML::Widget::BlockContainer>.
63  
64 =cut
65  
66 sub block_container {
67 my ( $self, $attributes ) = @_;
68 my $class = $self->block_container_class
69 || 'HTML::Widget::BlockContainer';
70 my $file = $class . ".pm";
71 $file =~ s{::}{/}g;
72 eval { require $file };
73 croak "Unable to load block container class $class: $@" if $@;
74  
75 return $class->new( { passive => $self->passive, %$attributes } );
76 }
77  
78 =head2 block_container_class
79  
80 Sets the class to be used by $self->block_container. Can be called as a
81 class or instance method.
82  
83 =cut
84  
85 sub block_container_class {
86 my ($self) = shift;
87  
88 if ( not $_[0] and @_ >= 1 ) {
89 delete $self->{block_container_class};
90 }
91  
92 return $self->_block_container_class_accessor(@_);
93 }
94  
95 =head2 containerize
96  
97 Containerize the block and all its contained elements for later
98 rendering. Uses HTML::Widget::BlockContainer by default, but this can
99 be over-ridden on a class or instance basis via
100 L<block_container_class>.
101  
102 =cut
103  
104 sub containerize {
105 my ( $self, $w, $value, $error, $args ) = @_;
106  
107 # NB: block-level HTML::Element generated here
108 my %attrs;
109 unless ( $self->{_anonymous} ) {
110 $attrs{id} = $self->id($w);
111 }
112 my $e = HTML::Element->new( $self->type, %attrs );
113  
114 my @pre_content = $self->_pre_content_elements($w);
115 my @post_content = $self->_post_content_elements($w);
116  
117 local $w->{attributes}->{id} = $self->id($w);
118  
119 my @content = $w->_containerize_elements( $self->content, $args );
120  
121 $e->attr( $_ => ${ $self->attributes }{$_} )
122 for ( keys %{ $self->attributes } );
123  
124 return $self->block_container( {
125 element => $e,
126 content => \@content,
127 pre_content => \@pre_content,
128 post_content => \@post_content,
129 wrap_sub => $self->wrap_sub,
130 name => $self->name,
131 } );
132 }
133  
134 sub _pre_content_elements { return (); }
135 sub _post_content_elements { return (); }
136  
137 =head2 get_elements
138  
139 my @elements = $self->get_elements;
140
141 my @elements = $self->get_elements( type => 'Textfield' );
142
143 my @elements = $self->get_elements( name => 'username' );
144  
145 Returns a list of all elements added to the widget.
146  
147 If a 'type' argument is given, only returns the elements of that type.
148  
149 If a 'name' argument is given, only returns the elements with that name.
150  
151 =head2 get_element
152  
153 my $element = $self->get_element;
154
155 my $element = $self->get_element( type => 'Textfield' );
156
157 my $element = $self->get_element( name => 'username' );
158  
159 Similar to get_elements(), but only returns the first element in the list.
160  
161 Accepts the same arguments as get_elements().
162  
163 =head2 find_elements
164  
165 Similar to get_elements(), and has the same alternate forms, but performs a
166 recursive search through itself and child elements.
167  
168 =head1 SEE ALSO
169  
170 L<HTML::Widget::Element>
171  
172 =head1 AUTHOR
173  
174 Michael Gray, C<mjg@cpan.org>
175  
176 =head1 LICENSE
177  
178 This library is free software, you can redistribute it and/or modify it under
179 the same terms as Perl itself.
180  
181 =cut
182  
183 1;