blib/lib/Venus/Config.pm

filter: Statementstmt 29.8% 23 / 77 Branchbran 10.0% 1 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.5% 6 / 21 Podpod 93.7% 15 / 16 totaltotal 35.6% 46 / 129 SCAR 32.4
CC
7
Cov
28%
read_file
12.0
write_file
12.0
build_args
3.8
CRAP 25.5
« blib/lib/Venus/Role/Serializable.pm ↑ Summary blib/lib/Venus/Try.pm »
1 package Venus::Config;
2  
3 1 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Venus::Class 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 base 'Venus::Kind::Utility';
11  
12 1 with 'Venus::Role::Buildable';
13 1 with 'Venus::Role::Valuable';
14  
15 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 state $reader = {
18 js => 'read_json_file',
19 json => 'read_json_file',
20 perl => 'read_perl_file',
21 pl => 'read_perl_file',
22 yaml => 'read_yaml_file',
23 yml => 'read_yaml_file',
24 };
25  
26 1 state $writer = {
27 js => 'write_json_file',
28 json => 'write_json_file',
29 perl => 'write_perl_file',
30 pl => 'write_perl_file',
31 yaml => 'write_yaml_file',
32 yml => 'write_yaml_file',
33 };
34  
35 # BUILDERS
36  
37 sub build_args {
38 4 ▶ my ($self, $data) = @_;
Subroutine build_args: called CC=3 SCAR=13.3
Pod undocumented
39  
40 4 ▶ if (keys %$data == 1 && exists $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"value"}) 0 4 4
Condition33%1/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
400
MC/DC0%0/2
keys %$data == 1 and exists $data->{"value"}
keys %$data == 1 exists $data->{"value"}
Truth table33%1/3
A and B
A= keys %$data == 1B= exists $data->{"value"}
ABresult
0X0
100
111
41 0 return $data;
42 }
43 return {
44 4 value => $data
45 };
46 }
47  
48 # METHODS
49  
50 sub edit_file {
51 0 ▶ my ($self, $file, $code) = @_;
Subroutine edit_file: not called CC=1 SCAR=6.9
Pod documented
52  
53 0 $self = $self->read_file($file);
54  
55 0 $self->value($self->$code($self->value));
56  
57 0 return $self->write_file($file);
58 }
59  
60 sub read_file {
61 0 ▶ my ($self, $file) = @_;
Subroutine read_file: not called CC=3 SCAR=24.8
Pod documented
62  
63 0 ▶ if (!$file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file) { } 0 0 0
elsif (my $method = $$reader{(split(/\./u, $file, 0))[-1]}) { } 0 0 0
64 0 return $self->class->new;
65 }
66 elsif (my $method = $reader->{(split/\./, $file)[-1]}) {
67 0 return $self->$method($file);
68 }
69 else {
70 0 return $self->class->new;
71 }
72 }
73  
74 sub read_json {
75 0 ▶ my ($self, $data) = @_;
Subroutine read_json: not called CC=1 SCAR=6.9
Pod documented
76  
77 0 require Venus::Json;
78  
79 0 return $self->class->new(Venus::Json->new->decode($data));
80 }
81  
82 sub read_json_file {
83 0 ▶ my ($self, $file) = @_;
Subroutine read_json_file: not called CC=1 SCAR=6.9
Pod documented
84  
85 0 require Venus::Path;
86  
87 0 return $self->read_json(Venus::Path->new($file)->read);
88 }
89  
90 sub read_perl {
91 0 ▶ my ($self, $data) = @_;
Subroutine read_perl: not called CC=1 SCAR=6.9
Pod documented
92  
93 0 require Venus::Dump;
94  
95 0 return $self->class->new(Venus::Dump->new->decode($data));
96 }
97  
98 sub read_perl_file {
99 0 ▶ my ($self, $file) = @_;
Subroutine read_perl_file: not called CC=1 SCAR=6.9
Pod documented
100  
101 0 require Venus::Path;
102  
103 0 return $self->read_perl(Venus::Path->new($file)->read);
104 }
105  
106 sub read_yaml {
107 0 ▶ my ($self, $data) = @_;
Subroutine read_yaml: not called CC=1 SCAR=6.9
Pod documented
108  
109 0 require Venus::Yaml;
110  
111 0 return $self->class->new(Venus::Yaml->new->decode($data));
112 }
113  
114 sub read_yaml_file {
115 0 ▶ my ($self, $file) = @_;
Subroutine read_yaml_file: not called CC=1 SCAR=6.9
Pod documented
116  
117 0 require Venus::Path;
118  
119 0 return $self->read_yaml(Venus::Path->new($file)->read);
120 }
121  
122 sub write_file {
123 0 ▶ my ($self, $file) = @_;
Subroutine write_file: not called CC=3 SCAR=24.8
Pod documented
124  
125 0 ▶ if (!$file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file) { } 0 0 0
elsif (my $method = $$writer{(split(/\./u, $file, 0))[-1]}) { } 0 0 0
126 0 return $self->class->new;
127 }
128 elsif (my $method = $writer->{(split/\./, $file)[-1]}) {
129 0 return $self->do($method, $file);
130 }
131 else {
132 0 return $self->class->new;
133 }
134 }
135  
136 sub write_json {
137 0 ▶ my ($self) = @_;
Subroutine write_json: not called CC=1 SCAR=6.9
Pod documented
138  
139 0 require Venus::Json;
140  
141 0 return Venus::Json->new($self->value)->encode;
142 }
143  
144 sub write_json_file {
145 0 ▶ my ($self, $file) = @_;
Subroutine write_json_file: not called CC=1 SCAR=6.9
Pod documented
146  
147 0 require Venus::Path;
148  
149 0 Venus::Path->new($file)->write($self->write_json);
150  
151 0 return $self;
152 }
153  
154 sub write_perl {
155 0 ▶ my ($self) = @_;
Subroutine write_perl: not called CC=1 SCAR=6.9
Pod documented
156  
157 0 require Venus::Dump;
158  
159 0 return Venus::Dump->new($self->value)->encode;
160 }
161  
162 sub write_perl_file {
163 0 ▶ my ($self, $file) = @_;
Subroutine write_perl_file: not called CC=1 SCAR=6.9
Pod documented
164  
165 0 require Venus::Path;
166  
167 0 Venus::Path->new($file)->write($self->write_perl);
168  
169 0 return $self;
170 }
171  
172 sub write_yaml {
173 0 ▶ my ($self) = @_;
Subroutine write_yaml: not called CC=1 SCAR=6.9
Pod documented
174  
175 0 require Venus::Yaml;
176  
177 0 return Venus::Yaml->new($self->value)->encode;
178 }
179  
180 sub write_yaml_file {
181 0 ▶ my ($self, $file) = @_;
Subroutine write_yaml_file: not called CC=1 SCAR=6.9
Pod documented
182  
183 0 require Venus::Path;
184  
185 0 Venus::Path->new($file)->write($self->write_yaml);
186  
187 0 return $self;
188 }
189  
190 1 1;
191  
192  
193  
194 =head1 NAME
195  
196 Venus::Config - Config Class
197  
198 =cut
199  
200 =head1 ABSTRACT
201  
202 Config Class for Perl 5
203  
204 =cut
205  
206 =head1 SYNOPSIS
207  
208 package main;
209  
210 use Venus::Config;
211  
212 my $config = Venus::Config->new;
213  
214 # $config = $config->read_file('app.pl');
215  
216 # "..."
217  
218 =cut
219  
220 =head1 DESCRIPTION
221  
222 This package provides methods for loading Perl, YAML, and JSON configuration
223 files, and fetching configuration information.
224  
225 =cut
226  
227 =head1 INHERITS
228  
229 This package inherits behaviors from:
230  
231 L<Venus::Kind::Utility>
232  
233 =cut
234  
235 =head1 INTEGRATES
236  
237 This package integrates behaviors from:
238  
239 L<Venus::Role::Buildable>
240  
241 L<Venus::Role::Valuable>
242  
243 =cut
244  
245 =head1 METHODS
246  
247 This package provides the following methods:
248  
249 =cut
250  
251 =head2 edit_file
252  
253 edit_file(string $file, string | coderef $code) (Venus::Config)
254  
255 The edit_file method does an in-place edit, i.e. it loads a Perl, YAML, or JSON
256 configuration file, passes the decoded data to the method or callback provided,
257 and writes the results of the method or callback to the file.
258  
259 I<Since C<3.10>>
260  
261 =over 4
262  
263 =item edit_file example 1
264  
265 package main;
266  
267 use Venus::Config;
268  
269 my $config = Venus::Config->edit_file('t/conf/edit.perl', sub {
270 my ($self, $data) = @_;
271  
272 $data->{edited} = 1;
273  
274 return $data;
275 });
276  
277 # bless(..., 'Venus::Config')
278  
279 =back
280  
281 =cut
282  
283 =head2 read_file
284  
285 read_file(string $path) (Venus::Config)
286  
287 The read_file method load a Perl, YAML, or JSON configuration file, based on
288 the file extension, and returns a new L<Venus::Config> object.
289  
290 I<Since C<2.91>>
291  
292 =over 4
293  
294 =item read_file example 1
295  
296 package main;
297  
298 use Venus::Config;
299  
300 my $config = Venus::Config->read_file('t/conf/read.perl');
301  
302 # bless(..., 'Venus::Config')
303  
304 =back
305  
306 =over 4
307  
308 =item read_file example 2
309  
310 package main;
311  
312 use Venus::Config;
313  
314 my $config = Venus::Config->read_file('t/conf/read.json');
315  
316 # bless(..., 'Venus::Config')
317  
318 =back
319  
320 =over 4
321  
322 =item read_file example 3
323  
324 package main;
325  
326 use Venus::Config;
327  
328 my $config = Venus::Config->read_file('t/conf/read.yaml');
329  
330 # bless(..., 'Venus::Config')
331  
332 =back
333  
334 =cut
335  
336 =head2 read_json
337  
338 read_json(string $data) (Venus::Config)
339  
340 The read_json method returns a new L<Venus::Config> object based on the JSON
341 string provided.
342  
343 I<Since C<2.91>>
344  
345 =over 4
346  
347 =item read_json example 1
348  
349 # given: synopsis
350  
351 package main;
352  
353 $config = $config->read_json(q(
354 {
355 "$metadata": {
356 "tmplog": "/tmp/log"
357 },
358 "$services": {
359 "log": { "package": "Venus/Path", "argument": { "$metadata": "tmplog" } }
360 }
361 }
362 ));
363  
364 # bless(..., 'Venus::Config')
365  
366 =back
367  
368 =cut
369  
370 =head2 read_json_file
371  
372 read_json_file(string $file) (Venus::Config)
373  
374 The read_json_file method uses L<Venus::Path> to return a new L<Venus::Config>
375 object based on the file provided.
376  
377 I<Since C<2.91>>
378  
379 =over 4
380  
381 =item read_json_file example 1
382  
383 # given: synopsis
384  
385 package main;
386  
387 $config = $config->read_json_file('t/conf/read.json');
388  
389 # bless(..., 'Venus::Config')
390  
391 =back
392  
393 =cut
394  
395 =head2 read_perl
396  
397 read_perl(string $data) (Venus::Config)
398  
399 The read_perl method returns a new L<Venus::Config> object based on the Perl
400 string provided.
401  
402 I<Since C<2.91>>
403  
404 =over 4
405  
406 =item read_perl example 1
407  
408 # given: synopsis
409  
410 package main;
411  
412 $config = $config->read_perl(q(
413 {
414 '$metadata' => {
415 tmplog => "/tmp/log"
416 },
417 '$services' => {
418 log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } }
419 }
420 }
421 ));
422  
423 # bless(..., 'Venus::Config')
424  
425 =back
426  
427 =cut
428  
429 =head2 read_perl_file
430  
431 read_perl_file(string $file) (Venus::Config)
432  
433 The read_perl_file method uses L<Venus::Path> to return a new L<Venus::Config>
434 object based on the file provided.
435  
436 I<Since C<2.91>>
437  
438 =over 4
439  
440 =item read_perl_file example 1
441  
442 # given: synopsis
443  
444 package main;
445  
446 $config = $config->read_perl_file('t/conf/read.perl');
447  
448 # bless(..., 'Venus::Config')
449  
450 =back
451  
452 =cut
453  
454 =head2 read_yaml
455  
456 read_yaml(string $data) (Venus::Config)
457  
458 The read_yaml method returns a new L<Venus::Config> object based on the YAML
459 string provided.
460  
461 I<Since C<2.91>>
462  
463 =over 4
464  
465 =item read_yaml example 1
466  
467 # given: synopsis
468  
469 package main;
470  
471 $config = $config->read_yaml(q(
472 '$metadata':
473 tmplog: /tmp/log
474 '$services':
475 log:
476 package: "Venus/Path"
477 argument:
478 '$metadata': tmplog
479 ));
480  
481 # bless(..., 'Venus::Config')
482  
483 =back
484  
485 =cut
486  
487 =head2 read_yaml_file
488  
489 read_yaml_file(string $file) (Venus::Config)
490  
491 The read_yaml_file method uses L<Venus::Path> to return a new L<Venus::Config>
492 object based on the YAML string provided.
493  
494 I<Since C<2.91>>
495  
496 =over 4
497  
498 =item read_yaml_file example 1
499  
500 # given: synopsis
501  
502 package main;
503  
504 $config = $config->read_yaml_file('t/conf/read.yaml');
505  
506 # bless(..., 'Venus::Config')
507  
508 =back
509  
510 =cut
511  
512 =head2 write_file
513  
514 write_file(string $path) (Venus::Config)
515  
516 The write_file method saves a Perl, YAML, or JSON configuration file, based on
517 the file extension, and returns a new L<Venus::Config> object.
518  
519 I<Since C<2.91>>
520  
521 =over 4
522  
523 =item write_file example 1
524  
525 # given: synopsis
526  
527 my $value = $config->value({
528 '$services' => {
529 log => { package => "Venus/Path", argument => { value => "." } }
530 }
531 });
532  
533 $config = $config->write_file('t/conf/write.perl');
534  
535 # bless(..., 'Venus::Config')
536  
537 =back
538  
539 =over 4
540  
541 =item write_file example 2
542  
543 # given: synopsis
544  
545 my $value = $config->value({
546 '$metadata' => {
547 tmplog => "/tmp/log"
548 },
549 '$services' => {
550 log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } }
551 }
552 });
553  
554 $config = $config->write_file('t/conf/write.json');
555  
556 # bless(..., 'Venus::Config')
557  
558 =back
559  
560 =over 4
561  
562 =item write_file example 3
563  
564 # given: synopsis
565  
566 my $value = $config->value({
567 '$metadata' => {
568 tmplog => "/tmp/log"
569 },
570 '$services' => {
571 log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } }
572 }
573 });
574  
575 $config = $config->write_file('t/conf/write.yaml');
576  
577 # bless(..., 'Venus::Config')
578  
579 =back
580  
581 =cut
582  
583 =head2 write_json
584  
585 write_json() (string)
586  
587 The write_json method returns a JSON encoded string based on the L</value> held
588 by the underlying L<Venus::Config> object.
589  
590 I<Since C<2.91>>
591  
592 =over 4
593  
594 =item write_json example 1
595  
596 # given: synopsis
597  
598 my $value = $config->value({
599 '$services' => {
600 log => { package => "Venus::Path" },
601 },
602 });
603  
604 my $json = $config->write_json;
605  
606 # '{ "$services":{ "log":{ "package":"Venus::Path" } } }'
607  
608 =back
609  
610 =cut
611  
612 =head2 write_json_file
613  
614 write_json_file(string $path) (Venus::Config)
615  
616 The write_json_file method saves a JSON configuration file and returns a new
617 L<Venus::Config> object.
618  
619 I<Since C<2.91>>
620  
621 =over 4
622  
623 =item write_json_file example 1
624  
625 # given: synopsis
626  
627 my $value = $config->value({
628 '$services' => {
629 log => { package => "Venus/Path", argument => { value => "." } }
630 }
631 });
632  
633 $config = $config->write_json_file('t/conf/write.json');
634  
635 # bless(..., 'Venus::Config')
636  
637 =back
638  
639 =cut
640  
641 =head2 write_perl
642  
643 write_perl() (string)
644  
645 The write_perl method returns a FILE encoded string based on the L</value> held
646 by the underlying L<Venus::Config> object.
647  
648 I<Since C<2.91>>
649  
650 =over 4
651  
652 =item write_perl example 1
653  
654 # given: synopsis
655  
656 my $value = $config->value({
657 '$services' => {
658 log => { package => "Venus::Path" },
659 },
660 });
661  
662 my $perl = $config->write_perl;
663  
664 # '{ "\$services" => { log => { package => "Venus::Path" } } }'
665  
666 =back
667  
668 =cut
669  
670 =head2 write_perl_file
671  
672 write_perl_file(string $path) (Venus::Config)
673  
674 The write_perl_file method saves a Perl configuration file and returns a new
675 L<Venus::Config> object.
676  
677 I<Since C<2.91>>
678  
679 =over 4
680  
681 =item write_perl_file example 1
682  
683 # given: synopsis
684  
685 my $value = $config->value({
686 '$services' => {
687 log => { package => "Venus/Path", argument => { value => "." } }
688 }
689 });
690  
691 $config = $config->write_perl_file('t/conf/write.perl');
692  
693 # bless(..., 'Venus::Config')
694  
695 =back
696  
697 =cut
698  
699 =head2 write_yaml
700  
701 write_yaml() (string)
702  
703 The write_yaml method returns a FILE encoded string based on the L</value> held
704 by the underlying L<Venus::Config> object.
705  
706 I<Since C<2.91>>
707  
708 =over 4
709  
710 =item write_yaml example 1
711  
712 # given: synopsis
713  
714 my $value = $config->value({
715 '$services' => {
716 log => { package => "Venus::Path" },
717 },
718 });
719  
720 my $yaml = $config->write_yaml;
721  
722 # '---\n$services:\n\s\slog:\n\s\s\s\spackage:\sVenus::Path'
723  
724 =back
725  
726 =cut
727  
728 =head2 write_yaml_file
729  
730 write_yaml_file(string $path) (Venus::Config)
731  
732 The write_yaml_file method saves a YAML configuration file and returns a new
733 L<Venus::Config> object.
734  
735 I<Since C<2.91>>
736  
737 =over 4
738  
739 =item write_yaml_file example 1
740  
741 # given: synopsis
742  
743 my $value = $config->value({
744 '$services' => {
745 log => { package => "Venus/Path", argument => { value => "." } }
746 }
747 });
748  
749 $config = $config->write_yaml_file('t/conf/write.yaml');
750  
751 # bless(..., 'Venus::Config')
752  
753 =back
754  
755 =cut
756  
757 =head1 AUTHORS
758  
759 Awncorp, C<awncorp@cpan.org>
760  
761 =cut
762  
763 =head1 LICENSE
764  
765 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
766  
767 This program is free software, you can redistribute it and/or modify it under
768 the terms of the Apache license version 2.0.
769  
770 =cut
« blib/lib/Venus/Role/Serializable.pm ↑ Summary blib/lib/Venus/Try.pm »