MasonX/Component/Registry.pm

filter: Statementstmt 24.2% 41 / 169 Branchbran 0.0% 0 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 38.7% 12 / 31 Podpod 100% 18 / 18 totaltotal 25.7% 71 / 276 CC 31 SCAR 29.2
CC
31
Cov
18%
comp_roots
72.0
unset_component_attributes
30.0
register_component
20.0
CRAP 18.5
↑ Summary MasonX/Component/Interp.pm »
1 package MasonX::Component::Registry;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec::Functions qw/catdir catfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 METHODS
16  
17 =head2 register_component
18  
19 =over 4
20  
21 =item Arguments: $component_name, \%attributes
22  
23 =item Return Value:
24  
25 =back
26  
27 =cut
28  
29 0 ▶sub register_component {
Subroutine register_component: not called CC=4 SCAR=30.0
Pod documented
30 0 my ($self, $component, $attrs) = @_;
31  
32 0 my $component_path = $self->build_config_path($component);
33  
34 0 ▶ if (-f $component_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $component_path) 0 0 0
35 0 croak "Component $component already registered";
36 }
37  
38 0 ▶ my $fh = IO::File->new($component_path, 'w')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($component_path, "w") or croak("Failed to open component config `${component_path}' for writing: $!") 0 0 0
39 or croak "Failed to open component config `$component_path' for writing: $!";
40  
41 0 eval {
42 0 $self->writer->write_handle({
43 $component => $attrs,
44 }, $fh);
45 };
46  
47 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
48 0 $fh->close;
49 0 unlink $component_path;
50  
51 0 croak $error;
52 }
53  
54 0 $fh->close;
55  
56 0 return;
57 }
58  
59 =head2 install_component
60  
61 =over 4
62  
63 =item Arguments: $component_name, \%attrs
64  
65 =item Return Value: none
66  
67 =back
68  
69 =cut
70  
71 0 ▶sub install_component {
Subroutine install_component: not called CC=1 SCAR=6.9
Pod documented
72 0 my ($self, $component, $attrs) = @_;
73  
74 0 eval {
75 0 $self->unregister_component($component);
76 };
77  
78 #TODO: better error handling
79  
80 0 $self->register_component($component, $attrs);
81  
82 0 return;
83 }
84  
85 =head2 set_component_attributes
86  
87 =over 4
88  
89 =item Arguments: $component_name, \%attributes
90  
91 =item Return Value: none
92  
93 =back
94  
95 =cut
96  
97 0 ▶sub set_component_attributes {
Subroutine set_component_attributes: not called CC=3 SCAR=24.8
Pod documented
98 0 my ($self, $component, $attrs) = @_;
99  
100 0 my $overrides = $self->get_overrides;
101 0 my $new_overrides = Hash::Merge::merge({ $component => $attrs }, $overrides);
102  
103 0 eval {
104 0 $self->set_overrides($new_overrides);
105 };
106  
107 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
108 0 eval {
109 0 $self->set_overrides($overrides);
110 };
111  
112 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
113 }
114  
115 0 return;
116 }
117  
118 =head2 unset_component_attributes
119  
120 =over 4
121  
122 =item Arguments: $component_name, \@attribute_names
123  
124 =item Return Value: none
125  
126 =back
127  
128 =cut
129  
130 0 ▶sub unset_component_attributes {
Subroutine unset_component_attributes: not called CC=5 SCAR=34.0
Pod documented
131 0 my ($self, $component, $keys) = @_;
132  
133 0 my $overrides = $self->get_overrides;
134  
135 0 ▶ if (exists $overrides->{$component}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $overrides->{$component}) 0 0 0
136 0 my $new_overrides = Clone::clone($overrides);
137  
138 delete $new_overrides->{$component}->{$_}
139 0 for @{ $keys };
140  
141 0 eval {
142 0 $self->set_overrides($new_overrides);
143 };
144  
145 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
146 0 eval {
147 0 $self->set_overrides($overrides);
148 };
149  
150 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
151 }
152 }
153  
154 0 return;
155 }
156  
157 =head2 get_overrides
158  
159 =over 4
160  
161 =item Arguments: none
162  
163 =item Return Value: \%attributes
164  
165 =back
166  
167 =cut
168  
169 0 ▶sub get_overrides {
Subroutine get_overrides: not called CC=3 SCAR=24.8
Pod documented
170 0 my ($self) = @_;
171  
172 return +{}
173 0 ▶ if !-f $self->conf_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->conf_file 0 0 0
174  
175 0 ▶ my $fh = IO::File->new($self->conf_file, 'r')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->conf_file, "r") or croak("Failed to open config file for reading: $!") 0 0 0
176 or croak "Failed to open config file for reading: $!";
177  
178 0 my $overrides = $self->reader->read_handle($fh);
179  
180 0 $fh->close;
181 0 return $overrides;
182 }
183  
184 =head2 set_overrides
185  
186 =over 4
187  
188 =item Arguments: \%attributes
189  
190 =item Return Value: none
191  
192 =back
193  
194 =cut
195  
196 0 ▶sub set_overrides {
Subroutine set_overrides: not called CC=3 SCAR=24.8
Pod documented
197 0 my ($self, $overrides) = @_;
198  
199 0 ▶ my $fh = IO::File->new($self->conf_file, 'w')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->conf_file, "w") or croak("Failed to open config file for writing: $!") 0 0 0
200 or croak "Failed to open config file for writing: $!";
201  
202 0 eval {
203 0 $self->writer->write_handle($overrides, $fh);
204 };
205  
206 0 my $error = $@;
207 0 $fh->close;
208  
209 0 ▶ croak $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
210  
211 0 return;
212 }
213  
214 =head2 comp_roots
215  
216 =over 4
217  
218 =item Arguments: none
219  
220 =item Return Value: @comp_roots
221  
222 =back
223  
224 =cut
225  
226 0 ▶sub comp_roots {
Subroutine comp_roots: not called CC=8 SCAR=42.8
Pod documented
227 0 my ($self) = @_;
228  
229 0 ▶ my $dir = IO::Dir->new($self->conf_dir)
Branch: 1 branch
BranchTrueFalseTotal
my $dir = "IO::Dir"->new($self->conf_dir) or croak("Failed to open config directory: $!") 0 0 0
230 or croak "Failed to open config directory: $!";
231  
232 0 my @comp_roots;
233  
234 0 ▶ while (defined (my $file = $dir->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = $dir->read)) 0 0 0
235 0 ▶ next if $file =~ /^\.{1,2}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^\.{1,2}$/ 0 0 0
236  
237 0 my $attrs;
238 0 eval {
239 0 $attrs = $self->component_info($file);
240 };
241  
242 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
243 0 warn $error;
244 0 next;
245 }
246  
247 0 ▶ next unless defined $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 0 0 0
248 0 ▶ next unless defined $attrs->{comp_root};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs->{"comp_root"} 0 0 0
249  
250 0 my $moniker = $attrs->{moniker};
251  
252 0 ▶ if (!$moniker) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($moniker) 0 0 0
253 0 ($moniker = lc $file) =~ s/.*:://;
254 }
255  
256 #TODO: sort by priority field
257 0 push @comp_roots, [ $moniker => $attrs->{comp_root} ],
258 }
259  
260 0 $dir->close;
261  
262 0 return @comp_roots;
263 }
264  
265 =head2 unregister_component
266  
267 =over 4
268  
269 =item Arguments: $component_name
270  
271 =item Return Value:
272  
273 =back
274  
275 =cut
276  
277 0 ▶sub unregister_component {
Subroutine unregister_component: not called CC=2 SCAR=17.9
Pod documented
278 0 my ($self, $component) = @_;
279  
280 0 my $component_path = $self->build_config_path($component);
281 0 ▶ if (!unlink $component_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $component_path) 0 0 0
282 0 croak "Failed to remove component config: $!";
283 }
284  
285 0 return;
286 }
287  
288 =head2 component_info
289  
290 =over 4
291  
292 =item Arguments: $component_name
293  
294 =item Return Value: \%component_attributes
295  
296 =back
297  
298 =cut
299  
300 0 ▶sub component_info {
Subroutine component_info: not called CC=3 SCAR=24.8
Pod documented
301 0 my ($self, $component) = @_;
302  
303 0 my $component_path = $self->build_config_path($component);
304  
305 0 ▶ if (!-f $component_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $component_path) 0 0 0
306 0 croak "Component $component not registered";
307 }
308  
309 0 my $attrs = $self->read_file($component_path);
310 0 $attrs = $self->apply_overrides($attrs);
311  
312 0 ▶ if (!exists $attrs->{$component}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $attrs->{$component}) 0 0 0
313 0 croak "Component config didn't contain any information on requested component $component";
314 }
315  
316 0 return $attrs->{$component};
317 }
318  
319 =head2 apply_overrides
320  
321 =over 4
322  
323 =item Arguments: \%attributes
324  
325 =item Return Value: \%modified_attributes
326  
327 =back
328  
329 =cut
330  
331 0 ▶sub apply_overrides {
Subroutine apply_overrides: not called CC=1 SCAR=6.9
Pod documented
332 0 my ($self, $attrs) = @_;
333  
334 0 my $overrides = $self->get_overrides;
335  
336 0 return Hash::Merge::merge($overrides, $attrs);
337 }
338  
339 =head2 read_file
340  
341 =over 4
342  
343 =item Arguments: $path
344  
345 =item Return Value: \%attributes
346  
347 =back
348  
349 =cut
350  
351 0 ▶sub read_file {
Subroutine read_file: not called CC=2 SCAR=17.9
Pod documented
352 0 my ($self, $path) = @_;
353  
354 0 ▶ my $fh = IO::File->new($path, 'r')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($path, "r") or croak("Failed to open component config `${path}' for reading: $!") 0 0 0
355 or croak "Failed to open component config `$path' for reading: $!";
356  
357 0 my $attrs = $self->reader->read_handle($fh);
358  
359 0 $fh->close;
360  
361 0 return $attrs;
362 }
363  
364 =head2 conf_prefix
365  
366 =over 4
367  
368 =item Arguments: none
369  
370 =item Return Value: $path
371  
372 =back
373  
374 =cut
375  
376 0 ▶sub conf_prefix {
Subroutine conf_prefix: not called CC=3 SCAR=24.8
Pod documented
377 0 my ($self) = @_;
378  
379 0 my $dir;
380  
381 0 eval {
382 0 $dir = File::ShareDir::module_dir($self);
383 };
384  
385 0 ▶ if (my $error = $@) { #FIME: fragile
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
386 0 ($dir) = $error =~ /Directory '(.*)', does not exist/;
387 }
388  
389 0 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
390 0 File::Path::mkpath($dir);
391 }
392  
393 0 return $dir;
394 }
395  
396 =head2 conf_file
397  
398 =over 4
399  
400 =item Arguments: none
401  
402 =item Return Value: $path
403  
404 =back
405  
406 =cut
407  
408 0 ▶sub conf_file {
Subroutine conf_file: not called CC=1 SCAR=6.9
Pod documented
409 0 my ($self) = @_;
410  
411 0 return catfile($self->conf_prefix, 'registry.ini');
412 }
413  
414 =head2 conf_dir
415  
416 =over 4
417  
418 =item Arguments: none
419  
420 =item Return Value: $path
421  
422 =back
423  
424 =cut
425  
426 0 ▶sub conf_dir {
Subroutine conf_dir: not called CC=2 SCAR=17.9
Pod documented
427 0 my ($self) = @_;
428  
429 0 my $dir = catdir($self->conf_prefix, 'registry.d');
430  
431 0 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
432 0 File::Path::mkpath($dir);
433 }
434  
435 0 return $dir;
436 }
437  
438 =head2 build_config_path
439  
440 =over 4
441  
442 =item Arguments: $component_name
443  
444 =item Return Value: $path
445  
446 =back
447  
448 =cut
449  
450 0 ▶sub build_config_path {
Subroutine build_config_path: not called CC=1 SCAR=6.9
Pod documented
451 0 my ($self, $component) = @_;
452  
453 0 return catfile($self->conf_dir, $component);
454 }
455  
456 =head2 reader_class
457  
458 =over 4
459  
460 =item Arguments: none
461  
462 =item Return Value: $class
463  
464 =back
465  
466 =cut
467  
468 0 ▶sub reader_class {
Subroutine reader_class: not called CC=1 SCAR=6.9
Pod documented
469 0 return 'MasonX::Component::Registry::Reader';
470 }
471  
472 =head2 writer_class
473  
474 =over 4
475  
476 =item Arguments: none
477  
478 =item Return Value: $class
479  
480 =back
481  
482 =cut
483  
484 0 ▶sub writer_class {
Subroutine writer_class: not called CC=1 SCAR=6.9
Pod documented
485 0 return 'MasonX::Component::Registry::Writer';
486 }
487  
488 =head2 reader
489  
490 =over 4
491  
492 =item Arguments: none
493  
494 =item Return Value: $class
495  
496 =back
497  
498 =head2 writer
499  
500 =over 4
501  
502 =item Arguments: none
503  
504 =item Return Value: $class
505  
506 =back
507  
508 =cut
509  
510 {
511 1 for my $meth (qw/reader writer/) {
512 1 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
513  
514 2 ▶ *{ $meth } = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.6
515 0 my ($self) = @_;
516  
517 0 my $get_class = "${meth}_class";
518 0 my $class = $self->$get_class;
519  
520 0 $self->ensure_class_loaded($class);
521  
522 0 return $class;
523 2 };
524 }
525 }
526  
527 =head2 ensure_class_loaded
528  
529 =over 4
530  
531 =item Arguments: none
532  
533 =item Return Value: none
534  
535 =back
536  
537 =cut
538  
539 0 ▶sub ensure_class_loaded {
Subroutine ensure_class_loaded: not called CC=4 SCAR=30.0
Pod documented
540 0 my ($self, $class) = @_;
541  
542 0 ▶ croak "Invalid class name $class"
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /(?:\b:\b|\:{3,})/ 0 0 0
543 if $class =~ /(?:\b:\b|\:{3,})/;
544  
545 0 ▶ return if Class::Inspector->loaded($class);
Branch: 1 branch
BranchTrueFalseTotal
if "Class::Inspector"->loaded($class) 0 0 0
546  
547 0 eval "require $class";
548 0 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
549 0 croak $error;
550 }
551  
552 0 return;
553 }
554  
555 1 1;
↑ Summary MasonX/Component/Interp.pm »