FindApp/Object/State/Group/State/Dirs.pm

filter: Statementstmt 67.2% 76 / 113 Branchbran 44.4% 8 / 18 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 74.0% 20 / 27 Podpod 100% 16 / 16 totaltotal 61.8% 123 / 199 CC 19 SCAR 12.8
CC
19
Cov
60%
has
12.0
del
12.0
class
6.0
CRAP 3.6
« FindApp/Object/Behavior/Overloading.pm ↑ Summary FindApp/Object/Behavior/Loader.pm »
1 package FindApp::Object::State::Group::State::Dirs;
2  
3 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use mro "c3";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use FindApp::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 :assert
10 :debug
11 :foreign
12 :list
13 6 );
14  
15 373 ▶sub all_defined {
Subroutine all_defined: called CC=2 SCAR=8.1
16 373 ▶ unless (@_ == grep {defined} @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == grep({defined $_;} @_)) 0 373 373
17 0 subcroak_N(2, "undefined arguments forbidden");
18 }
19 }
20  
21 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 ################################################################
24  
25 6 ▶use FindApp::Utils ":overload";
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 ▶use overload reverse (
Subroutine BEGIN: called CC=1 SCAR=0.0
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 6 );
35  
36 0 ▶sub op_eq {
Subroutine op_eq: not called CC=1 SCAR=2.6
Pod documented
37 0 ▶ my($this, $that, $swapped) = @_;
Subroutine op_eq: not called CC=1 SCAR=2.6
Pod documented
38 6 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 0 local $" = "\0"; # these are path elements, so NUL illegal
40 0 return "@$this" eq "@$that"; # "guaranteed" to be ordered
41 }
42  
43 0 ▶sub op_ne { !&op_eq }
Subroutine op_ne: not called CC=1 SCAR=6.9
Pod documented
44  
45 0 ▶sub class {
Subroutine class: not called CC=2 SCAR=17.9
Pod documented
46 0 my $self = shift;
47 0 good_args(@_ == 0);
48 0 ▶ return blessed($self) || $self;
Condition0%0/3
&blessed($self) || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
&blessed($self) || $self
&blessed($self) $self
Truth table0%0/3
A || B
A= &blessed($self)B= $self
ABresult
1X1
011
000
49 }
50  
51 108 ▶sub object {
Subroutine object: called CC=2 SCAR=7.0
Pod documented
52 108 my $self = shift;
53 108 good_args(@_ == 0);
54 108 ▶ croak "not an object -- and no old class object to uncache" unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless &blessed($self) 0 108 108
55 108 return $self;
56 }
57  
58 168 ▶sub new { &ENTER_TRACE_3;
Subroutine new: called CC=5 SCAR=17.6
Pod documented
59 168 my($invocant, @values) = @_;
60 168 all_defined(@values);
61 168 ▶ my $class = blessed($invocant) || $invocant;
Condition33%1/3
&blessed($invocant) || $invocant
l!l&&r!l&&!r
01680
MC/DC0%0/2
&blessed($invocant) || $invocant
&blessed($invocant) $invocant
Truth table33%1/3
A || B
A= &blessed($invocant)B= $invocant
ABresult
1X1
011
000
62 168 ▶ my $old = blessed($invocant) && $invocant;
Condition33%1/3
&blessed($invocant) && $invocant
!ll&&!rl&&r
16800
MC/DC0%0/2
&blessed($invocant) && $invocant
&blessed($invocant) $invocant
Truth table33%1/3
A && B
A= &blessed($invocant)B= $invocant
ABresult
0X0
100
111
63 168 my $new = bless([], $class);
64 168 ▶ $new->copy($old) if $old;
Branch: 1 branch
BranchTrueFalseTotal
if $old 0 168 168
65 168 ▶ $new->add(@values) if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 0 168 168
66 168 return $new;
67 }
68  
69 108 ▶sub copy { &ENTER_TRACE_3;
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
70 108 good_args(@_ == 2);
71 108 my($new, $old) = @_;
72 108 ▶ $new->set($old->get) if $old->count;
Branch: 1 branch
BranchTrueFalseTotal
if $old->count 45 63 108
73 }
74  
75 161 ▶sub get {
Subroutine get: called CC=2 SCAR=7.1
Pod documented
76 161 my $self = shift;
77 161 good_args(@_ == 0);
78 161 ▶ return wantarray ? @$self
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 161 0 161
79 : $$self[0];
80 }
81  
82 165 ▶sub set { &ENTER_TRACE_3;
Subroutine set: called CC=3 SCAR=11.4
Pod documented
83 165 my($self, @values) = @_;
84 165 all_defined(@values);
85 165 ▶ @values = map { (ref && reftype eq "ARRAY") ? @$_ : $_ } @values;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && &reftype($_) eq 'ARRAY' ? : 0 273 273
Condition33%1/3
ref $_ and &reftype($_) eq "ARRAY"
!ll&&!rl&&r
27300
MC/DC0%0/2
ref $_ and &reftype($_) eq "ARRAY"
ref $_ &reftype($_) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_B= &reftype($_) eq "ARRAY"
ABresult
0X0
100
111
86 165 @$self = uniq sort @values;
87 165 return $self;
88 }
89  
90 32 ▶sub reset { &ENTER_TRACE_3;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
91 32 my $self = shift;
92 32 good_args(@_ == 0);
93 32 my @old_bits = @$self;
94 32 @$self = ();
95 32 return @old_bits;
96 }
97  
98 # these should not exist
99 4 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
100 4 my $self = shift;
101 4 good_args(@_ == 0);
102 4 return $$self[0];
103 }
104  
105 # these should not exist
106 0 ▶sub last {
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
107 0 my $self = shift;
108 0 good_args(@_ == 0);
109 0 return $$self[-1];
110 }
111  
112 126 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
113 126 my $self = shift;
114 126 good_args(@_ == 0);
115 126 return 0+@$self;
116 }
117  
118 0 ▶sub has { &ENTER_TRACE_3;
Subroutine has: not called CC=3 SCAR=24.8
Pod documented
119 0 my($self, @values) = @_;
120 0 good_args(@values != 0);
121 0 all_defined(@values);
122 0 my %have = map { $_ => 1 } $self->get;
123 0 for my $value (@values) {
124 0 ▶ $have{$value} || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$have{$value} or return 0 0 0 0
125 }
126 0 return 1;
127 }
128  
129 6 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 40 ▶sub add { &ENTER_TRACE_3;
Subroutine add: called CC=1 SCAR=0.0
Pod documented
132 40 my($self, @values) = @_;
133 40 good_args(@values != 0);
134 40 all_defined(@values);
135 40 return $self->set($self->get, @values);
136 }
137  
138 0 ▶sub del { &ENTER_TRACE_3;
Subroutine del: not called CC=3 SCAR=24.8
Pod documented
139 0 my($self, @values) = @_;
140 0 good_args(@values != 0);
141 0 all_defined(@values);
142 0 ▶ cluck "silly bits" if $values[0] eq "1";
Branch: 1 branch
BranchTrueFalseTotal
if $values[0] eq 1 0 0 0
143 0 my %have = map { ($_ => 1) } $self->get;
144 0 my %gone = map { ($_ => 1) } @values;
145 0 ▶ my @removed = grep { $have{$_} && $gone{$_} } @values;
Condition0%0/3
$have{$_} && $gone{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$have{$_} && $gone{$_}
$have{$_} $gone{$_}
Truth table0%0/3
A && B
A= $have{$_}B= $gone{$_}
ABresult
0X0
100
111
146 0 $self->set(grep { ! $gone{$_} } keys %have);
147 0 return @removed;
148 }
149  
150 6 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  
« FindApp/Object/Behavior/Overloading.pm ↑ Summary FindApp/Object/Behavior/Loader.pm »