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