blib/lib/MooseX/TraitFor/Meta/Class/BetterAnonClassNames.pm

filter: Statementstmt 100% 43 / 43 Branchbran 58.3% 7 / 12 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 80.5% 62 / 77 CC 10 SCAR 13.1
CC
10
Cov
84%
__ANON__
6.6
__ANON__
4.3
_anon_package_prefix
2.0
CRAP 3.7
↑ Summary
1 #
2 # This file is part of MooseX-TraitFor-Meta-Class-BetterAnonClassNames
3 #
4 # This software is Copyright (c) 2014 by Chris Weyl.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package MooseX::TraitFor::Meta::Class::BetterAnonClassNames;
11 3 our $AUTHORITY = 'cpan:RSRCHBOY';
12 # git description: 0.002002-6-gfc980d7
13 3 $MooseX::TraitFor::Meta::Class::BetterAnonClassNames::VERSION = '0.002003';
14  
15 # ABSTRACT: Metaclass trait to *attempt* to demystify generated anonymous class names
16  
17 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use autobox::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 Moose::Exporter->setup_import_methods(
24 trait_aliases => [ __PACKAGE__ ],
25 );
26  
27  
28 3 has is_anon => (is => 'ro', isa => 'Bool', default => 0);
29  
30  
31 3 has anon_package_prefix => (
32 is => 'ro',
33 isa => 'Str',
34 builder => '_build_anon_package_prefix',
35 );
36  
37 1 ▶sub _build_anon_package_prefix { Moose::Meta::Class->_anon_package_prefix }
Subroutine _build_anon_package_prefix: called CC=1 SCAR=0.0
38  
39  
40 1 ▶sub _anon_package_middle { '::__ANON__::SERIAL::' }
Subroutine _anon_package_middle: called CC=1 SCAR=0.0
41  
42  
43 3 ▶sub _anon_package_prefix {
Subroutine _anon_package_prefix: called CC=2 SCAR=7.0
44 3 my $thing = shift @_;
45  
46 3 ▶ my $prefix = blessed $thing
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($thing) ? : 3 0 3
47 ? $thing->anon_package_prefix
48 : Moose::Meta::Class->_anon_package_prefix
49 ;
50  
51 3 my @caller = caller 1;
52 ### @caller
53 ### $prefix
54 3 return $prefix;
55 }
56  
57  
58 2 ▶around create => sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
59 2 my ($orig, $self) = (shift, shift);
60 2 my @args = @_;
61  
62 2 ▶ unshift @args, 'package'
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 2 0 2
63 if @args % 2;
64 2 my %opts = @args;
65  
66 return $self->$orig(%opts)
67 2 ▶ unless $opts{is_anon} && $opts{anon_package_prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"is_anon"} and $opts{"anon_package_prefix"} 0 2 2
Condition33%1/3
$opts{"is_anon"} and $opts{"anon_package_prefix"}
!ll&&!rl&&r
002
MC/DC0%0/2
$opts{"is_anon"} and $opts{"anon_package_prefix"}
$opts{"is_anon"} $opts{"anon_package_prefix"}
Truth table33%1/3
A and B
A= $opts{"is_anon"}B= $opts{"anon_package_prefix"}
ABresult
0X0
100
111
68  
69 ### old anon package name: $opts{package}
70 2 my $serial = $opts{package}->split(qr/::/)->[-1]; #at(-1); #tail(1);
71 2 $opts{package} = $opts{anon_package_prefix} . $serial;
72  
73 ### new anon package name: $opts{package}
74 ### %opts
75 2 return $self->$orig(%opts);
76 3 };
77  
78  
79 2 ▶around create_anon_class => sub {
Subroutine __ANON__: called CC=6 SCAR=18.8
80 2 my ($orig, $class) = (shift, shift);
81 2 my %opts = @_;
82  
83 # we're going to need some additional logic here to make sure that our
84 # prefixes make sense; e.g. if we're an anon descendent of an anon class,
85 # we should just use our parent's prefix.
86  
87 2 $opts{is_anon} = 1;
88  
89 2 ▶ my $superclasses = $opts{superclasses} || [];
Condition50%1/2
$opts{'superclasses'} || []
l!l
20
MC/DC0%0/1
$opts{'superclasses'} || []
$opts{'superclasses'}
Truth table50%1/2
A
A= $opts{'superclasses'}
Aresult
11
00
90  
91 # don't bother doing anything else if we don't have anything to add
92 return $class->$orig(%opts)
93 2 ▶ if exists $opts{anon_package_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"anon_package_prefix"} 0 2 2
94 2 ▶ return $class->$orig(%opts)
Branch: 1 branch
BranchTrueFalseTotal
unless @$superclasses and @$superclasses == 1 0 2 2
Condition33%1/3
@$superclasses and @$superclasses == 1
!ll&&!rl&&r
002
MC/DC0%0/2
@$superclasses and @$superclasses == 1
@$superclasses @$superclasses == 1
Truth table33%1/3
A and B
A= @$superclassesB= @$superclasses == 1
ABresult
0X0
100
111
95 unless @$superclasses && @$superclasses == 1;
96  
97 # XXX ::class_of?
98 2 my $sc = $superclasses->[0]->meta;
99  
100 #my $prefix
101 $opts{anon_package_prefix}
102 2 ▶ = $sc->is_anon
Branch: 1 branch
BranchTrueFalseTotal
$sc->is_anon ? : 1 1 2
103 ? $sc->_anon_package_prefix
104 : $superclasses->[0] . $class->_anon_package_middle
105 ;
106  
107 # basically: if we have no superclasses, live with the default prefix; if
108 # we have a superclass and it's anon, use it's anon prefix; if we have a
109 # superclass and it's not anon, make a new prefix out of it
110 #
111 # cases where we have 2+ superclasses aren't handled right now; we use the
112 # Moose::Meta::Class::_anon_package_prefix() for those
113 #
114 # if we're passed in 'anon_package_prefix', then just use that.
115  
116 2 return $class->$orig(%opts);
117 3 };
118  
119 3 !!42;
120  
121 __END__
↑ Summary