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