2 package Moose::Meta::Class;
10 use Scalar::Util 'weaken', 'blessed';
12 our $VERSION = '0.56';
13 our $AUTHORITY = 'cpan:STEVAN';
15 use Moose::Meta::Method::Overriden;
16 use Moose::Meta::Method::Augmented;
18 use base 'Class::MOP::Class';
20 __PACKAGE__->meta->add_attribute('roles' => (
28 return Class::MOP::get_metaclass_by_name($pkg)
29 || $class->SUPER::initialize($pkg,
30 'attribute_metaclass' => 'Moose::Meta::Attribute',
31 'method_metaclass' => 'Moose::Meta::Method',
32 'instance_metaclass' => 'Moose::Meta::Instance',
38 my ($self, $package_name, %options) = @_;
40 (ref $options{roles} eq 'ARRAY')
41 || confess "You must pass an ARRAY ref of roles"
42 if exists $options{roles};
44 my $class = $self->SUPER::create($package_name, %options);
46 if (exists $options{roles}) {
47 Moose::Util::apply_all_roles($class, @{$options{roles}});
55 sub create_anon_class {
56 my ($self, %options) = @_;
58 my $cache_ok = delete $options{cache};
60 # something like Super::Class|Super::Class::2=Role|Role::1
61 my $cache_key = join '=' => (
62 join('|', sort @{$options{superclasses} || []}),
63 join('|', sort @{$options{roles} || []}),
66 if ($cache_ok && defined $ANON_CLASSES{$cache_key}) {
67 return $ANON_CLASSES{$cache_key};
70 my $new_class = $self->SUPER::create_anon_class(%options);
72 $ANON_CLASSES{$cache_key} = $new_class
79 my ($self, $role) = @_;
80 (blessed($role) && $role->isa('Moose::Meta::Role'))
81 || confess "Roles must be instances of Moose::Meta::Role";
82 push @{$self->roles} => $role;
85 sub calculate_all_roles {
88 grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
92 my ($self, $role_name) = @_;
94 || confess "You must supply a role name to look for";
95 foreach my $class ($self->class_precedence_list) {
96 next unless $class->can('meta') && $class->meta->can('roles');
97 foreach my $role (@{$class->meta->roles}) {
98 return 1 if $role->does_role($role_name);
105 my ($self, $role_name) = @_;
107 || confess "You must supply a role name to look for";
108 foreach my $class ($self->class_precedence_list) {
109 next unless $class->can('meta');
111 # in the pretty rare instance when a Moose metaclass
112 # is itself extended with a role, this check needs to
113 # be done since some items in the class_precedence_list
114 # might in fact be Class::MOP based still.
115 next unless $class->meta->can('roles');
116 foreach my $role (@{$class->meta->roles}) {
117 return 1 if $role->excludes_role($role_name);
124 my ($class, %params) = @_;
125 my $self = $class->SUPER::new_object(%params);
126 foreach my $attr ($class->compute_all_applicable_attributes()) {
127 # if we have a trigger, then ...
128 if ($attr->can('has_trigger') && $attr->has_trigger) {
129 # make sure we have an init-arg ...
130 if (defined(my $init_arg = $attr->init_arg)) {
131 # now make sure an init-arg was passes ...
132 if (exists $params{$init_arg}) {
133 # and if get here, fire the trigger
136 # check if there is a coercion
137 ($attr->should_coerce
138 # and if so, we need to grab the
139 # value that is actually been stored
140 ? $attr->get_read_method_ref->($self)
141 # otherwise, just get the value from
142 # the constructor params
143 : $params{$init_arg}),
153 sub construct_instance {
154 my ($class, %params) = @_;
155 my $meta_instance = $class->get_meta_instance;
157 # the code below is almost certainly incorrect
158 # but this is foreign inheritence, so we might
159 # have to kludge it in the end.
160 my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
161 foreach my $attr ($class->compute_all_applicable_attributes()) {
162 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
172 my $current = Class::MOP::check_package_cache_flag($self->name);
174 if (defined $self->{'_package_cache_flag'} && $self->{'_package_cache_flag'} == $current) {
175 return $self->{'methods'};
178 $self->{_package_cache_flag} = $current;
180 my $map = $self->{'methods'};
182 my $class_name = $self->name;
183 my $method_metaclass = $self->method_metaclass;
185 my %all_code = $self->get_all_package_symbols('CODE');
187 foreach my $symbol (keys %all_code) {
188 my $code = $all_code{$symbol};
190 next if exists $map->{$symbol} &&
191 defined $map->{$symbol} &&
192 $map->{$symbol}->body == $code;
194 my ($pkg, $name) = Class::MOP::get_code_info($code);
196 if ($pkg->can('meta')
198 # we don't know what ->meta we are calling
199 # here, so we need to be careful cause it
200 # just might blow up at us, or just complain
201 # loudly (in the case of Curses.pm) so we
202 # just be a little overly cautious here.
204 && eval { no warnings; blessed($pkg->meta) }
205 && $pkg->meta->isa('Moose::Meta::Role')) {
206 #my $role = $pkg->meta->name;
207 #next unless $self->does_role($role);
212 # in 5.10 constant.pm the constants show up
213 # as being in the right package, but in pre-5.10
214 # they show up as constant::__ANON__ so we
215 # make an exception here to be sure that things
216 # work as expected in both.
218 unless ($pkg eq 'constant' && $name eq '__ANON__') {
219 next if ($pkg || '') ne $class_name ||
220 (($name || '') ne '__ANON__' && ($pkg || '') ne $class_name);
225 $map->{$symbol} = $method_metaclass->wrap(
227 package_name => $class_name,
235 ### ---------------------------------------------
239 $self->SUPER::add_attribute(
240 (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
242 : $self->_process_attribute(@_))
246 sub add_override_method_modifier {
247 my ($self, $name, $method, $_super_package) = @_;
249 (!$self->has_method($name))
250 || confess "Cannot add an override method if a local method is already present";
252 $self->add_method($name => Moose::Meta::Method::Overriden->new(
255 package => $_super_package, # need this for roles
260 sub add_augment_method_modifier {
261 my ($self, $name, $method) = @_;
262 (!$self->has_method($name))
263 || confess "Cannot add an augment method if a local method is already present";
265 $self->add_method($name => Moose::Meta::Method::Augmented->new(
272 ## Private Utility methods ...
274 sub _find_next_method_by_name_which_is_not_overridden {
275 my ($self, $name) = @_;
276 foreach my $method ($self->find_all_methods_by_name($name)) {
277 return $method->{code}
278 if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
283 sub _fix_metaclass_incompatability {
284 my ($self, @superclasses) = @_;
285 foreach my $super (@superclasses) {
286 # don't bother if it does not have a meta.
287 next unless $super->can('meta');
288 next unless $super->meta->isa("Class::MOP::Class");
289 # get the name, make sure we take
290 # immutable classes into account
291 my $super_meta_name = ($super->meta->is_immutable
292 ? $super->meta->get_mutable_metaclass_name
293 : blessed($super->meta));
294 # if it's meta is a vanilla Moose,
295 # then we can safely ignore it.
296 next if $super_meta_name eq 'Moose::Meta::Class';
297 # but if we have anything else,
298 # we need to check it out ...
299 unless (# see if of our metaclass is incompatible
300 ($self->isa($super_meta_name) &&
301 # and see if our instance metaclass is incompatible
302 $self->instance_metaclass->isa($super->meta->instance_metaclass)) &&
303 # ... and if we are just a vanilla Moose
304 $self->isa('Moose::Meta::Class')) {
305 # re-initialize the meta ...
306 my $super_meta = $super->meta;
308 # We might want to consider actually
309 # transfering any attributes from the
310 # original meta into this one, but in
311 # general you should not have any there
312 # at this point anyway, so it's very
313 # much an obscure edge case anyway
314 $self = $super_meta->reinitialize($self->name => (
315 'attribute_metaclass' => $super_meta->attribute_metaclass,
316 'method_metaclass' => $super_meta->method_metaclass,
317 'instance_metaclass' => $super_meta->instance_metaclass,
325 # this was crap anyway, see
326 # Moose::Util::apply_all_roles
328 sub _apply_all_roles {
329 Carp::croak 'DEPRECATED: use Moose::Util::apply_all_roles($meta, @roles) instead'
332 sub _process_attribute {
333 my ( $self, $name, @args ) = @_;
335 @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH';
337 if ($name =~ /^\+(.*)/) {
338 return $self->_process_inherited_attribute($1, @args);
341 return $self->_process_new_attribute($name, @args);
345 sub _process_new_attribute {
346 my ( $self, $name, @args ) = @_;
348 $self->attribute_metaclass->interpolate_class_and_new($name, @args);
351 sub _process_inherited_attribute {
352 my ($self, $attr_name, %options) = @_;
353 my $inherited_attr = $self->find_attribute_by_name($attr_name);
354 (defined $inherited_attr)
355 || confess "Could not find an attribute by the name of '$attr_name' to inherit from";
356 if ($inherited_attr->isa('Moose::Meta::Attribute')) {
357 return $inherited_attr->clone_and_inherit_options(%options);
361 # kind of a kludge to handle Class::MOP::Attributes
362 return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options);
366 ## -------------------------------------------------
368 use Moose::Meta::Method::Constructor;
369 use Moose::Meta::Method::Destructor;
371 # This could be done by using SUPER and altering ->options
372 # I am keeping it this way to make it more explicit.
373 sub create_immutable_transformer {
375 my $class = Class::MOP::Immutable->new($self, {
376 read_only => [qw/superclasses/],
383 remove_package_symbol
387 class_precedence_list => 'ARRAY',
388 compute_all_applicable_attributes => 'ARRAY',
389 get_meta_instance => 'SCALAR',
390 get_method_map => 'SCALAR',
392 calculate_all_roles => 'ARRAY',
395 # this is ugly, but so are typeglobs,
396 # so whattayahgonnadoboutit
399 add_package_symbol => sub {
400 my $original = shift;
401 confess "Cannot add package symbols to an immutable metaclass"
402 unless (caller(2))[3] eq 'Class::MOP::Package::get_package_symbol';
403 goto $original->body;
412 $self->SUPER::make_immutable
414 constructor_class => 'Moose::Meta::Method::Constructor',
415 destructor_class => 'Moose::Meta::Method::Destructor',
416 inline_destructor => 1,
418 # no need to do this,
419 # Moose always does it
420 inline_accessors => 0,
433 Moose::Meta::Class - The Moose metaclass
437 This is a subclass of L<Class::MOP::Class> with Moose specific
440 For the most part, the only time you will ever encounter an
441 instance of this class is if you are doing some serious deep
442 introspection. To really understand this class, you need to refer
443 to the L<Class::MOP::Class> documentation.
453 Overrides original to accept a list of roles to apply to
456 my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] );
458 =item B<create_anon_class>
460 Overrides original to support roles and caching.
462 my $metaclass = Moose::Meta::Class->create_anon_class(
463 superclasses => ['Foo'],
464 roles => [qw/Some Roles Go Here/],
468 =item B<make_immutable>
470 Override original to add default options for inlining destructor
471 and altering the Constructor metaclass.
473 =item B<create_immutable_transformer>
475 Override original to lock C<add_role> and memoize C<calculate_all_roles>
479 We override this method to support the C<trigger> attribute option.
481 =item B<construct_instance>
483 This provides some Moose specific extensions to this method, you
484 almost never call this method directly unless you really know what
487 This method makes sure to handle the moose weak-ref, type-constraint
488 and type coercion features.
490 =item B<get_method_map>
492 This accommodates Moose::Meta::Role::Method instances, which are
493 aliased, instead of added, but still need to be counted as valid
496 =item B<add_override_method_modifier ($name, $method)>
498 This will create an C<override> method modifier for you, and install
501 =item B<add_augment_method_modifier ($name, $method)>
503 This will create an C<augment> method modifier for you, and install
506 =item B<calculate_all_roles>
510 This will return an array of C<Moose::Meta::Role> instances which are
511 attached to this class.
513 =item B<add_role ($role)>
515 This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it
516 to the list of associated roles.
518 =item B<does_role ($role_name)>
520 This will test if this class C<does> a given C<$role_name>. It will
521 not only check it's local roles, but ask them as well in order to
522 cascade down the role hierarchy.
524 =item B<excludes_role ($role_name)>
526 This will test if this class C<excludes> a given C<$role_name>. It will
527 not only check it's local roles, but ask them as well in order to
528 cascade down the role hierarchy.
530 =item B<add_attribute ($attr_name, %params|$params)>
532 This method does the same thing as L<Class::MOP::Class::add_attribute>, but adds
533 support for taking the C<$params> as a HASH ref.
539 All complex software has bugs lurking in it, and this module is no
540 exception. If you find a bug please either email me, or add the bug
545 Stevan Little E<lt>stevan@iinteractive.comE<gt>
547 =head1 COPYRIGHT AND LICENSE
549 Copyright 2006-2008 by Infinity Interactive, Inc.
551 L<http://www.iinteractive.com>
553 This library is free software; you can redistribute it and/or modify
554 it under the same terms as Perl itself.