blib/lib/Data/MuFormX/Registry.pm

filter: Statementstmt 89.8% 62 / 69 Branchbran 71.4% 10 / 14 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 83.3% 10 / 12 Podpod 50.0% 3 / 6 totaltotal 83.1% 89 / 107 CC 13 SCAR 10.9
CC
13
Cov
86%
expand_config
4.1
create
4.1
_build_forms_by_ns
4.0
CRAP 3.0
↑ Summary
1 package Data::MuFormX::Registry;
2  
3 1 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.001';
7  
8 1 ▶sub config { return %{+{}} }
Subroutine config: called CC=1 SCAR=0.0
Pod documented
9  
10 1 has 'config' => (
11 is=>'ro',
12 required=>0,
13 predicate=>'has_init_arg_config',
14 reader=>'init_arg_config');
15  
16 1 has 'form_namespace' => (
17 is=>'ro',
18 required=>1,
19 lazy=>1,
20 builder=>'_build_form_namespace');
21  
22 1 ▶ sub _default_form_namespace_part { 'Form' }
Subroutine _default_form_namespace_part: called CC=1 SCAR=0.0
23  
24 1 ▶ sub _build_form_namespace {
Subroutine _build_form_namespace: called CC=1 SCAR=0.0
25 1 my $package = ref($_[0]);
26 1 my @parts = split('::', $package);
27 1 my @prefix = (@parts[0..($#parts-1)]);
28 1 my $form_namespace = join('::',
29 @prefix,
30 $_[0]->_default_form_namespace_part);
31 1 return $form_namespace;
32 }
33  
34 1 has 'form_packages' => (
35 is=>'ro',
36 required=>1,
37 lazy=>1,
38 builder=>'_build_form_packages');
39
40 2 ▶ sub _build_form_packages {
Subroutine _build_form_packages: called CC=3 SCAR=11.2
41 2 my $self = shift;
42 my @search = ref($self->form_namespace) ?
43 2 ▶ @{$self->form_namespace} :
Branch: 1 branch
BranchTrueFalseTotal
ref $self->form_namespace ? : 0 2 2
44 ($self->form_namespace);
45  
46 2 my %packages = ();
47 2 foreach my $search(@search, 'Data::MuForm::CommonForms') {
48 4 my @packages = Module::Pluggable::Object->new(
49 require => 1,
50 search_path => $search,
51 )->plugins;
52 4 $packages{$search} = \@packages;
53 }
54 2 return \%packages;
55 }
56  
57 2 ▶sub normalized_config {
Subroutine normalized_config: called CC=2 SCAR=6.9
Pod undocumented
58 2 my $self = shift;
59 2 my %normalized_config = $self->config;
60 2 ▶ if($self->has_init_arg_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_init_arg_config) 1 1 2
61 1 %normalized_config = (%normalized_config, %{$self->init_arg_config});
62 }
63 2 return %normalized_config;
64 }
65  
66 1 has 'forms_by_ns' => (
67 is=>'ro',
68 required=>1,
69 lazy=>1,
70 builder=>'_build_forms_by_ns');
71  
72 2 ▶ sub _build_forms_by_ns {
Subroutine _build_forms_by_ns: called CC=4 SCAR=13.9
73 2 my $self = shift;
74 2 my %normalized_config = $self->normalized_config;
75 2 my %form_packages = %{$self->form_packages};
76 2 my %names = ();
77  
78 2 foreach my $ns (keys %form_packages) {
79 4 foreach my $package (@{$form_packages{$ns}}) {
80 4 my ($name) = ($package=~/^$ns\:\:(.+)$/);
81 4 ▶ my $config = ($normalized_config{$name}||+{});
Condition100%2/2
$normalized_config{$name} || {}
l!l
22
MC/DC100%1/1
$normalized_config{$name} || {}
$normalized_config{$name}
Truth table100%2/2
A
A= $normalized_config{$name}
Aresult
11
00
82 4 $names{$name} = +{
83 package => $package,
84 config => $config,
85 form => $package->new(
86 $self->expand_config($config)),
87 };
88 }
89 }
90 2 return \%names;
91 }
92  
93 0 ▶sub form_names { return keys %{$_[0]->forms_by_ns} }
Subroutine form_names: not called CC=1 SCAR=6.9
Pod documented
94  
95 4 ▶sub create {
Subroutine create: called CC=4 SCAR=14.1
Pod documented
96 4 my ($self, $ns, @proto) = @_;
97 4 ▶ my %info = %{ $self->forms_by_ns->{$ns} || die "No component called '$ns'" };
Condition50%1/2
$self->forms_by_ns->{$ns} || die("No component called '${ns}'")
l!l
40
MC/DC0%0/1
$self->forms_by_ns->{$ns} || die("No component called '${ns}'")
$self->forms_by_ns->{$ns}
Truth table50%1/2
A
A= $self->forms_by_ns->{$ns}
Aresult
11
00
98  
99 4 ▶ if(@proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (@proto) { } 2 2 4
100 2 ▶ my %args = ref($proto[0]) ? %{$proto[0]} : @proto; # allow both hash and hashref
Branch: 1 branch
BranchTrueFalseTotal
ref $proto[0] ? : 0 2 2
101 2 my %config = $self->expand_config($info{config});
102 2 my $package = $info{package};
103 2 return $package->new(%config, %args);
104  
105 } else {
106 2 return $info{form} ;
107 }
108 }
109  
110 6 ▶sub expand_config {
Subroutine expand_config: called CC=4 SCAR=14.2
Pod undocumented
111 6 my ($self, $config_proto) = @_;
112 6 ▶ return unless $config_proto;
Branch: 1 branch
BranchTrueFalseTotal
unless $config_proto 0 6 6
113 6 ▶ if(ref($config_proto) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $config_proto eq 'CODE') { } 2 4 6
elsif (ref $config_proto eq 'HASH') { } 4 0 4
114 2 my $config = $config_proto->($self);
115 2 return %{$config};
116 } elsif(ref($config_proto) eq 'HASH') {
117 4 return %{$config_proto};
118 } else {
119 0 die "Not sure how to resolve $config_proto";
120 }
121 }
122  
123 0 ▶sub process_form_args {
Subroutine process_form_args: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my ($self, $package, %args) = @_;
125 0 return %args;
126 }
127  
128 1 1;
129  
130 =head1 NAME
131  
132 Data::MuFormX::Registry - Registry of Form classes
133  
134 =head1 SYNOPSIS
135  
136 Given some L<Data::MuForms> in a common namespace:
137  
138 package MyApp::Form::Login;
139  
140 use Moo;
141 use Data::MuForm::Meta;
142  
143 extends 'Data::MuForm';
144  
145 has_field 'username' => (
146 type => 'Text',
147 required => 1 );
148  
149 package MyApp::Form::Email;
150  
151 use Moo;
152 use Data::MuForm::Meta;
153  
154 extends 'Data::MuForm';
155  
156 has_field 'username' => (
157 type => 'Text',
158 required => 1 );
159  
160 Create a 'registry' object that will load a prepare all the forms:
161  
162 my $registry = Data::MuFormX::Registry->new(form_namespace=>'MyApp::Form');
163 my $login = $registry->create('Login'); # $login ISA MyApp::Form::Login
164  
165 You may also subclass for hardcoded defaults
166  
167 package MyApp::MyRegistry;
168  
169 use Moo;
170 extends 'Data::MuFormX::Registry';
171  
172 1;
173  
174 # 'form_namespace defaults to 'MyApp::Form'
175 my $registry = MyApp::MyRegistry->new;
176  
177 =head1 DESCRIPTION
178  
179 B<NOTE> Early access; the docs do not describe all existing features (read
180 the source :) ) and I reserve the right to break stuff if that's the only way
181 to fix deep problems. On the other hand there's not a ton of stuff here so
182 its probably ok...
183  
184 This is a wrapper on top of L<Module::Pluggable::Object> to make it easier
185 to load up and create a namespace of L<Data::MuForm> based form validation
186 classes. At its heart it makes it so you don't have to say 'use Form;' for
187 every form you need in a package. It also adds a way to centralize some
188 form initialization work. This may or may not recommend itself to you. I
189 think it makes it easier to reuse forms in different packages (for example in
190 different L<Mojolicous> controllers). On the other hand it injects a proxy
191 layer such that '$registry->create("Login")' is not 100% transparent in that
192 you are getting an instance of 'MyApp::Form::Login'. You may consider this
193 a type of action at a distance that makes your code harder to maintain.
194  
195 If you have a lot of L<Data::MuForm> based form validation classes you may find
196 it more useful. I also believe it helps you follow the 'code against an interface
197 not an class' best practice. As you wish ;)
198  
199 =head1 METHOD
200  
201 This class exposes the follow methods for intended public use.
202  
203 =head2 new
204  
205 Create a new registry object. You can set the following initial arguments:
206  
207 =over 4
208  
209 =item form_namespace
210  
211 Either a scalar or array ref of the base namespaces used to find forms.
212  
213 =item config
214  
215 configuration values used when creating form objects.
216  
217 =back
218  
219 =head2 create
220  
221 Create a new form. Requires a form name. May accept a hash of additional initialization
222 values (which are merged with any global configuration. Examples:
223  
224 my $transaction = $registry->create('Transaction');
225 my $login = $registry->create('Login', user_rs=>$users);
226  
227 If you do not need to pass any extra arguments we reuse a pre-initialized copy of
228 the form rather than build a new one as a performance enhancement.
229  
230 =head2 form_names
231  
232 Returns an array of the form names, which can be used in L</create>. Do not
233 rely on return order!
234  
235 =head2 config
236  
237 Global configuration information for all forms.
238  
239 Intended to be overridden in a subclass to provide form defaults. For example:
240  
241 package MyRegistry;
242  
243 use Moo;
244 extends 'Data::MuFormX::Registry';
245  
246 sub config {
247 'Login' => + {
248 min_username_length => 11,
249 },
250 'NewNodes' => sub {
251 my ($self) = @_;
252 return +{
253 example1 => 1,
254 example2 => 1,
255 };
256 },
257 }
258  
259 This method should return a hash where the key is the form name and the value is
260 either a hashref used as part of the instantiation of the form or a coderef which
261 recieves the registry instance and should return a hashref. The second form is
262 useful in cases where you have a form that itself has other forms as subforms or
263 when you custom subclass contains additional information of value to the form (such
264 as a database connection).
265  
266 =head1 AUTHOR
267
268 John Napiorkowski L<email:jjnapiork@cpan.org>
269
270 =head1 SEE ALSO
271
272 L<Data::MuForm>, L<Module::Pluggable::Object>
273  
274 =head1 COPYRIGHT & LICENSE
275
276 Copyright 2018, John Napiorkowski L<email:jjnapiork@cpan.org>
277
278 This library is free software; you can redistribute it and/or modify it under
279 the same terms as Perl itself.
↑ Summary