blib/lib/Venus/Config.pm

filter: Statementstmt 90.7% 147 / 162 Branchbran 70.0% 42 / 60 Conditioncond 48.2% 14 / 29 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 100% 25 / 25 Podpod 95.0% 19 / 20 totaltotal 80.0% 252 / 315 CC 42 SCAR 33.0
CC
42
Cov
81%
read_env
53.6
write_env
5.1
read_file
3.8
CRAP 27.1
« blib/lib/Venus/Role/Mappable.pm ↑ Summary blib/lib/Venus/Factory.pm »
1 package Venus::Config;
2  
3 9 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 9 ▶use Venus::Class 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 9 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 9 with 'Venus::Role::Buildable';
19 9 with 'Venus::Role::Valuable';
20  
21 9 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # STATE
24  
25 9 state $reader = {
26 env => 'read_env_file',
27 js => 'read_json_file',
28 json => 'read_json_file',
29 perl => 'read_perl_file',
30 pl => 'read_perl_file',
31 yaml => 'read_yaml_file',
32 yml => 'read_yaml_file',
33 };
34  
35 9 state $writer = {
36 env => 'write_env_file',
37 js => 'write_json_file',
38 json => 'write_json_file',
39 perl => 'write_perl_file',
40 pl => 'write_perl_file',
41 yaml => 'write_yaml_file',
42 yml => 'write_yaml_file',
43 };
44  
45 # BUILDERS
46  
47 154 ▶sub build_args {
Subroutine build_args: called CC=3 SCAR=11.0
Pod undocumented
48 154 my ($self, $data) = @_;
49  
50 154 ▶ if (keys %$data == 1 && exists $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"value"}) 1 153 154
Condition100%3/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
137161
MC/DC100%2/2
keys %$data == 1 and exists $data->{"value"}
keys %$data == 1 exists $data->{"value"}
Truth table100%3/3
A and B
A= keys %$data == 1B= exists $data->{"value"}
ABresult
0X0
100
111
51 1 return $data;
52 }
53 return {
54 153 value => $data
55 };
56 }
57  
58 # METHODS
59  
60 2 ▶sub edit_file {
Subroutine edit_file: called CC=1 SCAR=0.0
Pod documented
61 2 my ($self, $file, $code) = @_;
62  
63 2 $self = $self->read_file($file);
64  
65 2 $self->value($self->$code($self->value));
66  
67 2 return $self->write_file($file);
68 }
69  
70 9 ▶sub read_env {
Subroutine read_env: called CC=32 SCAR=39.8
Pod documented
71 9 my ($self, $lines) = @_;
72  
73 9 my $data = {};
74  
75 9 ▶ my $content = $lines // '';
Condition50%1/2
$lines // ""
l!l
90
MC/DC0%0/1
$lines // ""
$lines
Truth table50%1/2
A
A= $lines
Aresult
11
00
76 9 my $length = length($content);
77 9 my $pos = 0;
78  
79 9 ▶ while ($pos < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length) 59 9 68
80 # Skip whitespace and newlines
81 59 ▶ if (substr($content, $pos, 1) =~ /[\s\n]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($content, $pos, 1) =~ /[\s\n]/u) 29 30 59
82 29 $pos++;
83 29 next;
84 }
85  
86 # Skip comments (lines starting with #)
87 30 ▶ if (substr($content, $pos, 1) eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($content, $pos, 1) eq "#") 6 24 30
88 6 ▶ while ($pos < $length && substr($content, $pos, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length and substr($content, $pos, 1) ne "\n") 60 6 66
Condition66%2/3
$pos < $length and substr($content, $pos, 1) ne "\n"
!ll&&!rl&&r
0660
MC/DC50%1/2
$pos < $length and substr($content, $pos, 1) ne "\n"
$pos < $length substr($content, $pos, 1) ne "\n"
Truth table66%2/3
A and B
A= $pos < $lengthB= substr($content, $pos, 1) ne "\n"
ABresult
0X0
100
111
89 60 $pos++;
90 }
91 6 next;
92 }
93  
94 # Parse key (alphanumeric, underscore, and dot)
95 24 my $key = '';
96 24 ▶ while ($pos < $length && substr($content, $pos, 1) =~ /[\w\.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length and substr($content, $pos, 1) =~ /[\w\.]/u) 156 24 180
Condition66%2/3
$pos < $length and substr($content, $pos, 1) =~ /[\w\.]/u
!ll&&!rl&&r
024156
MC/DC50%1/2
$pos < $length and substr($content, $pos, 1) =~ /[\w\.]/u
$pos < $length substr($content, $pos, 1) =~ /[\w\.]/u
Truth table66%2/3
A and B
A= $pos < $lengthB= substr($content, $pos, 1) =~ /[\w\.]/u
ABresult
0X0
100
111
97 156 $key .= substr($content, $pos, 1);
98 156 $pos++;
99 }
100  
101 # Skip if no key found
102 24 ▶ next if !length($key);
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 24 24
103  
104 # Skip whitespace before =
105 24 ▶ while ($pos < $length && substr($content, $pos, 1) =~ /[ \t]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length and substr($content, $pos, 1) =~ /[ \t]/u) 0 24 24
Condition33%1/3
$pos < $length and substr($content, $pos, 1) =~ /[ \t]/u
!ll&&!rl&&r
0240
MC/DC0%0/2
$pos < $length and substr($content, $pos, 1) =~ /[ \t]/u
$pos < $length substr($content, $pos, 1) =~ /[ \t]/u
Truth table33%1/3
A and B
A= $pos < $lengthB= substr($content, $pos, 1) =~ /[ \t]/u
ABresult
0X0
100
111
106 0 $pos++;
107 }
108  
109 # Expect =
110 24 ▶ if ($pos >= $length || substr($content, $pos, 1) ne '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= $length or substr($content, $pos, 1) ne "=") 0 24 24
Condition33%1/3
$pos >= $length or substr($content, $pos, 1) ne "="
l!l&&r!l&&!r
0024
MC/DC0%0/2
$pos >= $length or substr($content, $pos, 1) ne "="
$pos >= $length substr($content, $pos, 1) ne "="
Truth table33%1/3
A or B
A= $pos >= $lengthB= substr($content, $pos, 1) ne "="
ABresult
1X1
011
000
111 # Skip to end of line if no =
112 0 ▶ while ($pos < $length && substr($content, $pos, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length and substr($content, $pos, 1) ne "\n") 0 0 0
Condition0%0/3
$pos < $length and substr($content, $pos, 1) ne "\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$pos < $length and substr($content, $pos, 1) ne "\n"
$pos < $length substr($content, $pos, 1) ne "\n"
Truth table0%0/3
A and B
A= $pos < $lengthB= substr($content, $pos, 1) ne "\n"
ABresult
0X0
100
111
113 0 $pos++;
114 }
115 0 next;
116 }
117 24 $pos++; # Skip =
118  
119 # Skip whitespace after =
120 24 ▶ while ($pos < $length && substr($content, $pos, 1) =~ /[ \t]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length and substr($content, $pos, 1) =~ /[ \t]/u) 0 24 24
Condition33%1/3
$pos < $length and substr($content, $pos, 1) =~ /[ \t]/u
!ll&&!rl&&r
0240
MC/DC0%0/2
$pos < $length and substr($content, $pos, 1) =~ /[ \t]/u
$pos < $length substr($content, $pos, 1) =~ /[ \t]/u
Truth table33%1/3
A and B
A= $pos < $lengthB= substr($content, $pos, 1) =~ /[ \t]/u
ABresult
0X0
100
111
121 0 $pos++;
122 }
123  
124 # Parse value
125 24 my $value = '';
126 24 my $char = substr($content, $pos, 1);
127  
128 24 ▶ if ($char eq '"') {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '"') { } 7 17 24
elsif ($char eq q[']) { } 1 16 17
129 # Double-quoted value (can be multiline)
130 7 $pos++; # Skip opening quote
131 7 ▶ while ($pos < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length) 87 0 87
132 87 $char = substr($content, $pos, 1);
133 87 ▶ if ($char eq '\\' && $pos + 1 < $length) {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '\\' and $pos + 1 < $length) { } 4 83 87
elsif ($char eq '"') { } 7 76 83
Condition66%2/3
$char eq "\\" and $pos + 1 < $length
!ll&&!rl&&r
8304
MC/DC50%1/2
$char eq "\\" and $pos + 1 < $length
$char eq "\\" $pos + 1 < $length
Truth table66%2/3
A and B
A= $char eq "\\"B= $pos + 1 < $length
ABresult
0X0
100
111
134 # Handle escape sequences
135 4 my $next = substr($content, $pos + 1, 1);
136 4 ▶ if ($next eq 'n') {
Branch: 4 branches
BranchTrueFalseTotal
if ($next eq 'n') { } 3 1 4
elsif ($next eq 't') { } 1 0 1
elsif ($next eq '"') { } 0 0 0
elsif ($next eq '\\') { } 0 0 0
137 3 $value .= "\n";
138 3 $pos += 2;
139 }
140 elsif ($next eq 't') {
141 1 $value .= "\t";
142 1 $pos += 2;
143 }
144 elsif ($next eq '"') {
145 0 $value .= '"';
146 0 $pos += 2;
147 }
148 elsif ($next eq '\\') {
149 0 $value .= '\\';
150 0 $pos += 2;
151 }
152 else {
153 0 $value .= $char;
154 0 $pos++;
155 }
156 }
157 elsif ($char eq '"') {
158 7 $pos++; # Skip closing quote
159 7 last;
160 }
161 else {
162 76 $value .= $char;
163 76 $pos++;
164 }
165 }
166 }
167 elsif ($char eq "'") {
168 # Single-quoted value (can be multiline, no escape processing)
169 1 $pos++; # Skip opening quote
170 1 ▶ while ($pos < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length) 11 0 11
171 11 $char = substr($content, $pos, 1);
172 11 ▶ if ($char eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq q[']) { } 1 10 11
173 1 $pos++; # Skip closing quote
174 1 last;
175 }
176 else {
177 10 $value .= $char;
178 10 $pos++;
179 }
180 }
181 }
182 else {
183 # Unquoted value (single line, until whitespace or comment)
184 16 ▶ while ($pos < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $length) 111 2 113
185 111 $char = substr($content, $pos, 1);
186 111 ▶ last if $char =~ /[\s#\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[\s#\n]/u 14 97 111
187 97 $value .= $char;
188 97 $pos++;
189 }
190 }
191  
192 24 $data->{$key} = $value;
193 }
194  
195 9 return $self->class->new($data);
196 }
197  
198 4 ▶sub read_env_file {
Subroutine read_env_file: called CC=1 SCAR=0.0
Pod documented
199 4 my ($self, $file) = @_;
200  
201 4 return $self->read_env(Venus::Path->new($file)->read);
202 }
203  
204 77 ▶sub read_file {
Subroutine read_file: called CC=3 SCAR=13.3
Pod documented
205 77 my ($self, $file) = @_;
206  
207 77 ▶ if (!$file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file) { } 0 77 77
elsif (my $method = $$reader{(split(/\./u, $file, 0))[-1]}) { } 77 0 77
208 0 return $self->class->new;
209 }
210 elsif (my $method = $reader->{(split/\./, $file)[-1]}) {
211 77 return $self->$method($file);
212 }
213 else {
214 0 return $self->class->new;
215 }
216 }
217  
218 8 ▶sub read_json {
Subroutine read_json: called CC=1 SCAR=0.0
Pod documented
219 8 my ($self, $data) = @_;
220  
221 8 require Venus::Json;
222  
223 8 return $self->class->new(Venus::Json->new->decode($data));
224 }
225  
226 6 ▶sub read_json_file {
Subroutine read_json_file: called CC=1 SCAR=0.0
Pod documented
227 6 my ($self, $file) = @_;
228  
229 6 require Venus::Path;
230  
231 6 return $self->read_json(Venus::Path->new($file)->read);
232 }
233  
234 72 ▶sub read_perl {
Subroutine read_perl: called CC=1 SCAR=0.0
Pod documented
235 72 my ($self, $data) = @_;
236  
237 72 require Venus::Dump;
238  
239 72 return $self->class->new(Venus::Dump->new->decode($data));
240 }
241  
242 69 ▶sub read_perl_file {
Subroutine read_perl_file: called CC=1 SCAR=0.0
Pod documented
243 69 my ($self, $file) = @_;
244  
245 69 require Venus::Path;
246  
247 69 return $self->read_perl(Venus::Path->new($file)->read);
248 }
249  
250 8 ▶sub read_yaml {
Subroutine read_yaml: called CC=1 SCAR=0.0
Pod documented
251 8 my ($self, $data) = @_;
252  
253 8 require Venus::Yaml;
254  
255 8 return $self->class->new(Venus::Yaml->new->decode($data));
256 }
257  
258 6 ▶sub read_yaml_file {
Subroutine read_yaml_file: called CC=1 SCAR=0.0
Pod documented
259 6 my ($self, $file) = @_;
260  
261 6 require Venus::Path;
262  
263 6 return $self->read_yaml(Venus::Path->new($file)->read);
264 }
265  
266 6 ▶sub write_env {
Subroutine write_env: called CC=5 SCAR=16.2
Pod documented
267 6 my ($self) = @_;
268  
269 6 my @data;
270  
271 6 for my $key (sort keys %{$self->value}) {
272 17 my $value = $self->value->{$key};
273  
274 17 ▶ next if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 17 17
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
0017
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
275  
276 # Check if value needs quoting (contains whitespace, newlines, or special chars)
277 17 my $needs_quotes = $value =~ /[\s\n\t"'#\\]/;
278  
279 17 ▶ if ($needs_quotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_quotes) 3 14 17
280 # Escape special characters for double-quoted values
281 3 $value =~ s/\\/\\\\/g;
282 3 $value =~ s/"/\\"/g;
283 3 $value =~ s/\n/\\n/g;
284 3 $value =~ s/\t/\\t/g;
285 3 $value = qq("$value");
286 }
287  
288 17 push @data, "$key=$value";
289 }
290  
291 6 return join "\n", @data;
292 }
293  
294 2 ▶sub write_env_file {
Subroutine write_env_file: called CC=1 SCAR=0.0
Pod documented
295 2 my ($self, $file) = @_;
296  
297 2 Venus::Path->new($file)->write($self->write_env);
298  
299 2 return $self;
300 }
301  
302 14 ▶sub write_file {
Subroutine write_file: called CC=3 SCAR=13.3
Pod documented
303 14 my ($self, $file) = @_;
304  
305 14 ▶ if (!$file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file) { } 0 14 14
elsif (my $method = $$writer{(split(/\./u, $file, 0))[-1]}) { } 14 0 14
306 0 return $self->class->new;
307 }
308 elsif (my $method = $writer->{(split/\./, $file)[-1]}) {
309 14 return $self->do($method, $file);
310 }
311 else {
312 0 return $self->class->new;
313 }
314 }
315  
316 7 ▶sub write_json {
Subroutine write_json: called CC=1 SCAR=0.0
Pod documented
317 7 my ($self) = @_;
318  
319 7 require Venus::Json;
320  
321 7 return Venus::Json->new($self->value)->encode;
322 }
323  
324 5 ▶sub write_json_file {
Subroutine write_json_file: called CC=1 SCAR=0.0
Pod documented
325 5 my ($self, $file) = @_;
326  
327 5 require Venus::Path;
328  
329 5 Venus::Path->new($file)->write($self->write_json);
330  
331 5 return $self;
332 }
333  
334 12 ▶sub write_perl {
Subroutine write_perl: called CC=1 SCAR=0.0
Pod documented
335 12 my ($self) = @_;
336  
337 12 require Venus::Dump;
338  
339 12 return Venus::Dump->new($self->value)->encode;
340 }
341  
342 10 ▶sub write_perl_file {
Subroutine write_perl_file: called CC=1 SCAR=0.0
Pod documented
343 10 my ($self, $file) = @_;
344  
345 10 require Venus::Path;
346  
347 10 Venus::Path->new($file)->write($self->write_perl);
348  
349 10 return $self;
350 }
351  
352 7 ▶sub write_yaml {
Subroutine write_yaml: called CC=1 SCAR=0.0
Pod documented
353 7 my ($self) = @_;
354  
355 7 require Venus::Yaml;
356  
357 7 return Venus::Yaml->new($self->value)->encode;
358 }
359  
360 5 ▶sub write_yaml_file {
Subroutine write_yaml_file: called CC=1 SCAR=0.0
Pod documented
361 5 my ($self, $file) = @_;
362  
363 5 require Venus::Path;
364  
365 5 Venus::Path->new($file)->write($self->write_yaml);
366  
367 5 return $self;
368 }
369  
370 9 1;
371  
372  
373  
374 =head1 NAME
375  
376 Venus::Config - Config Class
377  
378 =cut
379  
380 =head1 ABSTRACT
381  
382 Config Class for Perl 5
383  
384 =cut
385  
386 =head1 SYNOPSIS
387  
388 package main;
389  
390 use Venus::Config;
391  
392 my $config = Venus::Config->new;
393  
394 # $config = $config->read_file('app.pl');
395  
396 # "..."
397  
398 =cut
399  
400 =head1 DESCRIPTION
401  
402 This package provides methods for loading Perl, YAML, and JSON configuration
403 files, and fetching configuration information.
404  
405 =cut
406  
407 =head1 INHERITS
408  
409 This package inherits behaviors from:
410  
411 L<Venus::Kind::Utility>
412  
413 =cut
414  
415 =head1 INTEGRATES
416  
417 This package integrates behaviors from:
418  
419 L<Venus::Role::Buildable>
420  
421 L<Venus::Role::Valuable>
422  
423 =cut
424  
425 =head1 METHODS
426  
427 This package provides the following methods:
428  
429 =cut
430  
431 =head2 edit_file
432  
433 edit_file(string $file, string | coderef $code) (Venus::Config)
434  
435 The edit_file method does an in-place edit, i.e. it loads a Perl, YAML, or JSON
436 configuration file, passes the decoded data to the method or callback provided,
437 and writes the results of the method or callback to the file.
438  
439 I<Since C<3.10>>
440  
441 =over 4
442  
443 =item edit_file example 1
444  
445 package main;
446  
447 use Venus::Config;
448  
449 my $config = Venus::Config->edit_file('t/conf/edit.perl', sub {
450 my ($self, $data) = @_;
451  
452 $data->{edited} = 1;
453  
454 return $data;
455 });
456  
457 # bless(..., 'Venus::Config')
458  
459 =back
460  
461 =cut
462  
463 =head2 new
464  
465 new(any @args) (Venus::Config)
466  
467 The new method constructs an instance of the package.
468  
469 I<Since C<4.15>>
470  
471 =over 4
472  
473 =item new example 1
474  
475 package main;
476  
477 use Venus::Config;
478  
479 my $config = Venus::Config->new;
480  
481 # bless(..., "Venus::Config")
482  
483 =back
484  
485 =over 4
486  
487 =item new example 2
488  
489 package main;
490  
491 use Venus::Config;
492  
493 my $config = Venus::Config->new(value => {password => 'secret'});
494  
495 # bless(..., "Venus::Config")
496  
497 =back
498  
499 =cut
500  
501 =head2 read_env
502  
503 read_env(string $data) (Venus::Config)
504  
505 The read_env method returns a new L<Venus::Config> object based on the string
506 of key/value pairs provided. This method supports multiline values when
507 enclosed in double or single quotes.
508  
509 I<Since C<4.15>>
510  
511 =over 4
512  
513 =item read_env example 1
514  
515 # given: synopsis
516  
517 package main;
518  
519 my $read_env = $config->read_env(
520 "APPNAME=Example\nAPPVER=0.01\n# Comment\n\n\nAPPTAG=\"Godzilla\"",
521 );
522  
523 # bless(..., 'Venus::Config')
524  
525 =back
526  
527 =over 4
528  
529 =item read_env example 2
530  
531 # given: synopsis
532  
533 package main;
534  
535 my $read_env = $config->read_env(
536 "MESSAGE=\"Hello\nWorld\"\nSIGNATURE='Best,\nTeam'",
537 );
538  
539 # bless(..., 'Venus::Config')
540  
541 =back
542  
543 =over 4
544  
545 =item read_env example 3
546  
547 # given: synopsis
548  
549 package main;
550  
551 my $read_env = $config->read_env(
552 'ESCAPE="line1\nline2\ttabbed"',
553 );
554  
555 # bless(..., 'Venus::Config')
556  
557 =back
558  
559 =cut
560  
561 =head2 read_env_file
562  
563 read_env_file(string $file) (Venus::Config)
564  
565 The read_env_file method uses L<Venus::Path> to return a new L<Venus::Config>
566 object based on the file provided.
567  
568 I<Since C<4.15>>
569  
570 =over 4
571  
572 =item read_env_file example 1
573  
574 # given: synopsis
575  
576 package main;
577  
578 $config = $config->read_env_file('t/conf/read.env');
579  
580 # bless(..., 'Venus::Config')
581  
582 =back
583  
584 =cut
585  
586 =head2 read_file
587  
588 read_file(string $path) (Venus::Config)
589  
590 The read_file method load a Perl, YAML, or JSON configuration file, based on
591 the file extension, and returns a new L<Venus::Config> object.
592  
593 I<Since C<2.91>>
594  
595 =over 4
596  
597 =item read_file example 1
598  
599 package main;
600  
601 use Venus::Config;
602  
603 my $config = Venus::Config->read_file('t/conf/read.perl');
604  
605 # bless(..., 'Venus::Config')
606  
607 =back
608  
609 =over 4
610  
611 =item read_file example 2
612  
613 package main;
614  
615 use Venus::Config;
616  
617 my $config = Venus::Config->read_file('t/conf/read.json');
618  
619 # bless(..., 'Venus::Config')
620  
621 =back
622  
623 =over 4
624  
625 =item read_file example 3
626  
627 package main;
628  
629 use Venus::Config;
630  
631 my $config = Venus::Config->read_file('t/conf/read.yaml');
632  
633 # bless(..., 'Venus::Config')
634  
635 =back
636  
637 =cut
638  
639 =head2 read_json
640  
641 read_json(string $data) (Venus::Config)
642  
643 The read_json method returns a new L<Venus::Config> object based on the JSON
644 string provided.
645  
646 I<Since C<2.91>>
647  
648 =over 4
649  
650 =item read_json example 1
651  
652 # given: synopsis
653  
654 package main;
655  
656 $config = $config->read_json(q(
657 {
658 "$metadata": {
659 "tmplog": "/tmp/log"
660 },
661 "$services": {
662 "log": { "package": "Venus/Path", "argument": { "$metadata": "tmplog" } }
663 }
664 }
665 ));
666  
667 # bless(..., 'Venus::Config')
668  
669 =back
670  
671 =cut
672  
673 =head2 read_json_file
674  
675 read_json_file(string $file) (Venus::Config)
676  
677 The read_json_file method uses L<Venus::Path> to return a new L<Venus::Config>
678 object based on the file provided.
679  
680 I<Since C<2.91>>
681  
682 =over 4
683  
684 =item read_json_file example 1
685  
686 # given: synopsis
687  
688 package main;
689  
690 $config = $config->read_json_file('t/conf/read.json');
691  
692 # bless(..., 'Venus::Config')
693  
694 =back
695  
696 =cut
697  
698 =head2 read_perl
699  
700 read_perl(string $data) (Venus::Config)
701  
702 The read_perl method returns a new L<Venus::Config> object based on the Perl
703 string provided.
704  
705 I<Since C<2.91>>
706  
707 =over 4
708  
709 =item read_perl example 1
710  
711 # given: synopsis
712  
713 package main;
714  
715 $config = $config->read_perl(q(
716 {
717 '$metadata' => {
718 tmplog => "/tmp/log"
719 },
720 '$services' => {
721 log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } }
722 }
723 }
724 ));
725  
726 # bless(..., 'Venus::Config')
727  
728 =back
729  
730 =cut
731  
732 =head2 read_perl_file
733  
734 read_perl_file(string $file) (Venus::Config)
735  
736 The read_perl_file method uses L<Venus::Path> to return a new L<Venus::Config>
737 object based on the file provided.
738  
739 I<Since C<2.91>>
740  
741 =over 4
742  
743 =item read_perl_file example 1
744  
745 # given: synopsis
746  
747 package main;
748  
749 $config = $config->read_perl_file('t/conf/read.perl');
750  
751 # bless(..., 'Venus::Config')
752  
753 =back
754  
755 =cut
756  
757 =head2 read_yaml
758  
759 read_yaml(string $data) (Venus::Config)
760  
761 The read_yaml method returns a new L<Venus::Config> object based on the YAML
762 string provided.
763  
764 I<Since C<2.91>>
765  
766 =over 4
767  
768 =item read_yaml example 1
769  
770 # given: synopsis
771  
772 package main;
773  
774 $config = $config->read_yaml(q(
775 '$metadata':
776 tmplog: /tmp/log
777 '$services':
778 log:
779 package: "Venus/Path"
780 argument:
781 '$metadata': tmplog
782 ));
783  
784 # bless(..., 'Venus::Config')
785  
786 =back
787  
788 =cut
789  
790 =head2 read_yaml_file
791  
792 read_yaml_file(string $file) (Venus::Config)
793  
794 The read_yaml_file method uses L<Venus::Path> to return a new L<Venus::Config>
795 object based on the YAML string provided.
796  
797 I<Since C<2.91>>
798  
799 =over 4
800  
801 =item read_yaml_file example 1
802  
803 # given: synopsis
804  
805 package main;
806  
807 $config = $config->read_yaml_file('t/conf/read.yaml');
808  
809 # bless(..., 'Venus::Config')
810  
811 =back
812  
813 =cut
814  
815 =head2 write_env
816  
817 write_env() (string)
818  
819 The write_env method returns a string representing environment variable
820 key/value pairs based on the L</value> held by the underlying L<Venus::Config>
821 object. Multiline values are escaped using C<\n> notation and enclosed in
822 double quotes.
823  
824 I<Since C<4.15>>
825  
826 =over 4
827  
828 =item write_env example 1
829  
830 # given: synopsis
831  
832 package main;
833  
834 my $value = $config->value({
835 APPNAME => "Example",
836 APPTAG => "Godzilla",
837 APPVER => 0.01,
838 });
839  
840 my $write_env = $config->write_env;
841  
842 # "APPNAME=Example\nAPPTAG=Godzilla\nAPPVER=0.01"
843  
844 =back
845  
846 =over 4
847  
848 =item write_env example 2
849  
850 # given: synopsis
851  
852 package main;
853  
854 my $value = $config->value({
855 MESSAGE => "Hello\nWorld",
856 NOTE => "line1\ttabbed",
857 });
858  
859 my $write_env = $config->write_env;
860  
861 # "MESSAGE=\"Hello\\nWorld\"\nNOTE=\"line1\\ttabbed\""
862  
863 =back
864  
865 =over 4
866  
867 =item write_env example 3
868  
869 # given: synopsis
870  
871 package main;
872  
873 my $value = $config->value({
874 APPNAME => "Example",
875 MESSAGE => "Hello\nWorld\nGoodbye",
876 APPTAG => "Godzilla",
877 });
878  
879 my $write_env = $config->write_env;
880  
881 my $read_env = $config->read_env($write_env);
882  
883 # bless(..., 'Venus::Config')
884  
885 # round-trip: read_env(write_env($value)) == $value
886  
887 =back
888  
889 =cut
890  
891 =head2 write_env_file
892  
893 write_env_file(string $path) (Venus::Config)
894  
895 The write_env_file method saves a environment configuration file and returns a new
896 L<Venus::Config> object.
897  
898 I<Since C<4.15>>
899  
900 =over 4
901  
902 =item write_env_file example 1
903  
904 # given: synopsis
905  
906 my $value = $config->value({
907 APPNAME => "Example",
908 APPTAG => "Godzilla",
909 APPVER => 0.01,
910 });
911  
912 $config = $config->write_env_file('t/conf/write.env');
913  
914 # bless(..., 'Venus::Config')
915  
916 =back
917  
918 =cut
919  
920 =head2 write_file
921  
922 write_file(string $path) (Venus::Config)
923  
924 The write_file method saves a Perl, YAML, or JSON configuration file, based on
925 the file extension, and returns a new L<Venus::Config> object.
926  
927 I<Since C<2.91>>
928  
929 =over 4
930  
931 =item write_file example 1
932  
933 # given: synopsis
934  
935 my $value = $config->value({
936 '$services' => {
937 log => { package => "Venus/Path", argument => { value => "." } }
938 }
939 });
940  
941 $config = $config->write_file('t/conf/write.perl');
942  
943 # bless(..., 'Venus::Config')
944  
945 =back
946  
947 =over 4
948  
949 =item write_file example 2
950  
951 # given: synopsis
952  
953 my $value = $config->value({
954 '$metadata' => {
955 tmplog => "/tmp/log"
956 },
957 '$services' => {
958 log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } }
959 }
960 });
961  
962 $config = $config->write_file('t/conf/write.json');
963  
964 # bless(..., 'Venus::Config')
965  
966 =back
967  
968 =over 4
969  
970 =item write_file example 3
971  
972 # given: synopsis
973  
974 my $value = $config->value({
975 '$metadata' => {
976 tmplog => "/tmp/log"
977 },
978 '$services' => {
979 log => { package => "Venus/Path", argument => { '$metadata' => "tmplog" } }
980 }
981 });
982  
983 $config = $config->write_file('t/conf/write.yaml');
984  
985 # bless(..., 'Venus::Config')
986  
987 =back
988  
989 =cut
990  
991 =head2 write_json
992  
993 write_json() (string)
994  
995 The write_json method returns a JSON encoded string based on the L</value> held
996 by the underlying L<Venus::Config> object.
997  
998 I<Since C<2.91>>
999  
1000 =over 4
1001  
1002 =item write_json example 1
1003  
1004 # given: synopsis
1005  
1006 my $value = $config->value({
1007 '$services' => {
1008 log => { package => "Venus::Path" },
1009 },
1010 });
1011  
1012 my $json = $config->write_json;
1013  
1014 # '{ "$services":{ "log":{ "package":"Venus::Path" } } }'
1015  
1016 =back
1017  
1018 =cut
1019  
1020 =head2 write_json_file
1021  
1022 write_json_file(string $path) (Venus::Config)
1023  
1024 The write_json_file method saves a JSON configuration file and returns a new
1025 L<Venus::Config> object.
1026  
1027 I<Since C<2.91>>
1028  
1029 =over 4
1030  
1031 =item write_json_file example 1
1032  
1033 # given: synopsis
1034  
1035 my $value = $config->value({
1036 '$services' => {
1037 log => { package => "Venus/Path", argument => { value => "." } }
1038 }
1039 });
1040  
1041 $config = $config->write_json_file('t/conf/write.json');
1042  
1043 # bless(..., 'Venus::Config')
1044  
1045 =back
1046  
1047 =cut
1048  
1049 =head2 write_perl
1050  
1051 write_perl() (string)
1052  
1053 The write_perl method returns a FILE encoded string based on the L</value> held
1054 by the underlying L<Venus::Config> object.
1055  
1056 I<Since C<2.91>>
1057  
1058 =over 4
1059  
1060 =item write_perl example 1
1061  
1062 # given: synopsis
1063  
1064 my $value = $config->value({
1065 '$services' => {
1066 log => { package => "Venus::Path" },
1067 },
1068 });
1069  
1070 my $perl = $config->write_perl;
1071  
1072 # '{ "\$services" => { log => { package => "Venus::Path" } } }'
1073  
1074 =back
1075  
1076 =cut
1077  
1078 =head2 write_perl_file
1079  
1080 write_perl_file(string $path) (Venus::Config)
1081  
1082 The write_perl_file method saves a Perl configuration file and returns a new
1083 L<Venus::Config> object.
1084  
1085 I<Since C<2.91>>
1086  
1087 =over 4
1088  
1089 =item write_perl_file example 1
1090  
1091 # given: synopsis
1092  
1093 my $value = $config->value({
1094 '$services' => {
1095 log => { package => "Venus/Path", argument => { value => "." } }
1096 }
1097 });
1098  
1099 $config = $config->write_perl_file('t/conf/write.perl');
1100  
1101 # bless(..., 'Venus::Config')
1102  
1103 =back
1104  
1105 =cut
1106  
1107 =head2 write_yaml
1108  
1109 write_yaml() (string)
1110  
1111 The write_yaml method returns a FILE encoded string based on the L</value> held
1112 by the underlying L<Venus::Config> object.
1113  
1114 I<Since C<2.91>>
1115  
1116 =over 4
1117  
1118 =item write_yaml example 1
1119  
1120 # given: synopsis
1121  
1122 my $value = $config->value({
1123 '$services' => {
1124 log => { package => "Venus::Path" },
1125 },
1126 });
1127  
1128 my $yaml = $config->write_yaml;
1129  
1130 # '---\n$services:\n\s\slog:\n\s\s\s\spackage:\sVenus::Path'
1131  
1132 =back
1133  
1134 =cut
1135  
1136 =head2 write_yaml_file
1137  
1138 write_yaml_file(string $path) (Venus::Config)
1139  
1140 The write_yaml_file method saves a YAML configuration file and returns a new
1141 L<Venus::Config> object.
1142  
1143 I<Since C<2.91>>
1144  
1145 =over 4
1146  
1147 =item write_yaml_file example 1
1148  
1149 # given: synopsis
1150  
1151 my $value = $config->value({
1152 '$services' => {
1153 log => { package => "Venus/Path", argument => { value => "." } }
1154 }
1155 });
1156  
1157 $config = $config->write_yaml_file('t/conf/write.yaml');
1158  
1159 # bless(..., 'Venus::Config')
1160  
1161 =back
1162  
1163 =cut
1164  
1165 =head1 AUTHORS
1166  
1167 Awncorp, C<awncorp@cpan.org>
1168  
1169 =cut
1170  
1171 =head1 LICENSE
1172  
1173 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
1174  
1175 This program is free software, you can redistribute it and/or modify it under
1176 the terms of the Apache license version 2.0.
1177  
1178 =cut
« blib/lib/Venus/Role/Mappable.pm ↑ Summary blib/lib/Venus/Factory.pm »