Venus/Vars.pm

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