Split role application to a module like Moose
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
CommitLineData
3a63a2e7 1package Mouse::Meta::Module;
637d4f17 2use Mouse::Util qw/:meta get_code_package get_code_ref not_supported/; # enables strict and warnings
3a63a2e7 3
b397e301 4use Carp ();
5use Scalar::Util ();
fce211ae 6
fe1221ad 7my %METAS;
8536d351 8
029463f4 9if(Mouse::Util::MOUSE_XS){
fd168725 10 # register meta storage for performance
11 Mouse::Util::__register_metaclass_storage(\%METAS, 0);
12
13 # ensure thread safety
14 *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) };
15}
16
deb9a0f3 17sub _metaclass_cache { # DEPRECATED
230dd14a 18 my($self, $name) = @_;
8b0573c6 19 Carp::cluck('_metaclass_cache() has been deprecated. Use Mouse::Util::get_metaclass_by_name() instead');
fe1221ad 20 return $METAS{$name};
739525d0 21}
8536d351 22
739525d0 23sub initialize {
24 my($class, $package_name, @args) = @_;
8536d351 25
739525d0 26 ($package_name && !ref($package_name))
27 || $class->throw_error("You must pass a package name and it cannot be blessed");
8536d351 28
fe1221ad 29 return $METAS{$package_name}
739525d0 30 ||= $class->_construct_meta(package => $package_name, @args);
31}
8536d351 32
542f20ad 33sub reinitialize {
34 my($class, $package_name, @args) = @_;
35
f87debb9 36 $package_name = $package_name->name if ref $package_name;
37
542f20ad 38 ($package_name && !ref($package_name))
39 || $class->throw_error("You must pass a package name and it cannot be blessed");
40
902174eb 41 if(exists $METAS{$package_name}) {
42 unshift @args, %{ $METAS{$package_name} };
43 }
542f20ad 44 delete $METAS{$package_name};
45 return $class->initialize($package_name, @args);
46}
47
48sub _class_of{
739525d0 49 my($class_or_instance) = @_;
50 return undef unless defined $class_or_instance;
23274b88 51 return $METAS{ ref($class_or_instance) || $class_or_instance };
8536d351 52}
53
739525d0 54# Means of accessing all the metaclasses that have
55# been initialized thus far
542f20ad 56#sub _get_all_metaclasses { %METAS }
57sub _get_all_metaclass_instances { values %METAS }
58sub _get_all_metaclass_names { keys %METAS }
59sub _get_metaclass_by_name { $METAS{$_[0]} }
60#sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
61#sub _weaken_metaclass { weaken($METAS{$_[0]}) }
62#sub _does_metaclass_exist { defined $METAS{$_[0]} }
63#sub _remove_metaclass_by_name { delete $METAS{$_[0]} }
739525d0 64
43165725 65sub name;
3a63a2e7 66
2591e962 67sub namespace;
68
8536d351 69# add_attribute is an abstract method
70
deb9a0f3 71sub get_attribute_map { # DEPRECATED
8b0573c6 72 Carp::cluck('get_attribute_map() has been deprecated. Use get_attribute_list() and get_attribute() instead');
3a683350 73 return $_[0]->{attributes};
74}
75
8536d351 76sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
77sub get_attribute { $_[0]->{attributes}->{$_[1]} }
6cfa1e5e 78sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
23264b5b 79
047d7af0 80sub get_attribute_list{ keys %{$_[0]->{attributes}} }
81
01afd8ff 82# XXX: for backward compatibility
83my %foreign = map{ $_ => undef } qw(
84 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
81bc0675 85 Carp Scalar::Util List::Util
01afd8ff 86);
87sub _code_is_mine{
81bc0675 88# my($self, $code) = @_;
8e64d0fa 89
81bc0675 90 return !exists $foreign{ get_code_package($_[1]) };
3a63a2e7 91}
92
3e44140b 93sub add_method;
94
3a63a2e7 95sub has_method {
96 my($self, $method_name) = @_;
97
78dc5ed1 98 defined($method_name)
99 or $self->throw_error('You must define a method name');
100
81bc0675 101 return defined($self->{methods}{$method_name}) || do{
102 my $code = get_code_ref($self->{package}, $method_name);
103 $code && $self->_code_is_mine($code);
104 };
3a63a2e7 105}
106
81bc0675 107sub get_method_body {
71e7b544 108 my($self, $method_name) = @_;
109
110 defined($method_name)
111 or $self->throw_error('You must define a method name');
112
113 return $self->{methods}{$method_name} ||= do{
7d96ae4d 114 my $code = get_code_ref($self->{package}, $method_name);
81bc0675 115 $code && $self->_code_is_mine($code) ? $code : undef;
71e7b544 116 };
117}
118
8536d351 119sub get_method{
6cfa1e5e 120 my($self, $method_name) = @_;
121
81bc0675 122 if(my $code = $self->get_method_body($method_name)){
637d4f17 123 return Mouse::Util::load_class($self->method_metaclass)->wrap(
81bc0675 124 body => $code,
9010458d 125 name => $method_name,
60e446e8 126 package => $self->name,
9010458d 127 associated_metaclass => $self,
6cfa1e5e 128 );
129 }
130
131 return undef;
8536d351 132}
3a63a2e7 133
8e64d0fa 134sub get_method_list {
3a63a2e7 135 my($self) = @_;
8e64d0fa 136
137 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 138}
139
013ee5f0 140sub _collect_methods { # Mouse specific
141 my($meta, @args) = @_;
142
143 my @methods;
144 foreach my $arg(@args){
145 if(my $type = ref $arg){
146 if($type eq 'Regexp'){
147 push @methods, grep { $_ =~ $arg } $meta->get_all_method_names;
148 }
149 elsif($type eq 'ARRAY'){
150 push @methods, @{$arg};
151 }
152 else{
153 my $subname = ( caller(1) )[3];
154 $meta->throw_error(
155 sprintf(
a4faea11 156 'Methods passed to %s must be provided as a list,'
157 . ' ArrayRef or regular expression, not %s',
013ee5f0 158 $subname,
159 $type,
160 )
161 );
162 }
163 }
164 else{
165 push @methods, $arg;
166 }
167 }
168 return @methods;
169}
170
72dc2c9d 171my $ANON_SERIAL = 0; # anonymous class/role id
172my %IMMORTALS; # immortal anonymous classes
013ee5f0 173
72dc2c9d 174sub create {
175 my($self, $package_name, %options) = @_;
7a50b450 176
72dc2c9d 177 my $class = ref($self) || $self;
178 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 179
72dc2c9d 180 my $superclasses;
181 if(exists $options{superclasses}){
182 if(Mouse::Util::is_a_metarole($self)){
183 delete $options{superclasses};
8f40866c 184 }
72dc2c9d 185 else{
186 $superclasses = delete $options{superclasses};
187 (ref $superclasses eq 'ARRAY')
188 || $self->throw_error("You must pass an ARRAY ref of superclasses");
7a50b450 189 }
72dc2c9d 190 }
7a50b450 191
72dc2c9d 192 my $attributes = delete $options{attributes};
193 if(defined $attributes){
194 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
195 || $self->throw_error("You must pass an ARRAY ref of attributes");
196 }
197 my $methods = delete $options{methods};
198 if(defined $methods){
199 (ref $methods eq 'HASH')
200 || $self->throw_error("You must pass a HASH ref of methods");
201 }
202 my $roles = delete $options{roles};
203 if(defined $roles){
204 (ref $roles eq 'ARRAY')
205 || $self->throw_error("You must pass an ARRAY ref of roles");
206 }
207 my $mortal;
208 my $cache_key;
209
210 if(!defined $package_name){ # anonymous
211 $mortal = !$options{cache};
212
213 # anonymous but immortal
214 if(!$mortal){
215 # something like Super::Class|Super::Class::2=Role|Role::1
216 $cache_key = join '=' => (
217 join('|', @{$superclasses || []}),
218 join('|', sort @{$roles || []}),
219 );
220 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
7a50b450 221 }
72dc2c9d 222 $options{anon_serial_id} = ++$ANON_SERIAL;
223 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
224 }
7a50b450 225
72dc2c9d 226 # instantiate a module
227 {
228 no strict 'refs';
229 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
230 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
231 }
232
233 my $meta = $self->initialize( $package_name, %options);
234
235 Scalar::Util::weaken $METAS{$package_name}
236 if $mortal;
237
a062712d 238 $meta->add_method(meta => sub {
72dc2c9d 239 $self->initialize(ref($_[0]) || $_[0]);
240 });
241
242 $meta->superclasses(@{$superclasses})
243 if defined $superclasses;
244
245 # NOTE:
246 # process attributes first, so that they can
247 # install accessors, but locally defined methods
248 # can then overwrite them. It is maybe a little odd, but
249 # I think this should be the order of things.
250 if (defined $attributes) {
251 if(ref($attributes) eq 'ARRAY'){
252 # array of Mouse::Meta::Attribute
253 foreach my $attr (@{$attributes}) {
254 $meta->add_attribute($attr);
7a50b450 255 }
256 }
72dc2c9d 257 else{
258 # hash map of name and attribute spec pairs
259 while(my($name, $attr) = each %{$attributes}){
260 $meta->add_attribute($name => $attr);
7a50b450 261 }
262 }
72dc2c9d 263 }
264 if (defined $methods) {
265 while(my($method_name, $method_body) = each %{$methods}){
266 $meta->add_method($method_name, $method_body);
7a50b450 267 }
72dc2c9d 268 }
269 if (defined $roles){
270 Mouse::Util::apply_all_roles($package_name, @{$roles});
271 }
7a50b450 272
72dc2c9d 273 if($cache_key){
274 $IMMORTALS{$cache_key} = $meta;
7a50b450 275 }
276
72dc2c9d 277 return $meta;
278}
7a50b450 279
72dc2c9d 280sub DESTROY{
281 my($self) = @_;
39a8df63 282
72dc2c9d 283 return if $Mouse::Util::in_global_destruction;
7a50b450 284
72dc2c9d 285 my $serial_id = $self->{anon_serial_id};
72dc2c9d 286 return if !$serial_id;
e75f21db 287
a4faea11 288 # XXX: cleaning stash with threads causes panic/SEGV on legacy perls.
20a12328 289 if(exists $INC{'threads.pm'}) {
290 # (caller)[2] indicates the caller's line number,
a4faea11 291 # which is zero when the current thread is joining (destroying).
20a12328 292 return if( (caller)[2] == 0);
293 }
294
a4faea11 295 # clean up mortal anonymous class stuff
296
297 # @ISA is a magical variable, so we must clear it manually.
72dc2c9d 298 @{$self->{superclasses}} = () if exists $self->{superclasses};
7a50b450 299
72dc2c9d 300 # Then, clear the symbol table hash
301 %{$self->namespace} = ();
8f40866c 302
72dc2c9d 303 my $name = $self->name;
304 delete $METAS{$name};
7a50b450 305
72dc2c9d 306 $name =~ s/ $serial_id \z//xms;
72dc2c9d 307 no strict 'refs';
308 delete ${$name}{ $serial_id . '::' };
72dc2c9d 309 return;
7a50b450 310}
311
3a63a2e7 312
3131;
3a63a2e7 314__END__
315
316=head1 NAME
317
a4faea11 318Mouse::Meta::Module - The common base class of Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 319
a25ca8d6 320=head1 VERSION
321
86eb0b5e 322This document describes Mouse version 0.70
a25ca8d6 323
503ed648 324=head1 DESCRIPTION
325
a4faea11 326This class is an abstract base class of meta classes and meta roles.
503ed648 327
b995be6a 328=head1 SEE ALSO
329
31c5194b 330L<Class::MOP::Class>
331
332L<Class::MOP::Module>
333
334L<Class::MOP::Package>
b995be6a 335
3a63a2e7 336=cut
2cea7a5f 337