2 package Moose::Meta::Class;
10 use Scalar::Util 'weaken', 'blessed';
12 our $VERSION = '0.55_01';
13 $VERSION = eval $VERSION;
14 our $AUTHORITY = 'cpan:STEVAN';
16 use Moose::Meta::Method::Overriden;
17 use Moose::Meta::Method::Augmented;
19 use base 'Class::MOP::Class';
21 __PACKAGE__->meta->add_attribute('roles' => (
26 __PACKAGE__->meta->add_attribute('constructor_class' => (
27 accessor => 'constructor_class',
28 default => sub { 'Moose::Meta::Method::Constructor' }
31 __PACKAGE__->meta->add_attribute('destructor_class' => (
32 accessor => 'destructor_class',
33 default => sub { 'Moose::Meta::Method::Destructor' }
39 return Class::MOP::get_metaclass_by_name($pkg)
40 || $class->SUPER::initialize($pkg,
41 'attribute_metaclass' => 'Moose::Meta::Attribute',
42 'method_metaclass' => 'Moose::Meta::Method',
43 'instance_metaclass' => 'Moose::Meta::Instance',
49 my ($self, $package_name, %options) = @_;
51 (ref $options{roles} eq 'ARRAY')
52 || confess "You must pass an ARRAY ref of roles"
53 if exists $options{roles};
55 my $class = $self->SUPER::create($package_name, %options);
57 if (exists $options{roles}) {
58 Moose::Util::apply_all_roles($class, @{$options{roles}});
66 sub create_anon_class {
67 my ($self, %options) = @_;
69 my $cache_ok = delete $options{cache};
71 # something like Super::Class|Super::Class::2=Role|Role::1
72 my $cache_key = join '=' => (
73 join('|', sort @{$options{superclasses} || []}),
74 join('|', sort @{$options{roles} || []}),
77 if ($cache_ok && defined $ANON_CLASSES{$cache_key}) {
78 return $ANON_CLASSES{$cache_key};
81 my $new_class = $self->SUPER::create_anon_class(%options);
83 $ANON_CLASSES{$cache_key} = $new_class
90 my ($self, $role) = @_;
91 (blessed($role) && $role->isa('Moose::Meta::Role'))
92 || confess "Roles must be instances of Moose::Meta::Role";
93 push @{$self->roles} => $role;
96 sub calculate_all_roles {
99 grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
103 my ($self, $role_name) = @_;
105 || confess "You must supply a role name to look for";
106 foreach my $class ($self->class_precedence_list) {
107 next unless $class->can('meta') && $class->meta->can('roles');
108 foreach my $role (@{$class->meta->roles}) {
109 return 1 if $role->does_role($role_name);
116 my ($self, $role_name) = @_;
118 || confess "You must supply a role name to look for";
119 foreach my $class ($self->class_precedence_list) {
120 next unless $class->can('meta');
122 # in the pretty rare instance when a Moose metaclass
123 # is itself extended with a role, this check needs to
124 # be done since some items in the class_precedence_list
125 # might in fact be Class::MOP based still.
126 next unless $class->meta->can('roles');
127 foreach my $role (@{$class->meta->roles}) {
128 return 1 if $role->excludes_role($role_name);
136 my $params = @_ == 1 ? $_[0] : {@_};
137 my $self = $class->SUPER::new_object($params);
138 foreach my $attr ($class->compute_all_applicable_attributes()) {
139 # if we have a trigger, then ...
140 if ($attr->can('has_trigger') && $attr->has_trigger) {
141 # make sure we have an init-arg ...
142 if (defined(my $init_arg = $attr->init_arg)) {
143 # now make sure an init-arg was passes ...
144 if (exists $params->{$init_arg}) {
145 # and if get here, fire the trigger
148 # check if there is a coercion
149 ($attr->should_coerce
150 # and if so, we need to grab the
151 # value that is actually been stored
152 ? $attr->get_read_method_ref->($self)
153 # otherwise, just get the value from
154 # the constructor params
155 : $params->{$init_arg}),
165 sub construct_instance {
167 my $params = @_ == 1 ? $_[0] : {@_};
168 my $meta_instance = $class->get_meta_instance;
170 # the code below is almost certainly incorrect
171 # but this is foreign inheritence, so we might
172 # have to kludge it in the end.
173 my $instance = $params->{'__INSTANCE__'} || $meta_instance->create_instance();
174 foreach my $attr ($class->compute_all_applicable_attributes()) {
175 $attr->initialize_instance_slot($meta_instance, $instance, $params);
185 my $current = Class::MOP::check_package_cache_flag($self->name);
187 if (defined $self->{'_package_cache_flag'} && $self->{'_package_cache_flag'} == $current) {
188 return $self->{'methods'};
191 $self->{_package_cache_flag} = $current;
193 my $map = $self->{'methods'};
195 my $class_name = $self->name;
196 my $method_metaclass = $self->method_metaclass;
198 my %all_code = $self->get_all_package_symbols('CODE');
200 foreach my $symbol (keys %all_code) {
201 my $code = $all_code{$symbol};
203 next if exists $map->{$symbol} &&
204 defined $map->{$symbol} &&
205 $map->{$symbol}->body == $code;
207 my ($pkg, $name) = Class::MOP::get_code_info($code);
209 if ($pkg->can('meta')
211 # we don't know what ->meta we are calling
212 # here, so we need to be careful cause it
213 # just might blow up at us, or just complain
214 # loudly (in the case of Curses.pm) so we
215 # just be a little overly cautious here.
217 && eval { no warnings; blessed($pkg->meta) }
218 && $pkg->meta->isa('Moose::Meta::Role')) {
219 #my $role = $pkg->meta->name;
220 #next unless $self->does_role($role);
225 # in 5.10 constant.pm the constants show up
226 # as being in the right package, but in pre-5.10
227 # they show up as constant::__ANON__ so we
228 # make an exception here to be sure that things
229 # work as expected in both.
231 unless ($pkg eq 'constant' && $name eq '__ANON__') {
232 next if ($pkg || '') ne $class_name ||
233 (($name || '') ne '__ANON__' && ($pkg || '') ne $class_name);
238 $map->{$symbol} = $method_metaclass->wrap(
240 package_name => $class_name,
248 ### ---------------------------------------------
252 $self->SUPER::add_attribute(
253 (blessed $_[0] && $_[0]->isa('Class::MOP::Attribute')
255 : $self->_process_attribute(@_))
259 sub add_override_method_modifier {
260 my ($self, $name, $method, $_super_package) = @_;
262 (!$self->has_method($name))
263 || confess "Cannot add an override method if a local method is already present";
265 $self->add_method($name => Moose::Meta::Method::Overriden->new(
268 package => $_super_package, # need this for roles
273 sub add_augment_method_modifier {
274 my ($self, $name, $method) = @_;
275 (!$self->has_method($name))
276 || confess "Cannot add an augment method if a local method is already present";
278 $self->add_method($name => Moose::Meta::Method::Augmented->new(
285 ## Private Utility methods ...
287 sub _find_next_method_by_name_which_is_not_overridden {
288 my ($self, $name) = @_;
289 foreach my $method ($self->find_all_methods_by_name($name)) {
290 return $method->{code}
291 if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
296 sub _fix_metaclass_incompatability {
297 my ($self, @superclasses) = @_;
299 foreach my $super (@superclasses) {
300 # don't bother if it does not have a meta.
301 my $super_meta = Class::MOP::Class->initialize($super) or next;
302 next unless $super_meta->isa("Class::MOP::Class");
304 # get the name, make sure we take
305 # immutable classes into account
307 = $super_meta->is_immutable
308 ? $super_meta->get_mutable_metaclass_name
312 # if our metaclass is compatible
313 $self->isa($super_meta_name)
315 # and our instance metaclass is also compatible then no
317 $self->instance_metaclass->isa( $super_meta->instance_metaclass );
319 next unless $super_meta->isa( ref($self) );
321 unless ( $self->is_pristine ) {
322 confess "Not reinitializing metaclass for "
324 . ", it isn't pristine";
327 $self = $super_meta->reinitialize(
329 attribute_metaclass => $super_meta->attribute_metaclass,
330 method_metaclass => $super_meta->method_metaclass,
331 instance_metaclass => $super_meta->instance_metaclass,
339 # this was crap anyway, see
340 # Moose::Util::apply_all_roles
342 sub _apply_all_roles {
343 Carp::croak 'DEPRECATED: use Moose::Util::apply_all_roles($meta, @roles) instead'
346 sub _process_attribute {
347 my ( $self, $name, @args ) = @_;
349 @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH';
351 if ($name =~ /^\+(.*)/) {
352 return $self->_process_inherited_attribute($1, @args);
355 return $self->_process_new_attribute($name, @args);
359 sub _process_new_attribute {
360 my ( $self, $name, @args ) = @_;
362 $self->attribute_metaclass->interpolate_class_and_new($name, @args);
365 sub _process_inherited_attribute {
366 my ($self, $attr_name, %options) = @_;
367 my $inherited_attr = $self->find_attribute_by_name($attr_name);
368 (defined $inherited_attr)
369 || confess "Could not find an attribute by the name of '$attr_name' to inherit from";
370 if ($inherited_attr->isa('Moose::Meta::Attribute')) {
371 return $inherited_attr->clone_and_inherit_options(%options);
375 # kind of a kludge to handle Class::MOP::Attributes
376 return $inherited_attr->Moose::Meta::Attribute::clone_and_inherit_options(%options);
380 ## -------------------------------------------------
382 use Moose::Meta::Method::Constructor;
383 use Moose::Meta::Method::Destructor;
385 # This could be done by using SUPER and altering ->options
386 # I am keeping it this way to make it more explicit.
387 sub create_immutable_transformer {
389 my $class = Class::MOP::Immutable->new($self, {
390 read_only => [qw/superclasses/],
397 remove_package_symbol
401 class_precedence_list => 'ARRAY',
402 linearized_isa => 'ARRAY', # FIXME perl 5.10 memoizes this on its own, no need?
403 get_all_methods => 'ARRAY',
404 #get_all_attributes => 'ARRAY', # it's an alias, no need, but maybe in the future
405 compute_all_applicable_attributes => 'ARRAY',
406 get_meta_instance => 'SCALAR',
407 get_method_map => 'SCALAR',
408 calculate_all_roles => 'ARRAY',
411 # this is ugly, but so are typeglobs,
412 # so whattayahgonnadoboutit
415 add_package_symbol => sub {
416 my $original = shift;
417 confess "Cannot add package symbols to an immutable metaclass"
418 unless (caller(2))[3] eq 'Class::MOP::Package::get_package_symbol';
419 goto $original->body;
428 $self->SUPER::make_immutable
430 constructor_class => $self->constructor_class,
431 destructor_class => $self->destructor_class,
432 inline_destructor => 1,
434 # no need to do this,
435 # Moose always does it
436 inline_accessors => 0,
449 Moose::Meta::Class - The Moose metaclass
453 This is a subclass of L<Class::MOP::Class> with Moose specific
456 For the most part, the only time you will ever encounter an
457 instance of this class is if you are doing some serious deep
458 introspection. To really understand this class, you need to refer
459 to the L<Class::MOP::Class> documentation.
469 Overrides original to accept a list of roles to apply to
472 my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] );
474 =item B<create_anon_class>
476 Overrides original to support roles and caching.
478 my $metaclass = Moose::Meta::Class->create_anon_class(
479 superclasses => ['Foo'],
480 roles => [qw/Some Roles Go Here/],
484 =item B<make_immutable>
486 Override original to add default options for inlining destructor
487 and altering the Constructor metaclass.
489 =item B<create_immutable_transformer>
491 Override original to lock C<add_role> and memoize C<calculate_all_roles>
495 We override this method to support the C<trigger> attribute option.
497 =item B<construct_instance>
499 This provides some Moose specific extensions to this method, you
500 almost never call this method directly unless you really know what
503 This method makes sure to handle the moose weak-ref, type-constraint
504 and type coercion features.
506 =item B<get_method_map>
508 This accommodates Moose::Meta::Role::Method instances, which are
509 aliased, instead of added, but still need to be counted as valid
512 =item B<add_override_method_modifier ($name, $method)>
514 This will create an C<override> method modifier for you, and install
517 =item B<add_augment_method_modifier ($name, $method)>
519 This will create an C<augment> method modifier for you, and install
522 =item B<calculate_all_roles>
526 This will return an array of C<Moose::Meta::Role> instances which are
527 attached to this class.
529 =item B<add_role ($role)>
531 This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it
532 to the list of associated roles.
534 =item B<does_role ($role_name)>
536 This will test if this class C<does> a given C<$role_name>. It will
537 not only check it's local roles, but ask them as well in order to
538 cascade down the role hierarchy.
540 =item B<excludes_role ($role_name)>
542 This will test if this class C<excludes> a given C<$role_name>. It will
543 not only check it's local roles, but ask them as well in order to
544 cascade down the role hierarchy.
546 =item B<add_attribute ($attr_name, %params|$params)>
548 This method does the same thing as L<Class::MOP::Class::add_attribute>, but adds
549 support for taking the C<$params> as a HASH ref.
555 All complex software has bugs lurking in it, and this module is no
556 exception. If you find a bug please either email me, or add the bug
561 Stevan Little E<lt>stevan@iinteractive.comE<gt>
563 =head1 COPYRIGHT AND LICENSE
565 Copyright 2006-2008 by Infinity Interactive, Inc.
567 L<http://www.iinteractive.com>
569 This library is free software; you can redistribute it and/or modify
570 it under the same terms as Perl itself.