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