blib/lib/Venus/Vars.pm

filter: Statementstmt 98.5% 67 / 68 Branchbran 84.6% 22 / 26 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 14 / 14 Podpod 66.6% 6 / 9 totaltotal 93.3% 112 / 120 CC 16 SCAR 9.7
CC
16
Cov
95%
name
4.0
exists
3.0
get
3.0
CRAP 2.6
« blib/lib/Venus/Sealed.pm ↑ Summary blib/lib/Venus/Result.pm »
1 package Venus::Vars;
2  
3 3 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 3 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 3 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 3 with 'Venus::Role::Valuable';
19 3 with 'Venus::Role::Buildable';
20 3 with 'Venus::Role::Accessible';
21 3 with 'Venus::Role::Proxyable';
22  
23 # ATTRIBUTES
24  
25 3 attr 'named';
26  
27 # BUILDERS
28  
29 2 ▶sub build_proxy {
Subroutine build_proxy: called CC=1 SCAR=0.2
Pod undocumented
30 2 my ($self, $package, $method, $value) = @_;
31  
32 2 my $has_value = exists $_[3];
33  
34 2 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
35 2 ▶ return $self->get($method) if !$has_value; # no value
Branch: 1 branch
BranchTrueFalseTotal
unless $has_value 2 0 2
36 0 return $self->set($method, $value);
37 2 };
38 }
39  
40 29 ▶sub build_data {
Subroutine build_data: called CC=3 SCAR=11.0
Pod undocumented
41 29 my ($self, $data, $xargs) = @_;
42  
43 29 ▶ $data->{value} ||= {};
Condition100%2/2
$data->{"value"} ||= {}
l!l
209
MC/DC100%1/1
$data->{"value"} ||= {}
$data->{"value"}
Truth table100%2/2
A
A= $data->{"value"}
Aresult
11
00
44  
45 29 $data->{value} = ({%{$data->{value}}, %{$xargs}});
46  
47 29 ▶ $data->{value} = $self->default if !keys %{$data->{value}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$data{"value"};} 8 21 29
48  
49 29 return $data;
50 }
51  
52 29 ▶sub build_self {
Subroutine build_self: called CC=2 SCAR=6.9
Pod undocumented
53 29 my ($self, $data) = @_;
54  
55 29 ▶ $self->named({}) if !$self->named;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->named 10 19 29
56  
57 29 return $self;
58 }
59  
60 # METHODS
61  
62 9 ▶sub default {
Subroutine default: called CC=1 SCAR=0.0
Pod documented
63 9 my ($self) = @_;
64  
65 9 return {%ENV};
66 }
67  
68 5 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
69 5 my ($self, $name) = @_;
70  
71 5 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 5 5
72  
73 5 my $pos = $self->name($name);
74  
75 5 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 4 5
76  
77 4 return exists $self->value->{$pos};
78 }
79  
80 6 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
81 6 my ($self, $name) = @_;
82  
83 6 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 6 6
84  
85 6 my $pos = $self->name($name);
86  
87 6 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 5 6
88  
89 5 return $self->value->{$pos};
90 }
91  
92 19 ▶sub name {
Subroutine name: called CC=4 SCAR=13.9
Pod documented
93 19 my ($self, $name) = @_;
94  
95 19 ▶ if (defined $self->named->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->named->{$name}) 6 13 19
96 6 return $self->named->{$name};
97 }
98  
99 13 ▶ if (defined $self->value->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->value->{$name}) 4 9 13
100 4 return $name;
101 }
102  
103 9 ▶ if (defined $self->value->{uc($name)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->value->{uc $name}) 5 4 9
104 5 return uc($name);
105 }
106  
107 4 return undef;
108 }
109  
110 4 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
111 4 my ($self, $name, $data) = @_;
112  
113 4 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
114  
115 4 my $pos = $self->name($name);
116  
117 4 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 3 4
118  
119 3 return $self->value->{$pos} = $data;
120 }
121  
122 1 ▶sub unnamed {
Subroutine unnamed: called CC=3 SCAR=11.0
Pod documented
123 1 my ($self) = @_;
124  
125 1 my $list = {};
126  
127 1 my $vars = $self->value;
128 1 my $data = +{reverse %{$self->named}};
129  
130 1 for my $index (sort keys %$vars) {
131 2 ▶ unless (exists $data->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{$index}) 1 1 2
132 1 $list->{$index} = $vars->{$index};
133 }
134 }
135  
136 1 return $list;
137 }
138  
139 3 1;
140  
141  
142  
143 =head1 NAME
144  
145 Venus::Vars - Vars Class
146  
147 =cut
148  
149 =head1 ABSTRACT
150  
151 Vars Class for Perl 5
152  
153 =cut
154  
155 =head1 SYNOPSIS
156  
157 package main;
158  
159 use Venus::Vars;
160  
161 my $vars = Venus::Vars->new(
162 value => { USER => 'awncorp', HOME => '/home/awncorp', },
163 named => { iam => 'USER', root => 'HOME', },
164 );
165  
166 # $vars->root; # $ENV{HOME}
167 # $vars->home; # $ENV{HOME}
168 # $vars->get('home'); # $ENV{HOME}
169 # $vars->get('HOME'); # $ENV{HOME}
170  
171 # $vars->iam; # $ENV{USER}
172 # $vars->user; # $ENV{USER}
173 # $vars->get('user'); # $ENV{USER}
174 # $vars->get('USER'); # $ENV{USER}
175  
176 =cut
177  
178 =head1 DESCRIPTION
179  
180 This package provides methods for accessing C<%ENV> items.
181  
182 =cut
183  
184 =head1 ATTRIBUTES
185  
186 This package has the following attributes:
187  
188 =cut
189  
190 =head2 named
191  
192 named(HashRef)
193  
194 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
195  
196 =cut
197  
198 =head1 INHERITS
199  
200 This package inherits behaviors from:
201  
202 L<Venus::Kind::Utility>
203  
204 =cut
205  
206 =head1 INTEGRATES
207  
208 This package integrates behaviors from:
209  
210 L<Venus::Role::Accessible>
211  
212 L<Venus::Role::Buildable>
213  
214 L<Venus::Role::Proxyable>
215  
216 L<Venus::Role::Valuable>
217  
218 =cut
219  
220 =head1 METHODS
221  
222 This package provides the following methods:
223  
224 =cut
225  
226 =head2 default
227  
228 default() (hashref)
229  
230 The default method returns the default value, i.e. C<{%ENV}>.
231  
232 I<Since C<0.01>>
233  
234 =over 4
235  
236 =item default example 1
237  
238 # given: synopsis;
239  
240 my $default = $vars->default;
241  
242 # { USER => 'awncorp', HOME => '/home/awncorp', ... }
243  
244 =back
245  
246 =cut
247  
248 =head2 exists
249  
250 exists(string $key) (boolean)
251  
252 The exists method takes a name or index and returns truthy if an associated
253 value exists.
254  
255 I<Since C<0.01>>
256  
257 =over 4
258  
259 =item exists example 1
260  
261 # given: synopsis;
262  
263 my $exists = $vars->exists('iam');;
264  
265 # 1
266  
267 =back
268  
269 =over 4
270  
271 =item exists example 2
272  
273 # given: synopsis;
274  
275 my $exists = $vars->exists('USER');;
276  
277 # 1
278  
279 =back
280  
281 =over 4
282  
283 =item exists example 3
284  
285 # given: synopsis;
286  
287 my $exists = $vars->exists('PATH');
288  
289 # undef
290  
291 =back
292  
293 =over 4
294  
295 =item exists example 4
296  
297 # given: synopsis;
298  
299 my $exists = $vars->exists('user');
300  
301 # 1
302  
303 =back
304  
305 =cut
306  
307 =head2 get
308  
309 get(string $key) (any)
310  
311 The get method takes a name or index and returns the associated value.
312  
313 I<Since C<0.01>>
314  
315 =over 4
316  
317 =item get example 1
318  
319 # given: synopsis;
320  
321 my $get = $vars->get('iam');
322  
323 # "awncorp"
324  
325 =back
326  
327 =over 4
328  
329 =item get example 2
330  
331 # given: synopsis;
332  
333 my $get = $vars->get('USER');
334  
335 # "awncorp"
336  
337 =back
338  
339 =over 4
340  
341 =item get example 3
342  
343 # given: synopsis;
344  
345 my $get = $vars->get('PATH');
346  
347 # undef
348  
349 =back
350  
351 =over 4
352  
353 =item get example 4
354  
355 # given: synopsis;
356  
357 my $get = $vars->get('user');
358  
359 # "awncorp"
360  
361 =back
362  
363 =cut
364  
365 =head2 name
366  
367 name(string $key) (string | undef)
368  
369 The name method takes a name or index and returns index if the the associated
370 value exists.
371  
372 I<Since C<0.01>>
373  
374 =over 4
375  
376 =item name example 1
377  
378 # given: synopsis;
379  
380 my $name = $vars->name('iam');
381  
382 # "USER"
383  
384 =back
385  
386 =over 4
387  
388 =item name example 2
389  
390 # given: synopsis;
391  
392 my $name = $vars->name('USER');
393  
394 # "USER"
395  
396 =back
397  
398 =over 4
399  
400 =item name example 3
401  
402 # given: synopsis;
403  
404 my $name = $vars->name('PATH');
405  
406 # undef
407  
408 =back
409  
410 =over 4
411  
412 =item name example 4
413  
414 # given: synopsis;
415  
416 my $name = $vars->name('user');
417  
418 # "USER"
419  
420 =back
421  
422 =cut
423  
424 =head2 new
425  
426 new(any @args) (Venus::Vars)
427  
428 The new method constructs an instance of the package.
429  
430 I<Since C<4.15>>
431  
432 =over 4
433  
434 =item new example 1
435  
436 package main;
437  
438 use Venus::Vars;
439  
440 my $new = Venus::Vars->new;
441  
442 # bless(..., "Venus::Vars")
443  
444 =back
445  
446 =over 4
447  
448 =item new example 2
449  
450 package main;
451  
452 use Venus::Vars;
453  
454 my $new = Venus::Vars->new(
455 {USER => 'awncorp', HOME => '/home/awncorp'},
456 );
457  
458 # bless(..., "Venus::Vars")
459  
460 =back
461  
462 =over 4
463  
464 =item new example 3
465  
466 package main;
467  
468 use Venus::Vars;
469  
470 my $new = Venus::Vars->new(
471 value => {USER => 'awncorp', HOME => '/home/awncorp'},
472 );
473  
474 # bless(..., "Venus::Vars")
475  
476 =back
477  
478 =cut
479  
480 =head2 set
481  
482 set(string $key, any $value) (any)
483  
484 The set method takes a name or index and sets the value provided if the
485 associated argument exists.
486  
487 I<Since C<0.01>>
488  
489 =over 4
490  
491 =item set example 1
492  
493 # given: synopsis;
494  
495 my $set = $vars->set('iam', 'root');
496  
497 # "root"
498  
499 =back
500  
501 =over 4
502  
503 =item set example 2
504  
505 # given: synopsis;
506  
507 my $set = $vars->set('USER', 'root');
508  
509 # "root"
510  
511 =back
512  
513 =over 4
514  
515 =item set example 3
516  
517 # given: synopsis;
518  
519 my $set = $vars->set('PATH', '/tmp');
520  
521 # undef
522  
523 =back
524  
525 =over 4
526  
527 =item set example 4
528  
529 # given: synopsis;
530  
531 my $set = $vars->set('user', 'root');
532  
533 # "root"
534  
535 =back
536  
537 =cut
538  
539 =head2 unnamed
540  
541 unnamed() (hashref)
542  
543 The unnamed method returns an arrayref of values which have not been named
544 using the C<named> attribute.
545  
546 I<Since C<0.01>>
547  
548 =over 4
549  
550 =item unnamed example 1
551  
552 package main;
553  
554 use Venus::Vars;
555  
556 my $vars = Venus::Vars->new(
557 value => { USER => 'awncorp', HOME => '/home/awncorp', },
558 named => { root => 'HOME', },
559 );
560  
561 my $unnamed = $vars->unnamed;
562  
563 # { USER => "awncorp" }
564  
565 =back
566  
567 =cut
568  
569 =head1 AUTHORS
570  
571 Awncorp, C<awncorp@cpan.org>
572  
573 =cut
574  
575 =head1 LICENSE
576  
577 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
578  
579 This program is free software, you can redistribute it and/or modify it under
580 the terms of the Apache license version 2.0.
581  
582 =cut
« blib/lib/Venus/Sealed.pm ↑ Summary blib/lib/Venus/Result.pm »