File Coverage

blib/lib/Data/Object/Vars.pm
Criterion Covered Total %
statement 68 69 98.5
branch 17 22 77.2
condition n/a
subroutine 15 15 100.0
pod 5 7 71.4
total 105 113 92.9


line stmt bran cond sub pod time code
1             package Data::Object::Vars;
2              
3 1     1   31121 use 5.014;
  1         3  
4              
5 1     1   5 use strict;
  1         2  
  1         19  
6 1     1   4 use warnings;
  1         2  
  1         22  
7              
8 1     1   4 use registry;
  1         1  
  1         6  
9 1     1   4868 use routines;
  1         2  
  1         6  
10              
11 1     1   1985 use Data::Object::Class;
  1         906  
  1         11  
12 1     1   668 use Data::Object::ClassHas;
  1         8657  
  1         10  
13              
14             with 'Data::Object::Role::Buildable';
15             with 'Data::Object::Role::Proxyable';
16             with 'Data::Object::Role::Stashable';
17              
18             our $VERSION = '2.01'; # VERSION
19              
20             # ATTRIBUTES
21              
22             has 'named' => (
23             is => 'ro',
24             isa => 'HashRef',
25             opt => 1,
26             );
27              
28             # BUILD
29              
30 18     18 0 132238 method build_self($args) {
  18         36  
  18         26  
31 18 50       50 $self->{named} = {} if !$args->{named};
32              
33 18         94 my $envv = { map +($_, $ENV{$_}), keys %ENV };
34              
35 18         75 $self->stash(envv => $envv);
36              
37 18         203 return $self;
38             }
39              
40 2     2 0 774 method build_proxy($package, $method, $value) {
  2         5  
  2         3  
41 2         5 my $has_value = exists $_[2];
42              
43             return sub {
44              
45 2 50   2   32 return $self->get($method) if !$has_value; # no val
46              
47 0         0 return $self->set($method, $value);
48 2         10 };
49             }
50              
51             # METHODS
52              
53 4     4 1 94 method exists($key) {
  4         8  
  4         5  
54 4 50       10 return if not defined $key;
55              
56 4         9 my $pos = $self->name($key);
57              
58 4 100       32 return if not defined $pos;
59              
60 3         6 return exists $self->stashed->{$pos};
61             }
62              
63 6     6 1 101 method get($key) {
  6         14  
  6         10  
64 6 50       15 return if not defined $key;
65              
66 6         15 my $pos = $self->name($key);
67              
68 6 100       41 return if not defined $pos;
69              
70 5         15 return $self->stashed->{$pos};
71             }
72              
73 18     18 1 115 method name($key) {
  18         32  
  18         22  
74 18 100       66 if (defined $self->named->{$key}) {
75 6         75 return $self->named->{$key};
76             }
77              
78 12 100       42 if (defined $self->stashed->{$key}) {
79 4         37 return $key;
80             }
81              
82 8 100       17 if (defined $self->stashed->{uc($key)}) {
83 4         36 return uc($key);
84             }
85              
86 4         33 return undef;
87             }
88              
89 4     4 1 98 method set($key, $val) {
  4         8  
  4         6  
90 4 50       12 return if not defined $key;
91              
92 4         9 my $pos = $self->name($key);
93              
94 4 100       37 return if not defined $pos;
95              
96 3         7 return $self->stashed->{$pos} = $val;
97             }
98              
99 32     32 1 61 method stashed() {
  32         35  
100 32         59 my $data = $self->stash('envv');
101              
102 32         541 return $data;
103             }
104              
105             1;
106              
107             =encoding utf8
108              
109             =head1 NAME
110              
111             Data::Object::Vars
112              
113             =cut
114              
115             =head1 ABSTRACT
116              
117             Env Vars Class for Perl 5
118              
119             =cut
120              
121             =head1 SYNOPSIS
122              
123             package main;
124              
125             use Data::Object::Vars;
126              
127             local %ENV = (USER => 'ubuntu', HOME => '/home/ubuntu');
128              
129             my $vars = Data::Object::Vars->new(
130             named => { iam => 'USER', root => 'HOME' }
131             );
132              
133             # $vars->root; # $ENV{HOME}
134             # $vars->home; # $ENV{HOME}
135             # $vars->get('home'); # $ENV{HOME}
136             # $vars->get('HOME'); # $ENV{HOME}
137              
138             # $vars->iam; # $ENV{USER}
139             # $vars->user; # $ENV{USER}
140             # $vars->get('user'); # $ENV{USER}
141             # $vars->get('USER'); # $ENV{USER}
142              
143             =cut
144              
145             =head1 DESCRIPTION
146              
147             This package provides methods for accessing C<%ENV> items.
148              
149             =cut
150              
151             =head1 INTEGRATES
152              
153             This package integrates behaviors from:
154              
155             L<Data::Object::Role::Buildable>
156              
157             L<Data::Object::Role::Proxyable>
158              
159             L<Data::Object::Role::Stashable>
160              
161             =cut
162              
163             =head1 LIBRARIES
164              
165             This package uses type constraints from:
166              
167             L<Types::Standard>
168              
169             =cut
170              
171             =head1 ATTRIBUTES
172              
173             This package has the following attributes:
174              
175             =cut
176              
177             =head2 named
178              
179             named(HashRef)
180              
181             This attribute is read-only, accepts C<(HashRef)> values, and is optional.
182              
183             =cut
184              
185             =head1 METHODS
186              
187             This package implements the following methods:
188              
189             =cut
190              
191             =head2 exists
192              
193             exists(Str $key) : Any
194              
195             The exists method takes a name or index and returns truthy if an associated
196             value exists.
197              
198             =over 4
199              
200             =item exists example #1
201              
202             # given: synopsis
203              
204             $vars->exists('iam'); # truthy
205              
206             =back
207              
208             =over 4
209              
210             =item exists example #2
211              
212             # given: synopsis
213              
214             $vars->exists('USER'); # truthy
215              
216             =back
217              
218             =over 4
219              
220             =item exists example #3
221              
222             # given: synopsis
223              
224             $vars->exists('PATH'); # falsy
225              
226             =back
227              
228             =over 4
229              
230             =item exists example #4
231              
232             # given: synopsis
233              
234             $vars->exists('user'); # truthy
235              
236             =back
237              
238             =cut
239              
240             =head2 get
241              
242             get(Str $key) : Any
243              
244             The get method takes a name or index and returns the associated value.
245              
246             =over 4
247              
248             =item get example #1
249              
250             # given: synopsis
251              
252             $vars->get('iam'); # ubuntu
253              
254             =back
255              
256             =over 4
257              
258             =item get example #2
259              
260             # given: synopsis
261              
262             $vars->get('USER'); # ubuntu
263              
264             =back
265              
266             =over 4
267              
268             =item get example #3
269              
270             # given: synopsis
271              
272             $vars->get('PATH'); # undef
273              
274             =back
275              
276             =over 4
277              
278             =item get example #4
279              
280             # given: synopsis
281              
282             $vars->get('user'); # ubuntu
283              
284             =back
285              
286             =cut
287              
288             =head2 name
289              
290             name(Str $key) : Any
291              
292             The name method takes a name or index and returns index if the the associated
293             value exists.
294              
295             =over 4
296              
297             =item name example #1
298              
299             # given: synopsis
300              
301             $vars->name('iam'); # USER
302              
303             =back
304              
305             =over 4
306              
307             =item name example #2
308              
309             # given: synopsis
310              
311             $vars->name('USER'); # USER
312              
313             =back
314              
315             =over 4
316              
317             =item name example #3
318              
319             # given: synopsis
320              
321             $vars->name('PATH'); # undef
322              
323             =back
324              
325             =over 4
326              
327             =item name example #4
328              
329             # given: synopsis
330              
331             $vars->name('user'); # USER
332              
333             =back
334              
335             =cut
336              
337             =head2 set
338              
339             set(Str $key, Maybe[Any] $value) : Any
340              
341             The set method takes a name or index and sets the value provided if the
342             associated argument exists.
343              
344             =over 4
345              
346             =item set example #1
347              
348             # given: synopsis
349              
350             $vars->set('iam', 'root'); # root
351              
352             =back
353              
354             =over 4
355              
356             =item set example #2
357              
358             # given: synopsis
359              
360             $vars->set('USER', 'root'); # root
361              
362             =back
363              
364             =over 4
365              
366             =item set example #3
367              
368             # given: synopsis
369              
370             $vars->set('PATH', '/tmp'); # undef
371              
372             # is not set
373              
374             =back
375              
376             =over 4
377              
378             =item set example #4
379              
380             # given: synopsis
381              
382             $vars->set('user', 'root'); # root
383              
384             =back
385              
386             =cut
387              
388             =head2 stashed
389              
390             stashed() : HashRef
391              
392             The stashed method returns the stashed data associated with the object.
393              
394             =over 4
395              
396             =item stashed example #1
397              
398             # given: synopsis
399              
400             $vars->stashed
401              
402             =back
403              
404             =cut
405              
406             =head1 AUTHOR
407              
408             Al Newkirk, C<awncorp@cpan.org>
409              
410             =head1 LICENSE
411              
412             Copyright (C) 2011-2019, Al Newkirk, et al.
413              
414             This is free software; you can redistribute it and/or modify it under the terms
415             of the The Apache License, Version 2.0, as elucidated in the L<"license
416             file"|https://github.com/iamalnewkirk/data-object-vars/blob/master/LICENSE>.
417              
418             =head1 PROJECT
419              
420             L<Wiki|https://github.com/iamalnewkirk/data-object-vars/wiki>
421              
422             L<Project|https://github.com/iamalnewkirk/data-object-vars>
423              
424             L<Initiatives|https://github.com/iamalnewkirk/data-object-vars/projects>
425              
426             L<Milestones|https://github.com/iamalnewkirk/data-object-vars/milestones>
427              
428             L<Contributing|https://github.com/iamalnewkirk/data-object-vars/blob/master/CONTRIBUTE.md>
429              
430             L<Issues|https://github.com/iamalnewkirk/data-object-vars/issues>
431              
432             =cut