File Coverage

blib/lib/MooseX/TraitFor/Meta/Class/BetterAnonClassNames.pm
Criterion Covered Total %
statement 16 16 100.0
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 24 25 96.0


line stmt bran cond sub pod time code
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             BEGIN {
12 1     1   835224 $MooseX::TraitFor::Meta::Class::BetterAnonClassNames::AUTHORITY = 'cpan:RSRCHBOY';
13             }
14             # git description: 7888bc7
15             $MooseX::TraitFor::Meta::Class::BetterAnonClassNames::VERSION = '0.002001';
16              
17             # ABSTRACT: Metaclass trait to *attempt* to demystify generated anonymous class names
18              
19 1     1   9 use Moose::Role;
  1         3  
  1         12  
20 1     1   7569 use namespace::autoclean;
  1         1640  
  1         6  
21 1     1   1260 use autobox::Core;
  1         10464  
  1         14  
22              
23             has is_anon => (is => 'ro', isa => 'Bool', default => 0);
24              
25             has anon_package_prefix => (
26             is => 'ro',
27             isa => 'Str',
28             builder => '_build_anon_package_prefix',
29             );
30              
31 1     1   3711 sub _build_anon_package_prefix { Moose::Meta::Class->_anon_package_prefix }
32              
33 1     1   5 sub _anon_package_middle { '::__ANON__::SERIAL::' }
34              
35             # XXX around?
36             sub _anon_package_prefix {
37 3     3   36 my $thing = shift @_;
38              
39 3 50       155 my $prefix = blessed $thing
40             ? $thing->anon_package_prefix
41             : Moose::Meta::Class->_anon_package_prefix
42             ;
43              
44 3         25 my @caller = caller 1;
45             ### @caller
46             ### $prefix
47 3         13 return $prefix;
48             }
49              
50             around create => sub {
51             my ($orig, $self) = (shift, shift);
52             my @args = @_;
53              
54             unshift @args, 'package'
55             if @args % 2;
56             my %opts = @args;
57              
58             return $self->$orig(%opts)
59             unless $opts{is_anon} && $opts{anon_package_prefix};
60              
61             ### old anon package name: $opts{package}
62             my $serial = $opts{package}->split(qr/::/)->[-1]; #at(-1); #tail(1);
63             $opts{package} = $opts{anon_package_prefix} . $serial;
64              
65             ### new anon package name: $opts{package}
66             ### %opts
67             return $self->$orig(%opts);
68             };
69              
70             around create_anon_class => sub {
71             my ($orig, $class) = (shift, shift);
72             my %opts = @_;
73              
74             # we're going to need some additional logic here to make sure that our
75             # prefixes make sense; e.g. if we're an anon descendent of an anon class,
76             # we should just use our parent's prefix.
77              
78             $opts{is_anon} = 1;
79              
80             my $superclasses = $opts{superclasses} || [];
81              
82             # don't bother doing anything else if we don't have anything to add
83             return $class->$orig(%opts)
84             if exists $opts{anon_package_prefix};
85             return $class->$orig(%opts)
86             unless @$superclasses && @$superclasses == 1;
87              
88             # XXX ::class_of?
89             my $sc = $superclasses->[0]->meta;
90              
91             #my $prefix
92             $opts{anon_package_prefix}
93             = $sc->is_anon
94             ? $sc->_anon_package_prefix
95             : $superclasses->[0] . $class->_anon_package_middle
96             ;
97              
98             # basically: if we have no superclasses, live with the default prefix; if
99             # we have a superclass and it's anon, use it's anon prefix; if we have a
100             # superclass and it's not anon, make a new prefix out of it
101             #
102             # cases where we have 2+ superclasses aren't handled right now; we use the
103             # Moose::Meta::Class::_anon_package_prefix() for those
104             #
105             # if we're passed in 'anon_package_prefix', then just use that.
106              
107             return $class->$orig(%opts);
108             };
109              
110             !!42;
111              
112             __END__
113              
114             =pod
115              
116             =encoding UTF-8
117              
118             =for :stopwords Chris Weyl
119              
120             =for :stopwords Wishlist flattr flattr'ed gittip gittip'ed
121              
122             =head1 NAME
123              
124             MooseX::TraitFor::Meta::Class::BetterAnonClassNames - Metaclass trait to *attempt* to demystify generated anonymous class names
125              
126             =head1 VERSION
127              
128             This document describes version 0.002001 of MooseX::TraitFor::Meta::Class::BetterAnonClassNames - released May 22, 2014 as part of MooseX-TraitFor-Meta-Class-BetterAnonClassNames.
129              
130             =head1 SUMMARY
131              
132             You really want to be looking at L<MooseX::Util/with_traits>.
133              
134             =head1 SEE ALSO
135              
136             Please see those modules/websites for more information related to this module.
137              
138             =over 4
139              
140             =item *
141              
142             L<MooseX::Util>
143              
144             =back
145              
146             =head1 SOURCE
147              
148             The development version is on github at L<http://https://github.com/RsrchBoy/moosex-traitfor-class-betteranonclassnames>
149             and may be cloned from L<git://https://github.com/RsrchBoy/moosex-traitfor-class-betteranonclassnames.git>
150              
151             =head1 BUGS
152              
153             Please report any bugs or feature requests on the bugtracker website
154             https://github.com/RsrchBoy/moosex-traitfor-class-betteranonclassnames/issu
155             es
156              
157             When submitting a bug or request, please include a test-file or a
158             patch to an existing test-file that illustrates the bug or desired
159             feature.
160              
161             =head1 AUTHOR
162              
163             Chris Weyl <cweyl@alumni.drew.edu>
164              
165             =head2 I'm a material boy in a material world
166              
167             =begin html
168              
169             <a href="https://www.gittip.com/RsrchBoy/"><img src="https://raw.githubusercontent.com/gittip/www.gittip.com/master/www/assets/%25version/logo.png" /></a>
170             <a href="http://bit.ly/rsrchboys-wishlist"><img src="http://wps.io/wp-content/uploads/2014/05/amazon_wishlist.resized.png" /></a>
171             <a href="https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-traitfor-class-betteranonclassnames&title=RsrchBoy's%20CPAN%20MooseX-TraitFor-Meta-Class-BetterAnonClassNames&tags=%22RsrchBoy's%20MooseX-TraitFor-Meta-Class-BetterAnonClassNames%20in%20the%20CPAN%22"><img src="http://api.flattr.com/button/flattr-badge-large.png" /></a>
172              
173             =end html
174              
175             Please note B<I do not expect to be gittip'ed or flattr'ed for this work>,
176             rather B<it is simply a very pleasant surprise>. I largely create and release
177             works like this because I need them or I find it enjoyable; however, don't let
178             that stop you if you feel like it ;)
179              
180             L<Flattr this|https://flattr.com/submit/auto?user_id=RsrchBoy&url=https%3A%2F%2Fgithub.com%2FRsrchBoy%2Fmoosex-traitfor-class-betteranonclassnames&title=RsrchBoy's%20CPAN%20MooseX-TraitFor-Meta-Class-BetterAnonClassNames&tags=%22RsrchBoy's%20MooseX-TraitFor-Meta-Class-BetterAnonClassNames%20in%20the%20CPAN%22>,
181             L<gittip me|https://www.gittip.com/RsrchBoy/>, or indulge my
182             L<Amazon Wishlist|http://bit.ly/rsrchboys-wishlist>... If you so desire.
183              
184             =head1 COPYRIGHT AND LICENSE
185              
186             This software is Copyright (c) 2014 by Chris Weyl.
187              
188             This is free software, licensed under:
189              
190             The GNU Lesser General Public License, Version 2.1, February 1999
191              
192             =cut