blib/lib/FindApp/Object/State/Group/State/Dirs.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 186 CC 9 SCAR 14.6
CC
9
Cov
0%
new
12.0
all_defined
6.0
object
6.0
CRAP 4.3
1 package FindApp::Object::State::Group::State::Dirs;
2  
3 use v5.10;
4 use strict;
5 use warnings;
6 use mro "c3";
7  
8 use FindApp::Utils qw(
9 :assert
10 :debug
11 :foreign
12 :list
13 );
14  
15 sub all_defined {
16 unless (@_ == grep {defined} @_) {
17 subcroak_N(2, "undefined arguments forbidden");
18 }
19 }
20  
21 use namespace::clean;
22  
23 ################################################################
24  
25 use FindApp::Utils ":overload";
26  
27 use overload reverse (
28 as_string => qw( "" ),
29 as_number => qw( 0+ ),
30 op_equals => qw( == ),
31 op_notequals => qw( != ),
32 op_eq => qw( eq ),
33 op_ne => qw( ne ),
34 );
35  
36 sub op_eq {
37 my($this, $that, $swapped) = @_;
38 no overloading;
39 local $" = "\0"; # these are path elements, so NUL illegal
40 return "@$this" eq "@$that"; # "guaranteed" to be ordered
41 }
42  
43 sub op_ne { !&op_eq }
44  
45 sub class {
46 my $self = shift;
47 good_args(@_ == 0);
48 return blessed($self) || $self;
49 }
50  
51 sub object {
52 my $self = shift;
53 good_args(@_ == 0);
54 croak "not an object -- and no old class object to uncache" unless blessed $self;
55 return $self;
56 }
57  
58 sub new { &ENTER_TRACE_3;
59 my($invocant, @values) = @_;
60 all_defined(@values);
61 my $class = blessed($invocant) || $invocant;
62 my $old = blessed($invocant) && $invocant;
63 my $new = bless([], $class);
64 $new->copy($old) if $old;
65 $new->add(@values) if @values;
66 return $new;
67 }
68  
69 sub copy { &ENTER_TRACE_3;
70 good_args(@_ == 2);
71 my($new, $old) = @_;
72 $new->set($old->get) if $old->count;
73 }
74  
75 sub get {
76 my $self = shift;
77 good_args(@_ == 0);
78 return wantarray ? @$self
79 : $$self[0];
80 }
81  
82 sub set { &ENTER_TRACE_3;
83 my($self, @values) = @_;
84 all_defined(@values);
85 @values = map { (ref && reftype eq "ARRAY") ? @$_ : $_ } @values;
86 @$self = uniq sort @values;
87 return $self;
88 }
89  
90 sub reset { &ENTER_TRACE_3;
91 my $self = shift;
92 good_args(@_ == 0);
93 my @old_bits = @$self;
94 @$self = ();
95 return @old_bits;
96 }
97  
98 # these should not exist
99 sub first {
100 my $self = shift;
101 good_args(@_ == 0);
102 return $$self[0];
103 }
104  
105 # these should not exist
106 sub last {
107 my $self = shift;
108 good_args(@_ == 0);
109 return $$self[-1];
110 }
111  
112 sub count {
113 my $self = shift;
114 good_args(@_ == 0);
115 return 0+@$self;
116 }
117  
118 sub has { &ENTER_TRACE_3;
119 my($self, @values) = @_;
120 good_args(@values != 0);
121 all_defined(@values);
122 my %have = map { $_ => 1 } $self->get;
123 for my $value (@values) {
124 $have{$value} || return 0;
125 }
126 return 1;
127 }
128  
129 use Carp qw(cluck);
130  
131 sub add { &ENTER_TRACE_3;
132 my($self, @values) = @_;
133 good_args(@values != 0);
134 all_defined(@values);
135 return $self->set($self->get, @values);
136 }
137  
138 sub del { &ENTER_TRACE_3;
139 my($self, @values) = @_;
140 good_args(@values != 0);
141 all_defined(@values);
142 cluck "silly bits" if $values[0] eq "1";
143 my %have = map { ($_ => 1) } $self->get;
144 my %gone = map { ($_ => 1) } @values;
145 my @removed = grep { $have{$_} && $gone{$_} } @values;
146 $self->set(grep { ! $gone{$_} } keys %have);
147 return @removed;
148 }
149  
150 1;
151  
152 =encoding utf8
153  
154 =head1 NAME
155  
156 FindApp::Object::State::Group::State::Dirs - Implement the FindApp group-directory set object
157  
158 =head1 DESCRIPTION
159  
160 This is the class that each L<FindApp::Object::State::Group> object has three of,
161 one for each of I<allowed>, I<wanted>, I<found>. Each Dirs object represents
162 a set of paths.
163  
164 =head2 Methods
165  
166 =over
167  
168 =item new LIST
169  
170 Creates, initializes, and returns a Dirs object. The list is a list
171 of filenames. As a class method:
172
173 use FindApp::Object::State::Group::Dirs;
174 my $ob1 = FindApp::Object::State::Group::Dirs->new("lib", "t/lib");
175  
176 Invoked as an instance method, the old object's contents are added
177 to the set, making it the union of the old object set and the parameter list.
178  
179 my $ob2 = $ob1->new("testlib");
180  
181 Now C<< $ob2->get >> returns a list of three items: F<lib>, F<t/lib>, and F<testlib>.
182  
183 If you don't want the old object's contents, don't call it as an object method:
184 use its class instead:
185  
186 my $ob3 = $ob2->class->new("bin");
187  
188 Now $ob3 will hold only the single new item.
189  
190 =item copy OLD
191  
192 Method used to copy an old object into a new one. This sets the
193 new object to be the same set as the old one's only if the old one
194 has anything in its set. So this:
195  
196 $new->copy($old);
197  
198 is like this:
199  
200 $new->add($old->get) if $old->count;
201  
202 This is called implicitly by the C<new> method as just explained.
203  
204 =item class
205  
206 Returns the package of the blessed argument. This is useful for
207 inherited constructors so that you don't need to know the exact
208 class. See L</new>.
209  
210 =item object
211  
212 Returns the object itself. This is the identity function.
213  
214 =item as_number
215  
216 Overload for comparing whether two objects are truly the same reference.
217 Used by the C<==> and C<!=> operators.
218  
219 =item as_string
220  
221 Overload for pretty-printing an object's contents.
222 This is I<not> what is used by the C<eq> and C<ne> operators.
223  
224 =item add LIST
225  
226 Add a list of directories to the object. Duplicates are suppressed.
227  
228 =item has LIST
229  
230 Returns true if the object has all the I<LIST> elements, and false otherwise.
231  
232 =item del LIST
233  
234 Remove all I<LIST> elements from the object, and return those deleted.
235  
236 =item count
237  
238 Report how many elements are in the Dirs set.
239  
240 =item first
241  
242 Return one element, or I<undef> if there isn't one.
243 This should not exist, because these are to be thought of as sets not lists.
244  
245 =item get
246  
247 Return a list of the set contents in list context, and just one of them otherwise.
248  
249 =item last
250  
251 Return one element, or I<undef> if there isn't one.
252 This should not exist, because these are to be thought of as sets not lists.
253  
254 =item reset
255  
256 Clear the set, and return the deleted list.
257  
258 =item set LIST
259  
260 Destructively make the current object equal to the LIST and return the object.
261 One level of array reference will be interpolated.
262  
263 =item op_eq
264  
265 Used to implement the C<eq> operator, this compares contents.
266  
267 =item op_equals
268  
269 Used to implement the C<==> operator, this compares addresses.
270  
271 =item op_ne
272  
273 The opposite of C<op_eq>.
274  
275 =item op_notequals
276  
277 The opposite of C<op_equals>.
278  
279 =back
280  
281 =head1 ENVIRONMENT
282  
283 =over
284  
285 =item FINDAPP_TRACE
286  
287 If the FINDAPP_TRACE variable is set to 3 or higher, will trace
288 some method calls.
289  
290 tchrist% perl -MFindApp::Object::State::Group::Dirs -E 'say FindApp::Object::State::Group::Dirs->new(<R B G>)->add(<C M Y>)->get'
291 BCGMRY
292  
293 tchrist% env FINDAPP_TRACE=3 perl -MFindApp::Object::State::Group::Dirs -E 'say FindApp::Object::State::Group::Dirs->new(<R B G>)->add(<C M Y>)->get'
294 FindApp::Object::State::Group::Dirs::new FindApp::Object::State::Group::Dirs R B G
295 FindApp::Object::State::Group::Dirs::add FindApp::Object::State::Group::Dirs=ARRAY(0x7f9e8b02f780) R B G
296 FindApp::Object::State::Group::Dirs::set FindApp::Object::State::Group::Dirs=ARRAY(0x7f9e8b02f780) R B G
297 FindApp::Object::State::Group::Dirs::add FindApp::Object::State::Group::Dirs=ARRAY(0x7f9e8b02f780) C M Y
298 FindApp::Object::State::Group::Dirs::set FindApp::Object::State::Group::Dirs=ARRAY(0x7f9e8b02f780) B G R C M Y
299 BCGMRY
300  
301 =item FINDAPP_DEBUG_SHORTEN
302  
303 If true, this will shorten the trace and debug output by abbreviating
304 package names that start with "FindApp".
305  
306 tchrist% FINDAPP_DEBUG_SHORTEN=1 FINDAPP_TRACE=3 perl -MFindApp::Object::State::Group::Dirs -E 'say FindApp::Object::State::Group::Dirs->new(<R B G>)->add(<C M Y>)->get'
307 f:o:s:g:Dirs::new f:o:s:Group::Dirs R B G
308 f:o:s:g:Dirs::add f:o:s:Group::Dirs=ARRAY(0x7fc00b030738) R B G
309 f:o:s:g:Dirs::set f:o:s:Group::Dirs=ARRAY(0x7fc00b030738) R B G
310 f:o:s:g:Dirs::add f:o:s:Group::Dirs=ARRAY(0x7fc00b030738) C M Y
311 f:o:s:g:Dirs::set f:o:s:Group::Dirs=ARRAY(0x7fc00b030738) B G R C M Y
312 BCGMRY
313  
314 Which is much easier to read. The last two components in the
315 fully-qualfied name are always left as is, with everything else clipped to
316 a single lowercase letter and a single colon instead of a double one for
317 the seprator.
318  
319 Likewise, instead of this:
320  
321 tchrist% perl -MFindApp::Object::State::Group::Dirs -e 'print FindApp::Object::State::Group::Dirs->new(<R B G>)->add(<C M Y>)'
322 bless(["B", "C", "G", "M", "R", "Y"], "FindApp::Object::State::Group::Dirs"); # FindApp::Object::State::Group::Dirs=ARRAY(0x7fcaf302f018)
323  
324  
325 With the shortening, you get this:
326  
327 tchrist% FINDAPP_DEBUG_SHORTEN=1 perl -MFindApp::Object::State::Group::Dirs -e 'print FindApp::Object::State::Group::Dirs->new(<R B G>)->add(<C M Y>)'
328 bless(["B", "C", "G", "M", "R", "Y"], "f:o:s:Group::Dirs"); # FindApp::Object::State::Group::Dirs=ARRAY(0x7fc79102f030)
329  
330 The object type in the comment is not subject to shortening.
331  
332 =back
333  
334 =head1 SEE ALSO
335  
336 =over
337  
338 =item L<FindApp>
339  
340 =back
341  
342 =head1 CAVEATS AND PROVISOS
343  
344 Do not rely on the internal representation of these
345 object being an array reference.
346  
347 =head1 AUTHOR
348  
349 Tom Christiansen C<< <tchrist@perl.com> >>
350  
351 =head1 LICENCE AND COPYRIGHT
352  
353 Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>.
354 All Rights Reserved.
355  
356 This module is free software; you can redistribute it and/or
357 modify it under the same terms as Perl itself. See L<perlartistic>.
358  
359 =cut
360