blib/lib/FindApp/Object/Class.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 161 CC 10 SCAR 19.9
CC
10
Cov
0%
adopts_parents
20.0
new
12.0
adopts_children
12.0
CRAP 7.3
1 package FindApp::Object::Class;
2  
3 use v5.10;
4 use strict;
5 use warnings;
6 use mro "c3";
7  
8 use FindApp::Vars qw(:all);
9 use FindApp::Utils qw(:all);
10  
11 use namespace::clean;
12  
13 sub class($) {
14 my $self = &myself;
15 return blessed($self);
16 }
17  
18 # postfix scalar operator
19 sub object($) {
20 my $self = &myself;
21 return $self;
22 }
23  
24 ################################################################
25 # Our three constructors: new, old, renew.
26 ################################################################
27  
28 # Make and return a new singleton, copying the old one to it.
29 # This is so subclasses can stick their own singleton into our
30 # slot without knowing who else put what there.
31 sub renew { &ENTER_TRACE;
32 my $self = shift;
33 my $new = $self->new;
34 my $old = $self->old;
35 $new->copy($old);
36 $self->old($new);
37 }
38  
39 ## UNITCHECK { __PACKAGE__->renew } # stash new singleton
40  
41 # Return singleton, making one as needed.
42 # Stash the argument as the new singleton.
43 sub old { &ENTER_TRACE;
44 my($self, $new) = @_;
45 state $Singleton;
46 $Singleton = $new if $new;
47 return $Singleton ||= $self->new;
48 }
49  
50 # Allocate, initialize, and return a new instance
51 # of the invocant's class, or this one if none.
52 # As an instance method, copy the old instance's
53 # values to the new one. Run any params separately.
54 # Subclasses should override init and copy methods
55 # but invoke the c3 next::method to get these ones
56 # to run, too.
57 sub new { &ENTER_TRACE;
58 my $old = shift;
59 my $class = blessed($old) || $old || __PACKAGE__;
60 my $new = bless { }, $class;
61 $new->init;
62 $new->copy($old) if blessed($old);
63 $new->params(@_) if @_;
64 return $new;
65 }
66  
67 ################################################################
68 # Our three subconstructors: init, copy, params.
69 ################################################################
70  
71 # The job of an init() method is to populate its
72 # object's attributes. The base object has only 3:
73 # origin, default_origin, and groups. Subclasses
74 # with their own attributes should override this
75 # but call their c3 next::method first to eventually
76 # get back to this one.
77 sub init { &ENTER_TRACE;
78 my $self = shift;
79  
80 $self->reset_origin;
81 $self->default_origin("script");
82 $self->allocate_groups;
83  
84 $self->group("root")->allowed->add(".");
85 for my $root ($self->rootdir->object) {
86 $root->allowed(".");
87 $root->wanted("lib/");
88 }
89  
90 for my $group ($self->subgroups) {
91 $group->allowed($group->name);
92 }
93  
94 $self->bindirs->allowed->add( glob "{script,util}{,s}" );
95
96 $self->maybe::next::method;
97 }
98  
99 # The job of a copy() method is to mass-copy all the
100 # attributes of one object into another object. The
101 # receiving object is the invocant, and the donating
102 # object is the argument. This makes it look like
103 # assignment with the receiver on the left, saying
104 # the the new object gets a copy of the old object:
105 # $new->copy($old);
106 sub copy { &ENTER_TRACE;
107 good_args(@_ == 2);
108 my($new, $old) = @_;
109  
110 $new->origin($old->origin) if $old->has_origin;
111 $new->default_origin($old->default_origin);
112  
113 for my $from_group ($old->groups) {
114 my $name = $from_group->name;
115 $new->group($name)->copy($from_group);
116 }
117  
118 return $new;
119 }
120  
121 # Run methods passed as constructor arguments
122 # against the new object. This looks like
123 # named argument pairs, where the name is
124 # that of the method, and the argument is a
125 # reference to any array of arguments to that
126 # named method.
127 sub params { &ENTER_TRACE;
128 my($self, %method_arg_pair) = @_;
129 while (my($method, $argref) = each %method_arg_pair) {
130 $self->$method(@$argref);
131 }
132 }
133  
134 ################################################################
135 # Both class adoptors: adopts_children and adopts_parents.
136 ################################################################
137  
138 # Class method that inserts itself into the
139 # inheritance chain of its "new children".
140 # Used by the -subclass import pragma to make
141 # it easy for new classes to subclass us.
142 sub adopts_children { &ENTER_TRACE;
143 my($class, @children) = @_;
144 for my $child (@children) {
145 next if $child eq "FindApp";
146 no strict "refs";
147 say "push @{ $child . '::ISA' }, $class" if $Debugging;
148 push @{ $child . "::ISA" }, $class;
149 }
150 }
151  
152 # Class method that turns itself into a
153 # subclass of its "new parents". With one
154 # parent this is easy, but when inheriting
155 # from multiple parents, a new class must be
156 # generated: its name is that of all its
157 # parent classes, each separated by a "+".
158 sub adopts_parents { &ENTER_TRACE;
159 no strict "refs";
160 my($class, @parents) = @_;
161 return unless @parents;
162 for my $module (@parents) {
163 $module =~ s/^(?=::)/FindApp/;
164 eval qq{ require $module; 1 } || die;
165 }
166 @parents = grep { ! $class->isa($_) } uniq @parents;
167 unshift @parents, $class unless grep { $_->isa($class) } @parents;
168 my $parent = join "+", @parents;
169 if (@parents > 1) {
170 debug("$class CREATES A NEW PARENT $parent OUT OF THIN AIR");
171 @{ $parent . "::ISA" } = @parents; # drat, need to make a new parent...
172 mro::set_mro($parent, "c3");
173 }
174 $parent->renew();
175 debug("$_[0] SINGLETON BECOMES $parent");
176 $_[0] = $parent; # ...and a new self
177 }
178  
179 1;
180  
181 =encoding utf8
182  
183 =head1 NAME
184  
185 FindApp::Object::Class - FIXME
186  
187 =head1 SYNOPSIS
188  
189 use FindApp::Object::Class;
190  
191 =head1 DESCRIPTION
192  
193 =head2 Public Methods
194  
195 =over
196  
197 =item adopts_children
198  
199 =item adopts_parents
200  
201 =item class
202  
203 =item copy
204  
205 =item init
206  
207 =item new
208  
209 =item object
210  
211 =item old
212  
213 =item params
214  
215 =item renew
216  
217 =back
218  
219 =head2 Exports
220  
221 =over
222  
223 =item FIXME
224  
225 =back
226  
227 =head1 EXAMPLES
228  
229 =head1 ENVIRONMENT
230  
231 =head1 SEE ALSO
232  
233 =over
234  
235 =item L<FindApp>
236  
237 =back
238  
239 =head1 CAVEATS AND PROVISOS
240  
241 =head1 BUGS AND LIMITATIONS
242  
243 =head1 HISTORY
244  
245 =head1 AUTHOR
246  
247 Tom Christiansen << <tchrist@perl.com> >>
248  
249 =head1 LICENCE AND COPYRIGHT
250  
251 Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>.
252 All Rights Reserved.
253  
254 This module is free software; you can redistribute it and/or
255 modify it under the same terms as Perl itself. See L<perlartistic>.
256  
257 =cut
258