File Coverage

blib/lib/Data/Object/Vars.pm
Criterion Covered Total %
statement 66 67 98.5
branch 15 20 75.0
condition n/a
subroutine 15 15 100.0
pod 5 7 71.4
total 101 109 92.6


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