1 package Mouse::Meta::Class;
2 use Mouse::Util qw/:meta get_linear_isa not_supported/; # enables strict and warnings
4 use Scalar::Util qw/blessed weaken/;
6 use Mouse::Meta::Method::Constructor;
7 use Mouse::Meta::Method::Destructor;
8 use Mouse::Meta::Module;
9 our @ISA = qw(Mouse::Meta::Module);
11 sub method_metaclass() { 'Mouse::Meta::Method' }
12 sub attribute_metaclass() { 'Mouse::Meta::Attribute' }
14 sub constructor_class() { 'Mouse::Meta::Method::Constructor' }
15 sub destructor_class() { 'Mouse::Meta::Method::Destructor' }
18 my($class, %args) = @_;
20 $args{attributes} ||= {};
21 $args{methods} ||= {};
24 $args{superclasses} = do {
26 \@{ $args{package} . '::ISA' };
29 my $self = bless \%args, ref($class) || $class;
30 if(ref($self) ne __PACKAGE__){
31 $self->meta->_initialize_object($self, \%args);
36 sub create_anon_class{
38 return $self->create(undef, @_);
42 return exists $_[0]->{anon_serial_id};
45 sub roles { $_[0]->{roles} }
47 sub calculate_all_roles {
50 return grep { !$seen{ $_->name }++ }
51 map { $_->calculate_all_roles } @{ $self->roles };
58 foreach my $super(@_){
59 Mouse::Util::load_class($super);
60 my $meta = Mouse::Util::get_metaclass_by_name($super);
61 if($meta && $meta->isa('Mouse::Meta::Role')){
62 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
65 @{ $self->{superclasses} } = @_;
68 return @{ $self->{superclasses} };
71 sub find_method_by_name{
72 my($self, $method_name) = @_;
74 or $self->throw_error('You must define a method name to find');
76 foreach my $class( $self->linearized_isa ){
77 my $method = $self->initialize($class)->get_method($method_name);
78 return $method if defined $method;
85 return map{ $self->find_method_by_name($_) } $self->get_all_method_names;
88 sub get_all_method_names {
91 return grep { $uniq{$_}++ == 0 }
92 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
93 $self->linearized_isa;
96 sub find_attribute_by_name{
97 my($self, $name) = @_;
99 foreach my $class($self->linearized_isa){
100 my $meta = Mouse::Util::get_metaclass_by_name($class) or next;
101 $attr = $meta->get_attribute($name) and last;
114 $attr->isa('Mouse::Meta::Attribute')
115 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
123 my %args = (@_ == 1) ? %{$_[0]} : @_;
126 or $self->throw_error('You must provide a name for the attribute');
128 if ($name =~ s/^\+//) { # inherited attributes
129 my $inherited_attr = $self->find_attribute_by_name($name)
130 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
132 $attr = $inherited_attr->clone_and_inherit_options(%args);
135 my($attribute_class, @traits) = $self->attribute_metaclass->interpolate_class(\%args);
136 $args{traits} = \@traits if @traits;
138 $attr = $attribute_class->new($name, %args);
142 weaken( $attr->{associated_class} = $self );
144 $self->{attributes}{$attr->name} = $attr;
145 $attr->install_accessors();
147 if(_MOUSE_VERBOSE && !$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
148 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
153 sub compute_all_applicable_attributes {
154 Carp::cluck('compute_all_applicable_attributes() has been deprecated')
156 return shift->get_all_attributes(@_)
159 sub get_all_attributes {
163 for my $class ($self->linearized_isa) {
164 my $meta = Mouse::Util::get_metaclass_by_name($class)
167 for my $name ($meta->get_attribute_list) {
168 next if $seen{$name}++;
169 push @attr, $meta->get_attribute($name);
176 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
180 my %args = (@_ == 1 ? %{$_[0]} : @_);
182 my $object = bless {}, $self->name;
184 $self->_initialize_object($object, \%args);
188 sub _initialize_object{
189 my($self, $object, $args) = @_;
193 foreach my $attribute ($self->get_all_attributes) {
194 my $from = $attribute->init_arg;
195 my $key = $attribute->name;
197 if (defined($from) && exists($args->{$from})) {
198 $object->{$key} = $attribute->_coerce_and_verify($args->{$from}, $object);
200 weaken($object->{$key})
201 if ref($object->{$key}) && $attribute->is_weak_ref;
203 if ($attribute->has_trigger) {
204 push @triggers_queue, [ $attribute->trigger, $object->{$key} ];
208 if ($attribute->has_default || $attribute->has_builder) {
209 unless ($attribute->is_lazy) {
210 my $default = $attribute->default;
211 my $builder = $attribute->builder;
212 my $value = $builder ? $object->$builder()
213 : ref($default) eq 'CODE' ? $object->$default()
216 $object->{$key} = $attribute->_coerce_and_verify($value, $object);
218 weaken($object->{$key})
219 if ref($object->{$key}) && $attribute->is_weak_ref;
223 if ($attribute->is_required) {
224 $self->throw_error("Attribute (".$attribute->name.") is required");
230 foreach my $trigger_and_value(@triggers_queue){
231 my($trigger, $value) = @{$trigger_and_value};
232 $trigger->($object, $value);
235 if($self->is_anon_class){
236 $object->{__METACLASS__} = $self;
245 my %params = (@_ == 1) ? %{$_[0]} : @_;
247 (blessed($object) && $object->isa($class->name))
248 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
250 my $cloned = bless { %$object }, ref $object;
251 $class->_initialize_object($cloned, \%params);
257 my ($class, $instance, %params) = @_;
259 Carp::cluck('clone_instance has been deprecated. Use clone_object instead')
261 return $class->clone_object($instance, %params);
267 inline_constructor => 1,
268 inline_destructor => 1,
269 constructor_name => 'new',
273 $self->{is_immutable}++;
275 if ($args{inline_constructor}) {
276 $self->add_method($args{constructor_name} =>
277 $self->constructor_class->_generate_constructor($self, \%args));
280 if ($args{inline_destructor}) {
281 $self->add_method(DESTROY =>
282 $self->destructor_class->_generate_destructor($self, \%args));
285 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
286 # at the end of a source file.
290 sub make_mutable { not_supported }
292 sub is_immutable { $_[0]->{is_immutable} }
293 sub is_mutable { !$_[0]->{is_immutable} }
295 sub _install_modifier_pp{
296 my( $self, $type, $name, $code ) = @_;
297 my $into = $self->name;
299 my $original = $into->can($name)
300 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
302 my $modifier_table = $self->{modifiers}{$name};
304 if(!$modifier_table){
305 my(@before, @after, @around, $cache, $modified);
310 for my $c (@before) { $c->(@_) }
312 if(wantarray){ # list context
313 my @rval = $cache->(@_);
315 for my $c(@after){ $c->(@_) }
318 elsif(defined wantarray){ # scalar context
319 my $rval = $cache->(@_);
321 for my $c(@after){ $c->(@_) }
327 for my $c(@after){ $c->(@_) }
332 $self->{modifiers}{$name} = $modifier_table = {
333 original => $original,
339 cache => \$cache, # cache for around modifiers
342 $self->add_method($name => $modified);
345 if($type eq 'before'){
346 unshift @{$modifier_table->{before}}, $code;
348 elsif($type eq 'after'){
349 push @{$modifier_table->{after}}, $code;
352 push @{$modifier_table->{around}}, $code;
354 my $next = ${ $modifier_table->{cache} };
355 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
361 sub _install_modifier {
362 my ( $self, $type, $name, $code ) = @_;
364 # load Class::Method::Modifiers first
365 my $no_cmm_fast = do{
367 eval q{ require Class::Method::Modifiers::Fast };
373 $impl = \&_install_modifier_pp;
376 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
378 my ( $self, $type, $name, $code ) = @_;
379 my $into = $self->name;
380 $install_modifier->($into, $type, $name, $code);
382 $self->add_method($name => do{
384 \&{ $into . '::' . $name };
390 # replace this method itself :)
392 no warnings 'redefine';
393 *_install_modifier = $impl;
396 $self->$impl( $type, $name, $code );
399 sub add_before_method_modifier {
400 my ( $self, $name, $code ) = @_;
401 $self->_install_modifier( 'before', $name, $code );
404 sub add_around_method_modifier {
405 my ( $self, $name, $code ) = @_;
406 $self->_install_modifier( 'around', $name, $code );
409 sub add_after_method_modifier {
410 my ( $self, $name, $code ) = @_;
411 $self->_install_modifier( 'after', $name, $code );
414 sub add_override_method_modifier {
415 my ($self, $name, $code) = @_;
417 if($self->has_method($name)){
418 $self->throw_error("Cannot add an override method if a local method is already present");
421 my $package = $self->name;
423 my $super_body = $package->can($name)
424 or $self->throw_error("You cannot override '$name' because it has no super method");
426 $self->add_method($name => sub {
427 local $Mouse::SUPER_PACKAGE = $package;
428 local $Mouse::SUPER_BODY = $super_body;
429 local @Mouse::SUPER_ARGS = @_;
436 sub add_augment_method_modifier {
437 my ($self, $name, $code) = @_;
438 if($self->has_method($name)){
439 $self->throw_error("Cannot add an augment method if a local method is already present");
442 my $super = $self->find_method_by_name($name)
443 or $self->throw_error("You cannot augment '$name' because it has no super method");
445 my $super_package = $super->package_name;
446 my $super_body = $super->body;
448 $self->add_method($name => sub{
449 local $Mouse::INNER_BODY{$super_package} = $code;
450 local $Mouse::INNER_ARGS{$super_package} = [@_];
457 my ($self, $role_name) = @_;
460 || $self->throw_error("You must supply a role name to look for");
462 for my $class ($self->linearized_isa) {
463 my $meta = Mouse::Util::get_metaclass_by_name($class)
466 for my $role (@{ $meta->roles }) {
468 return 1 if $role->does_role($role_name);
480 Mouse::Meta::Class - The Mouse class metaclass
484 This document describes Mouse version 0.40
488 =head2 C<< initialize(ClassName) -> Mouse::Meta::Class >>
490 Finds or creates a C<Mouse::Meta::Class> instance for the given ClassName. Only
491 one instance should exist for a given class.
493 =head2 C<< name -> ClassName >>
495 Returns the name of the owner class.
497 =head2 C<< superclasses -> ClassNames >> C<< superclass(ClassNames) >>
499 Gets (or sets) the list of superclasses of the owner class.
501 =head2 C<< add_method(name => CodeRef) >>
503 Adds a method to the owner class.
505 =head2 C<< has_method(name) -> Bool >>
507 Returns whether we have a method with the given name.
509 =head2 C<< get_method(name) -> Mouse::Meta::Method | undef >>
511 Returns a L<Mouse::Meta::Method> with the given name.
513 Note that you can also use C<< $metaclass->name->can($name) >> for a method body.
515 =head2 C<< get_method_list -> Names >>
517 Returns a list of method names which are defined in the local class.
518 If you want a list of all applicable methods for a class, use the
519 C<get_all_methods> method.
521 =head2 C<< get_all_methods -> (Mouse::Meta::Method) >>
523 Return the list of all L<Mouse::Meta::Method> instances associated with
524 the class and its superclasses.
526 =head2 C<< add_attribute(name => spec | Mouse::Meta::Attribute) >>
528 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
531 =head2 C<< has_attribute(Name) -> Bool >>
533 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
535 =head2 C<< get_attribute Name -> Mouse::Meta::Attribute | undef >>
537 Returns the L<Mouse::Meta::Attribute> with the given name.
539 =head2 C<< get_attribute_list -> Names >>
541 Returns a list of attribute names which are defined in the local
542 class. If you want a list of all applicable attributes for a class,
543 use the C<get_all_attributes> method.
545 =head2 C<< get_all_attributes -> (Mouse::Meta::Attribute) >>
547 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
548 this class and its superclasses.
550 =head2 C<< linearized_isa -> [ClassNames] >>
552 Returns the list of classes in method dispatch order, with duplicates removed.
554 =head2 C<< new_object(Parameters) -> Instance >>
556 Creates a new instance.
558 =head2 C<< clone_object(Instance, Parameters) -> Instance >>
560 Clones the given instance which must be an instance governed by this
563 =head2 C<< throw_error(Message, Parameters) >>
565 Throws an error with the given message.
569 L<Moose::Meta::Class>