Whoops. This should have been included in the previous commit.
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
306290e8 1package Mouse::Meta::Class;
c3398f5b 2use strict;
3use warnings;
4
fc1d8369 5use Mouse::Meta::Method::Constructor;
bbf64e76 6use Mouse::Meta::Method::Destructor;
6c169c50 7use Scalar::Util qw/blessed/;
67199842 8use Mouse::Util qw/get_linear_isa version authority identifier/;
7a59f4e8 9use Carp 'confess';
72b88a88 10
c3398f5b 11do {
12 my %METACLASS_CACHE;
72b88a88 13
14 # because Mouse doesn't introspect existing classes, we're forced to
15 # only pay attention to other Mouse classes
16 sub _metaclass_cache {
17 my $class = shift;
18 my $name = shift;
19 return $METACLASS_CACHE{$name};
20 }
21
c3398f5b 22 sub initialize {
67199842 23 my $class = blessed($_[0]) || $_[0];
24 my $name = $_[1];
25
c3398f5b 26 $METACLASS_CACHE{$name} = $class->new(name => $name)
27 if !exists($METACLASS_CACHE{$name});
28 return $METACLASS_CACHE{$name};
29 }
30};
31
32sub new {
33 my $class = shift;
6cbacbf6 34 my %args = @_;
c3398f5b 35
36 $args{attributes} = {};
37 $args{superclasses} = do {
38 no strict 'refs';
39 \@{ $args{name} . '::ISA' };
40 };
47f36c05 41 $args{roles} ||= [];
c3398f5b 42
43 bless \%args, $class;
44}
45
46sub name { $_[0]->{name} }
47
48sub superclasses {
49 my $self = shift;
50
51 if (@_) {
52 Mouse::load_class($_) for @_;
53 @{ $self->{superclasses} } = @_;
54 }
55
56 @{ $self->{superclasses} };
57}
58
d16fe7d7 59sub add_method {
60 my $self = shift;
61 my $name = shift;
62 my $code = shift;
63
64 my $pkg = $self->name;
65
66 no strict 'refs';
47b5feb9 67 no warnings 'redefine';
8632b6fe 68 $self->{'methods'}->{$name}++; # Moose stores meta object here.
d16fe7d7 69 *{ $pkg . '::' . $name } = $code;
70}
71
616d0d64 72sub has_method {
73 my $self = shift;
74 my $name = shift;
75 $self->name->can($name);
76}
77
2e92bb89 78# copied from Class::Inspector
4724d42e 79my $get_methods_for_class = sub {
2e92bb89 80 my $self = shift;
4724d42e 81 my $name = shift;
2e92bb89 82
83 no strict 'refs';
84 # Get all the CODE symbol table entries
8632b6fe 85 my @functions =
67199842 86 grep !/^(?:has|with|around|before|after|augment|inner|blessed|extends|confess|override|super)$/,
2e92bb89 87 grep { defined &{"${name}::$_"} }
88 keys %{"${name}::"};
4724d42e 89 push @functions, keys %{$self->{'methods'}->{$name}} if $self;
2e92bb89 90 wantarray ? @functions : \@functions;
4724d42e 91};
92
93sub get_method_list {
94 my $self = shift;
95 $get_methods_for_class->($self, $self->name);
2e92bb89 96}
97
60cfc6ad 98sub get_all_method_names {
99 my $self = shift;
100 my %uniq;
101 return grep { $uniq{$_}++ == 0 }
102 map { $get_methods_for_class->(undef, $_) }
103 $self->linearized_isa;
104}
105
c3398f5b 106sub add_attribute {
107 my $self = shift;
c3398f5b 108
60f6eba9 109 if (@_ == 1 && blessed($_[0])) {
110 my $attr = shift @_;
111 $self->{'attributes'}{$attr->name} = $attr;
112 } else {
113 my $names = shift @_;
114 $names = [$names] if !ref($names);
115 my $metaclass = 'Mouse::Meta::Attribute';
116 my %options = @_;
117
118 if ( my $metaclass_name = delete $options{metaclass} ) {
119 my $new_class = Mouse::Util::resolve_metaclass_alias(
120 'Attribute',
121 $metaclass_name
122 );
123 if ( $metaclass ne $new_class ) {
124 $metaclass = $new_class;
125 }
126 }
127
128 for my $name (@$names) {
129 if ($name =~ s/^\+//) {
130 $metaclass->clone_parent($self, $name, @_);
131 }
132 else {
133 $metaclass->create($self, $name, @_);
134 }
135 }
136 }
c3398f5b 137}
138
72b88a88 139sub compute_all_applicable_attributes {
140 my $self = shift;
141 my (@attr, %seen);
142
143 for my $class ($self->linearized_isa) {
144 my $meta = $self->_metaclass_cache($class)
145 or next;
146
147 for my $name (keys %{ $meta->get_attribute_map }) {
148 next if $seen{$name}++;
149 push @attr, $meta->get_attribute($name);
150 }
151 }
152
153 return @attr;
154}
155
c3398f5b 156sub get_attribute_map { $_[0]->{attributes} }
66eea168 157sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
c3398f5b 158sub get_attribute { $_[0]->{attributes}->{$_[1]} }
c68b4110 159sub get_attribute_list {
160 my $self = shift;
161 keys %{$self->get_attribute_map};
162}
c3398f5b 163
00ca1c62 164sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
c3398f5b 165
7a59f4e8 166sub clone_object {
167 my $class = shift;
168 my $instance = shift;
169
170 (blessed($instance) && $instance->isa($class->name))
1a0f0802 171 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
7a59f4e8 172
173 $class->clone_instance($instance, @_);
174}
175
176sub clone_instance {
177 my ($class, $instance, %params) = @_;
178
179 (blessed($instance))
e42bee44 180 || confess "You can only clone instances, ($instance) is not a blessed instance";
7a59f4e8 181
182 my $clone = bless { %$instance }, ref $instance;
183
184 foreach my $attr ($class->compute_all_applicable_attributes()) {
185 if ( defined( my $init_arg = $attr->init_arg ) ) {
186 if (exists $params{$init_arg}) {
187 $clone->{ $attr->name } = $params{$init_arg};
188 }
189 }
190 }
191
192 return $clone;
193
194}
195
fc1d8369 196sub make_immutable {
197 my $self = shift;
6a1d1835 198 my %args = (
199 inline_constructor => 1,
200 @_,
201 );
202
fc1d8369 203 my $name = $self->name;
204 $self->{is_immutable}++;
c7a6403f 205
6a1d1835 206 if ($args{inline_constructor}) {
c7a6403f 207 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
208 }
209
8632b6fe 210 if ($args{inline_destructor}) {
211 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
212 }
2276cb14 213
214 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
215 # at the end of a source file.
216 return 1;
fc1d8369 217}
ad958001 218
219sub make_mutable { confess "Mouse does not currently support 'make_mutable'" }
220
fc1d8369 221sub is_immutable { $_[0]->{is_immutable} }
84ef660f 222
223sub attribute_metaclass { "Mouse::Meta::Class" }
7a59f4e8 224
4859d490 225sub _install_modifier {
226 my ( $self, $into, $type, $name, $code ) = @_;
4f5b44a0 227
228 # which is modifer class available?
229 my $modifier_class = do {
230 if (eval "require Class::Method::Modifiers::Fast; 1") {
231 'Class::Method::Modifiers::Fast';
232 } elsif (eval "require Class::Method::Modifiers; 1") {
233 'Class::Method::Modifiers';
234 } else {
235 Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application.");
236 }
237 };
238 my $modifier = $modifier_class->can('_install_modifier');
239
240 # replace this method itself :)
241 {
242 no strict 'refs';
243 no warnings 'redefine';
244 *{__PACKAGE__ . '::_install_modifier'} = sub {
245 my ( $self, $into, $type, $name, $code ) = @_;
246 $modifier->(
247 $into,
248 $type,
249 $name,
250 $code
251 );
252 };
1b79a118 253 }
4f5b44a0 254
255 # call me. for first time.
256 $self->_install_modifier( $into, $type, $name, $code );
4859d490 257}
258
50dc6ee5 259sub add_before_method_modifier {
4859d490 260 my ( $self, $name, $code ) = @_;
261 $self->_install_modifier( $self->name, 'before', $name, $code );
50dc6ee5 262}
263
264sub add_around_method_modifier {
4859d490 265 my ( $self, $name, $code ) = @_;
266 $self->_install_modifier( $self->name, 'around', $name, $code );
50dc6ee5 267}
268
269sub add_after_method_modifier {
4859d490 270 my ( $self, $name, $code ) = @_;
271 $self->_install_modifier( $self->name, 'after', $name, $code );
50dc6ee5 272}
273
67199842 274sub add_override_method_modifier {
275 my ($self, $name, $code) = @_;
276
277 my $pkg = $self->name;
278 my $method = "${pkg}::${name}";
279
280 # Class::Method::Modifiers won't do this for us, so do it ourselves
281
282 my $body = $pkg->can($name)
283 or confess "You cannot override '$method' because it has no super method";
284
285 no strict 'refs';
286 *$method = sub { $code->($pkg, $body, @_) };
287}
288
289
47f36c05 290sub roles { $_[0]->{roles} }
291
292sub does_role {
293 my ($self, $role_name) = @_;
ad958001 294
47f36c05 295 (defined $role_name)
296 || confess "You must supply a role name to look for";
ad958001 297
f7fec86c 298 for my $class ($self->linearized_isa) {
299 next unless $class->can('meta') and $class->meta->can('roles');
300 for my $role (@{ $self->roles }) {
301 return 1 if $role->name eq $role_name;
302 }
47f36c05 303 }
ad958001 304
47f36c05 305 return 0;
306}
307
ae3edb8a 308sub create {
1a72f15c 309 my ($self, $package_name, %options) = @_;
ae3edb8a 310
311 (ref $options{superclasses} eq 'ARRAY')
312 || confess "You must pass an ARRAY ref of superclasses"
313 if exists $options{superclasses};
ad958001 314
ae3edb8a 315 (ref $options{attributes} eq 'ARRAY')
316 || confess "You must pass an ARRAY ref of attributes"
ad958001 317 if exists $options{attributes};
318
ae3edb8a 319 (ref $options{methods} eq 'HASH')
320 || confess "You must pass a HASH ref of methods"
ad958001 321 if exists $options{methods};
ae3edb8a 322
323 do {
ae3edb8a 324 ( defined $package_name && $package_name )
325 || confess "You must pass a package name";
326
327 my $code = "package $package_name;";
328 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
329 if exists $options{version};
330 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
331 if exists $options{authority};
332
333 eval $code;
334 confess "creation of $package_name failed : $@" if $@;
335 };
336
dd3c04d7 337 my %initialize_options = %options;
338 delete @initialize_options{qw(
ae3edb8a 339 package
340 superclasses
341 attributes
342 methods
343 version
344 authority
345 )};
dd3c04d7 346 my $meta = $self->initialize( $package_name => %initialize_options );
ae3edb8a 347
348 # FIXME totally lame
349 $meta->add_method('meta' => sub {
1a72f15c 350 $self->initialize(ref($_[0]) || $_[0]);
ae3edb8a 351 });
352
353 $meta->superclasses(@{$options{superclasses}})
354 if exists $options{superclasses};
355 # NOTE:
356 # process attributes first, so that they can
357 # install accessors, but locally defined methods
358 # can then overwrite them. It is maybe a little odd, but
359 # I think this should be the order of things.
360 if (exists $options{attributes}) {
361 foreach my $attr (@{$options{attributes}}) {
362 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
363 }
364 }
365 if (exists $options{methods}) {
366 foreach my $method_name (keys %{$options{methods}}) {
367 $meta->add_method($method_name, $options{methods}->{$method_name});
368 }
369 }
370 return $meta;
371}
372
373{
374 my $ANON_CLASS_SERIAL = 0;
375 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
376 sub create_anon_class {
377 my ( $class, %options ) = @_;
378 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
379 return $class->create( $package_name, %options );
380 }
381}
382
c3398f5b 3831;
384
385__END__
386
387=head1 NAME
388
306290e8 389Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 390
391=head1 METHODS
392
306290e8 393=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 394
306290e8 395Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
396one instance should exist for a given class.
c3398f5b 397
306290e8 398=head2 new %args -> Mouse::Meta::Class
c3398f5b 399
306290e8 400Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 401
402=head2 name -> ClassName
403
404Returns the name of the owner class.
405
406=head2 superclasses -> [ClassName]
407
408Gets (or sets) the list of superclasses of the owner class.
409
60f6eba9 410=head2 add_attribute (Mouse::Meta::Attribute| name => spec)
c3398f5b 411
306290e8 412Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
413class.
c3398f5b 414
72b88a88 415=head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
416
417Returns the list of all L<Mouse::Meta::Attribute> instances associated with
418this class and its superclasses.
419
306290e8 420=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 421
422Returns a mapping of attribute names to their corresponding
306290e8 423L<Mouse::Meta::Attribute> objects.
c3398f5b 424
c68b4110 425=head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
426
427This returns a list of attribute names which are defined in the local
428class. If you want a list of all applicable attributes for a class,
429use the C<compute_all_applicable_attributes> method.
430
cbc437f2 431=head2 has_attribute Name -> Bool
66eea168 432
433Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
434
306290e8 435=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 436
306290e8 437Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 438
439=head2 linearized_isa -> [ClassNames]
440
441Returns the list of classes in method dispatch order, with duplicates removed.
442
f7b11a21 443=head2 clone_object Instance -> Instance
444
445Clones the given C<Instance> which must be an instance governed by this
446metaclass.
447
448=head2 clone_instance Instance, Parameters -> Instance
449
450Clones the given C<Instance> and sets any additional parameters.
451
c3398f5b 452=cut
453