| 1 | package FindApp::Object::Class; | ||||||||||||||||||||||||
| 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::Vars qw(:all); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 6 | ▶ | use FindApp::Utils qw(:all); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | 6 | ▶ | use namespace::clean; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | 0 | ▶ | sub class($) { | ||||||||||||||||||||||
|
Subroutine
class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 14 | 0 | ▶ | my $self = &myself; | ||||||||||||||||||||||
|
Subroutine
class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 15 | 0 | return blessed($self); | |||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||
| 18 | # postfix scalar operator | ||||||||||||||||||||||||
| 19 | 0 | ▶ | sub object($) { | ||||||||||||||||||||||
|
Subroutine
object: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 20 | 0 | ▶ | my $self = &myself; | ||||||||||||||||||||||
|
Subroutine
object: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 21 | 0 | 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 | 9 | ▶ | sub renew { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
renew: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 32 | 9 | my $self = shift; | |||||||||||||||||||||||
| 33 | 9 | my $new = $self->new; | |||||||||||||||||||||||
| 34 | 9 | my $old = $self->old; | |||||||||||||||||||||||
| 35 | 9 | $new->copy($old); | |||||||||||||||||||||||
| 36 | 9 | $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 | 26 | ▶ | sub old { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
old: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||
| 44 | 26 | my($self, $new) = @_; | |||||||||||||||||||||||
| 45 | 26 | state $Singleton; | |||||||||||||||||||||||
| 46 | 26 | ▶ | $Singleton = $new if $new; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 47 | 26 | ▶ | return $Singleton ||= $self->new; | ||||||||||||||||||||||
|
Condition66%2/3
$Singleton ||= $self->new
MC/DC0%0/2
$Singleton ||= $self->new
$Singleton
$self->new
Truth table66%2/3
A ||= B
A= $SingletonB= $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 | 14 | ▶ | sub new { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||
| 58 | 14 | my $old = shift; | |||||||||||||||||||||||
| 59 | 14 | ▶ | my $class = blessed($old) || $old || __PACKAGE__; | ||||||||||||||||||||||
|
Condition40%2/5
&blessed($old) || $old || __PACKAGE__
&blessed($old) or $old
MC/DC0%0/2
&blessed($old) || $old || __PACKAGE__
&blessed($old)
$old
Truth table33%1/3
A or B
A= &blessed($old)B= $old
| |||||||||||||||||||||||||
| 60 | 14 | my $new = bless { }, $class; | |||||||||||||||||||||||
| 61 | 14 | $new->init; | |||||||||||||||||||||||
| 62 | 14 | ▶ | $new->copy($old) if blessed($old); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 63 | 14 | ▶ | $new->params(@_) if @_; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 64 | 14 | 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 | 14 | ▶ | sub init { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||
| 78 | 14 | my $self = shift; | |||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | 14 | $self->reset_origin; | |||||||||||||||||||||||
| 81 | 14 | $self->default_origin("script"); | |||||||||||||||||||||||
| 82 | 14 | $self->allocate_groups; | |||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | 14 | $self->group("root")->allowed->add("."); | |||||||||||||||||||||||
| 85 | 14 | for my $root ($self->rootdir->object) { | |||||||||||||||||||||||
| 86 | 14 | $root->allowed("."); | |||||||||||||||||||||||
| 87 | 14 | $root->wanted("lib/"); | |||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||
| 90 | 14 | for my $group ($self->subgroups) { | |||||||||||||||||||||||
| 91 | 42 | $group->allowed($group->name); | |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||
| 94 | 14 | $self->bindirs->allowed->add( glob "{script,util}{,s}" ); | |||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | 14 | $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 | 9 | ▶ | sub copy { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
copy: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||
| 107 | 9 | good_args(@_ == 2); | |||||||||||||||||||||||
| 108 | 9 | my($new, $old) = @_; | |||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||
| 110 | 9 | ▶ | $new->origin($old->origin) if $old->has_origin; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 111 | 9 | $new->default_origin($old->default_origin); | |||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | 9 | for my $from_group ($old->groups) { | |||||||||||||||||||||||
| 114 | 36 | my $name = $from_group->name; | |||||||||||||||||||||||
| 115 | 36 | $new->group($name)->copy($from_group); | |||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||
| 118 | 9 | 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 | 0 | ▶ | sub params { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
params: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 128 | 0 | my($self, %method_arg_pair) = @_; | |||||||||||||||||||||||
| 129 | 0 | ▶ | while (my($method, $argref) = each %method_arg_pair) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 130 | 0 | $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 | 0 | ▶ | sub adopts_children { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
adopts_children: not called CC=4 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||
| 143 | 0 | my($class, @children) = @_; | |||||||||||||||||||||||
| 144 | 0 | for my $child (@children) { | |||||||||||||||||||||||
| 145 | 0 | ▶ | next if $child eq "FindApp"; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 146 | 6 | ▶ | no strict "refs"; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 147 | 0 | ▶ | say "push @{ $child . '::ISA' }, $class" if $Debugging; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 148 | 0 | 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 | 0 | ▶ | sub adopts_parents { &ENTER_TRACE; | ||||||||||||||||||||||
|
Subroutine
adopts_parents: not called CC=6 SCAR=34.7
Pod
documented
| |||||||||||||||||||||||||
| 159 | 6 | ▶ | no strict "refs"; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 160 | 0 | my($class, @parents) = @_; | |||||||||||||||||||||||
| 161 | 0 | ▶ | return unless @parents; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 162 | 0 | for my $module (@parents) { | |||||||||||||||||||||||
| 163 | 0 | $module =~ s/^(?=::)/FindApp/; | |||||||||||||||||||||||
| 164 | 0 | ▶ | eval qq{ require $module; 1 } || die; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||
| 166 | 0 | @parents = grep { ! $class->isa($_) } uniq @parents; | |||||||||||||||||||||||
| 167 | 0 | ▶ | unshift @parents, $class unless grep { $_->isa($class) } @parents; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 168 | 0 | my $parent = join "+", @parents; | |||||||||||||||||||||||
| 169 | 0 | ▶ | if (@parents > 1) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 170 | 0 | debug("$class CREATES A NEW PARENT $parent OUT OF THIN AIR"); | |||||||||||||||||||||||
| 171 | 0 | @{ $parent . "::ISA" } = @parents; # drat, need to make a new parent... | |||||||||||||||||||||||
| 172 | 0 | mro::set_mro($parent, "c3"); | |||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||
| 174 | 0 | $parent->renew(); | |||||||||||||||||||||||
| 175 | 0 | debug("$_[0] SINGLETON BECOMES $parent"); | |||||||||||||||||||||||
| 176 | 0 | $_[0] = $parent; # ...and a new self | |||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||
| 179 | 6 | 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 | |||||||||||||||||||||||||