File Coverage

blib/lib/Data/Object/Args.pm
Criterion Covered Total %
statement 76 77 98.7
branch 17 22 77.2
condition n/a
subroutine 16 16 100.0
pod 6 8 75.0
total 115 123 93.5


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