FormHandler/Blocks.pm

filter: Statementstmt 83.3% 65 / 78 Branchbran 53.5% 15 / 28 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 4 totaltotal 75.0% 105 / 140 CC 26 SCAR 22.8
CC
26
Cov
76%
get_renderer
20.0
make_block
15.0
_build_meta_block_list
10.0
CRAP 9.8
« FormHandler/Widget/Form/Simple.pm ↑ Summary FormHandler/Field.pm »
1 package HTML::FormHandler::Blocks;
2 # ABSTRACT: arrange form layout using blocks
3 146 $HTML::FormHandler::Blocks::VERSION = '0.410000';
4  
5 146 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 146 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 146 ▶use Class::Load qw/ load_optional_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 146 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 146 ▶use Data::Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 146 ▶use HTML::FormHandler::Widget::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 146 has 'blocks' => (
13 isa => 'HashRef[Object]',
14 is => 'ro',
15 lazy => 1,
16 traits => ['Hash'],
17 builder => 'build_blocks',
18 handles => {
19 has_blocks => 'count',
20 add_block => 'set',
21 block => 'get',
22 block_exists => 'exists',
23 },
24 );
25 10 ▶sub build_blocks { {} }
Subroutine build_blocks: called CC=1 SCAR=0.0
Pod undocumented
26  
27 146 has 'block_list' => ( is => 'rw', isa => 'ArrayRef', lazy => 1, builder => 'build_block_list' );
28 254 ▶sub build_block_list {[]}
Subroutine build_block_list: called CC=1 SCAR=0.0
Pod undocumented
29  
30 146 has 'render_list' => (
31 is => 'rw',
32 isa => 'ArrayRef[Str]',
33 traits => ['Array'],
34 lazy => 1,
35 builder => 'build_render_list',
36 handles => {
37 has_render_list => 'count',
38 add_to_render_list => 'push',
39 all_render_list => 'elements',
40 get_render_list => 'get',
41 }
42 );
43  
44 sub get_renderer {
45 0 ▶ my ( $self, $name ) = @_;
Subroutine get_renderer: not called CC=4 SCAR=30.0
Pod undocumented
46 0 ▶ die "must provide a name to get_renderer" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
47 0 my $obj = $self->block($name);
48 0 ▶ return $obj if ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj 0 0 0
49 0 $obj = $self->field_from_index($name);
50 0 ▶ return $obj if ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj 0 0 0
51 0 die "did not find a field or block with name $name\n";
52 }
53  
54 after '_build_fields' => sub {
55 256 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
56  
57 256 my $meta_blist = $self->_build_meta_block_list;
58 256 ▶ if( @$meta_blist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$meta_blist) 8 248 256
59 8 foreach my $block_attr (@$meta_blist) {
60 16 $self->make_block($block_attr);
61 }
62 }
63 256 my $blist = $self->block_list;
64 256 ▶ if( @$blist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$blist) 2 254 256
65 2 foreach my $block_attr (@$blist) {
66 6 $self->make_block($block_attr);
67 }
68 }
69 146 };
70  
71 sub make_block {
72 22 ▶ my ( $self, $block_attr ) = @_;
Subroutine make_block: called CC=10 SCAR=27.1
Pod undocumented
73  
74 22 ▶ my $type = $block_attr->{type} ||= '';
Condition100%2/2
$block_attr->{"type"} ||= ""
l!l
517
MC/DC100%1/1
$block_attr->{"type"} ||= ""
$block_attr->{"type"}
Truth table100%2/2
A
A= $block_attr->{"type"}
Aresult
11
00
75 22 my $name = $block_attr->{name};
76 22 ▶ die "You must supply a name for a block" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 22 22
77  
78 22 my $do_update;
79 22 ▶ if ( $name =~ /^\+(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\+(.*)/) 0 22 22
80 0 $block_attr->{name} = $name = $1;
81 0 $do_update = 1;
82 }
83  
84 22 my $class;
85 22 ▶ if( $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 5 17 22
86 5 $class = $self->get_widget_role($type, 'Block');
87 }
88 else {
89 17 $class = 'HTML::FormHandler::Widget::Block';
90 }
91  
92 22 ▶ $block_attr->{form} = $self->form if $self->form;
Branch: 1 branch
BranchTrueFalseTotal
if $self->form 22 0 22
93  
94 22 my $block = $self->form->block( $block_attr->{name} );
95 22 ▶ if ( defined $block && $do_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block and $do_update) { } 0 22 22
Condition33%1/3
defined $block and $do_update
!ll&&!rl&&r
2200
MC/DC0%0/2
defined $block and $do_update
defined $block $do_update
Truth table33%1/3
A and B
A= defined $blockB= $do_update
ABresult
0X0
100
111
96 0 delete $block_attr->{name};
97 0 foreach my $key ( keys %{$block_attr} ) {
98 0 ▶ $block->$key( $block_attr->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if $block->can($key) 0 0 0
99 if $block->can($key);
100 }
101 }
102 else # new block
103 {
104 22 $block = $class->new(%$block_attr);
105 22 $self->add_block( $name, $block );
106 }
107 }
108  
109 # loops through all inherited classes and composed roles
110 # to find blocks specified with 'has_block'
111 sub _build_meta_block_list {
112 256 ▶ my $self = shift;
Subroutine _build_meta_block_list: called CC=10 SCAR=23.0
113 256 my @block_list;
114  
115 256 foreach my $sc ( reverse $self->meta->linearized_isa ) {
116 1032 my $meta = $sc->meta;
117 1032 ▶ if ( $meta->can('calculate_all_roles') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->can("calculate_all_roles")) 1032 0 1032
118 1032 foreach my $role ( reverse $meta->calculate_all_roles ) {
119 2888 ▶ if ( $role->can('block_list') && $role->has_block_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role->can("block_list") and $role->has_block_list) 1 2887 2888
Condition100%3/3
$role->can("block_list") and $role->has_block_list
!ll&&!rl&&r
288161
MC/DC100%2/2
$role->can("block_list") and $role->has_block_list
$role->can("block_list") $role->has_block_list
Truth table100%3/3
A and B
A= $role->can("block_list")B= $role->has_block_list
ABresult
0X0
100
111
120 1 foreach my $block_def ( @{ $role->block_list } ) {
121 3 push @block_list, $block_def;
122 }
123 }
124 }
125 }
126 1032 ▶ if ( $meta->can('block_list') && $meta->has_block_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->can("block_list") and $meta->has_block_list) 7 1025 1032
Condition100%3/3
$meta->can("block_list") and $meta->has_block_list
!ll&&!rl&&r
7712547
MC/DC100%2/2
$meta->can("block_list") and $meta->has_block_list
$meta->can("block_list") $meta->has_block_list
Truth table100%3/3
A and B
A= $meta->can("block_list")B= $meta->has_block_list
ABresult
0X0
100
111
127 7 foreach my $block_def ( @{ $meta->block_list } ) {
128 13 push @block_list, $block_def;
129 }
130 }
131 }
132 256 return clone( \@block_list );
133 }
134  
135  
136 146 1;
137  
138 __END__
139  
140 =pod
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 HTML::FormHandler::Blocks - arrange form layout using blocks
147  
148 =head1 VERSION
149  
150 version 0.410000
151  
152 =head1 SYNOPSIS
153  
154 This is a role which provides the ability to render your form in
155 arbitrary 'blocks', instead of by fields. This role is included
156 by default in HTML::FormHandler.
157  
158 package MyApp::Form;
159 use HTML::FormHandler::Moose;
160 extends 'HTML::FormHandler';
161  
162 sub build_render_list {[ 'foo', 'fset' ]}
163 has_field 'foo';
164 has_field 'bar';
165 has_field 'nox';
166 has_block 'fset' => ( tag => 'fieldset', render_list => ['bar', 'nox'] );;
167 ....
168 $form->render;
169  
170 Blocks live in the HTML::FormHandler::Widget::Block:: namespace. The default,
171 non-typed block is L<HTML::FormHandler::Widget::Block>. Provide a type for
172 custom blocks:
173  
174 has_block 'my_block' => ( type => 'CustomBlock', render_list => [...] );
175  
176 You can also build blocks with a 'block_list' attribute, or the builder for it,
177 'build_block_list'.
178  
179 Rendering with blocks is supported by the rendering widgets. Render::Simple doesn't
180 do it, though it would be possible to make your own custom renderer.
181  
182 =head1 AUTHOR
183  
184 FormHandler Contributors - see HTML::FormHandler
185  
186 =head1 COPYRIGHT AND LICENSE
187  
188 This software is copyright (c) 2017 by Gerda Shank.
189  
190 This is free software; you can redistribute it and/or modify it under
191 the same terms as the Perl 5 programming language system itself.
192  
193 =cut
« FormHandler/Widget/Form/Simple.pm ↑ Summary FormHandler/Field.pm »