Tweaks
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
CommitLineData
3a63a2e7 1package Mouse::Meta::Module;
7d96ae4d 2use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings
3a63a2e7 3
b397e301 4use Carp ();
5use Scalar::Util ();
fce211ae 6
fe1221ad 7my %METAS;
8536d351 8
deb9a0f3 9sub _metaclass_cache { # DEPRECATED
739525d0 10 my($class, $name) = @_;
fe1221ad 11 return $METAS{$name};
739525d0 12}
8536d351 13
739525d0 14sub initialize {
15 my($class, $package_name, @args) = @_;
8536d351 16
739525d0 17 ($package_name && !ref($package_name))
18 || $class->throw_error("You must pass a package name and it cannot be blessed");
8536d351 19
fe1221ad 20 return $METAS{$package_name}
739525d0 21 ||= $class->_construct_meta(package => $package_name, @args);
22}
8536d351 23
739525d0 24sub class_of{
25 my($class_or_instance) = @_;
26 return undef unless defined $class_or_instance;
23274b88 27 return $METAS{ ref($class_or_instance) || $class_or_instance };
8536d351 28}
29
739525d0 30# Means of accessing all the metaclasses that have
31# been initialized thus far
fe1221ad 32#sub get_all_metaclasses { %METAS }
33sub get_all_metaclass_instances { values %METAS }
34sub get_all_metaclass_names { keys %METAS }
35sub get_metaclass_by_name { $METAS{$_[0]} }
36#sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
37#sub weaken_metaclass { weaken($METAS{$_[0]}) }
38#sub does_metaclass_exist { defined $METAS{$_[0]} }
39#sub remove_metaclass_by_name { delete $METAS{$_[0]} }
739525d0 40
43165725 41sub name;
3a63a2e7 42
2591e962 43sub namespace;
44
739525d0 45# The followings are Class::MOP specific methods
46
47#sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
48#sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
49#sub identifier {
50# my $self = shift;
51# return join '-' => (
52# $self->name,
53# ($self->version || ()),
54# ($self->authority || ()),
55# );
56#}
23264b5b 57
8536d351 58# add_attribute is an abstract method
59
deb9a0f3 60sub get_attribute_map { # DEPRECATED
3a683350 61 Carp::cluck('get_attribute_map() has been deprecated');
62 return $_[0]->{attributes};
63}
64
8536d351 65sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
66sub get_attribute { $_[0]->{attributes}->{$_[1]} }
67sub get_attribute_list{ keys %{$_[0]->{attributes}} }
6cfa1e5e 68sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
23264b5b 69
01afd8ff 70# XXX: for backward compatibility
71my %foreign = map{ $_ => undef } qw(
72 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
81bc0675 73 Carp Scalar::Util List::Util
01afd8ff 74);
75sub _code_is_mine{
81bc0675 76# my($self, $code) = @_;
8e64d0fa 77
81bc0675 78 return !exists $foreign{ get_code_package($_[1]) };
3a63a2e7 79}
80
3e44140b 81sub add_method;
82
3a63a2e7 83sub has_method {
84 my($self, $method_name) = @_;
85
78dc5ed1 86 defined($method_name)
87 or $self->throw_error('You must define a method name');
88
81bc0675 89 return defined($self->{methods}{$method_name}) || do{
90 my $code = get_code_ref($self->{package}, $method_name);
91 $code && $self->_code_is_mine($code);
92 };
3a63a2e7 93}
94
81bc0675 95sub get_method_body {
71e7b544 96 my($self, $method_name) = @_;
97
98 defined($method_name)
99 or $self->throw_error('You must define a method name');
100
101 return $self->{methods}{$method_name} ||= do{
7d96ae4d 102 my $code = get_code_ref($self->{package}, $method_name);
81bc0675 103 $code && $self->_code_is_mine($code) ? $code : undef;
71e7b544 104 };
105}
106
8536d351 107sub get_method{
6cfa1e5e 108 my($self, $method_name) = @_;
109
81bc0675 110 if(my $code = $self->get_method_body($method_name)){
6cfa1e5e 111 my $method_metaclass = $self->method_metaclass;
112 load_class($method_metaclass);
113
9010458d 114 return $method_metaclass->wrap(
81bc0675 115 body => $code,
9010458d 116 name => $method_name,
60e446e8 117 package => $self->name,
9010458d 118 associated_metaclass => $self,
6cfa1e5e 119 );
120 }
121
122 return undef;
8536d351 123}
3a63a2e7 124
8e64d0fa 125sub get_method_list {
3a63a2e7 126 my($self) = @_;
8e64d0fa 127
128 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 129}
130
7a50b450 131{
132 my $ANON_SERIAL = 0;
7a50b450 133
134 my %IMMORTALS;
135
136 sub create {
7eb3a8d5 137 my($self, $package_name, %options) = @_;
7a50b450 138
7eb3a8d5 139 my $class = ref($self) || $self;
140 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 141
8f40866c 142 my $superclasses;
7a50b450 143 if(exists $options{superclasses}){
f48920c1 144 if(Mouse::Util::is_a_metarole($self)){
7a50b450 145 delete $options{superclasses};
146 }
8f40866c 147 else{
148 $superclasses = delete $options{superclasses};
149 (ref $superclasses eq 'ARRAY')
7eb3a8d5 150 || $self->throw_error("You must pass an ARRAY ref of superclasses");
8f40866c 151 }
7a50b450 152 }
153
8f40866c 154 my $attributes = delete $options{attributes};
155 if(defined $attributes){
156 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
7eb3a8d5 157 || $self->throw_error("You must pass an ARRAY ref of attributes");
8f40866c 158 }
159 my $methods = delete $options{methods};
160 if(defined $methods){
161 (ref $methods eq 'HASH')
7eb3a8d5 162 || $self->throw_error("You must pass a HASH ref of methods");
8f40866c 163 }
164 my $roles = delete $options{roles};
165 if(defined $roles){
166 (ref $roles eq 'ARRAY')
7eb3a8d5 167 || $self->throw_error("You must pass an ARRAY ref of roles");
8f40866c 168 }
7a50b450 169 my $mortal;
170 my $cache_key;
171
172 if(!defined $package_name){ # anonymous
173 $mortal = !$options{cache};
174
175 # anonymous but immortal
176 if(!$mortal){
8e64d0fa 177 # something like Super::Class|Super::Class::2=Role|Role::1
178 $cache_key = join '=' => (
8f40866c 179 join('|', @{$superclasses || []}),
180 join('|', sort @{$roles || []}),
7a50b450 181 );
182 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
183 }
8f40866c 184 $options{anon_serial_id} = ++$ANON_SERIAL;
185 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
7a50b450 186 }
187
188 # instantiate a module
189 {
190 no strict 'refs';
191 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
192 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
193 }
194
7eb3a8d5 195 my $meta = $self->initialize( $package_name, %options);
7a50b450 196
b397e301 197 Scalar::Util::weaken $METAS{$package_name}
7a50b450 198 if $mortal;
199
7eb3a8d5 200 $meta->add_method(meta => sub{
201 $self->initialize(ref($_[0]) || $_[0]);
7a50b450 202 });
203
8f40866c 204 $meta->superclasses(@{$superclasses})
205 if defined $superclasses;
7a50b450 206
207 # NOTE:
208 # process attributes first, so that they can
209 # install accessors, but locally defined methods
210 # can then overwrite them. It is maybe a little odd, but
211 # I think this should be the order of things.
212 if (defined $attributes) {
213 if(ref($attributes) eq 'ARRAY'){
8f40866c 214 # array of Mouse::Meta::Attribute
7a50b450 215 foreach my $attr (@{$attributes}) {
8f40866c 216 $meta->add_attribute($attr);
7a50b450 217 }
218 }
219 else{
8f40866c 220 # hash map of name and attribute spec pairs
7a50b450 221 while(my($name, $attr) = each %{$attributes}){
222 $meta->add_attribute($name => $attr);
223 }
224 }
225 }
8f40866c 226 if (defined $methods) {
227 while(my($method_name, $method_body) = each %{$methods}){
228 $meta->add_method($method_name, $method_body);
7a50b450 229 }
230 }
8f40866c 231 if (defined $roles){
232 Mouse::Util::apply_all_roles($package_name, @{$roles});
7a50b450 233 }
234
8f40866c 235 if($cache_key){
7a50b450 236 $IMMORTALS{$cache_key} = $meta;
237 }
238
239 return $meta;
240 }
241
242 sub DESTROY{
243 my($self) = @_;
244
245 my $serial_id = $self->{anon_serial_id};
246
247 return if !$serial_id;
248
e75f21db 249 # @ISA is a magical variable, so we clear it manually.
250 @{$self->{superclasses}} = () if exists $self->{superclasses};
251
252 # Then, clear the symbol table hash
253 %{$self->namespace} = ();
7a50b450 254
8f40866c 255 my $name = $self->name;
256 delete $METAS{$name};
257
e75f21db 258 $name =~ s/ $serial_id \z//xms;
7a50b450 259
260 no strict 'refs';
8f40866c 261 delete ${$name}{ $serial_id . '::' };
7a50b450 262
263 return;
264 }
265}
266
8536d351 267sub throw_error{
268 my($class, $message, %args) = @_;
269
fce211ae 270 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
8e64d0fa 271 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
3a63a2e7 272
8536d351 273 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
274 Carp::croak($message);
275 }
276 else{
277 Carp::confess($message);
278 }
279}
3a63a2e7 280
2811;
282
283__END__
284
285=head1 NAME
286
1820fffe 287Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 288
a25ca8d6 289=head1 VERSION
290
6a97bbda 291This document describes Mouse version 0.40_04
a25ca8d6 292
b995be6a 293=head1 SEE ALSO
294
31c5194b 295L<Class::MOP::Class>
296
297L<Class::MOP::Module>
298
299L<Class::MOP::Package>
b995be6a 300
3a63a2e7 301=cut
2cea7a5f 302