1 package Mouse::Meta::Class;
5 use Scalar::Util qw/blessed weaken/;
7 use Mouse::Util qw/get_linear_isa not_supported/;
9 use Mouse::Meta::Method::Constructor;
10 use Mouse::Meta::Method::Destructor;
11 use Mouse::Meta::Module;
13 use base qw(Mouse::Meta::Module);
15 sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method()
18 my($class, %args) = @_;
20 $args{attributes} ||= {};
21 $args{methods} ||= {};
24 $args{superclasses} = do {
26 \@{ $args{package} . '::ISA' };
29 #return Mouse::Meta::Class->initialize($class)->new_object(%args)
30 # if $class ne __PACKAGE__;
32 return bless \%args, ref($class) || $class;
35 sub create_anon_class{
37 return $self->create(undef, @_);
41 return exists $_[0]->{anon_serial_id};
44 sub roles { $_[0]->{roles} }
50 Mouse::load_class($_) for @_;
51 @{ $self->{superclasses} } = @_;
54 return @{ $self->{superclasses} };
57 sub find_method_by_name{
58 my($self, $method_name) = @_;
60 or $self->throw_error('You must define a method name to find');
61 foreach my $class( $self->linearized_isa ){
62 my $method = $self->initialize($class)->get_method($method_name);
63 return $method if defined $method;
70 return map{ $self->find_method_by_name($self) } $self->get_all_method_names;
73 sub get_all_method_names {
76 return grep { $uniq{$_}++ == 0 }
77 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
78 $self->linearized_isa;
89 $attr->isa('Mouse::Meta::Attribute')
90 || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)");
98 my %args = (@_ == 1) ? %{$_[0]} : @_;
101 or $self->throw_error('You must provide a name for the attribute');
103 if ($name =~ s/^\+//) { # inherited attributes
106 foreach my $class($self->linearized_isa){
107 my $meta = Mouse::Meta::Module::get_metaclass_by_name($class) or next;
108 $inherited_attr = $meta->get_attribute($name) and last;
111 defined($inherited_attr)
112 or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name);
114 $attr = $inherited_attr->clone_and_inherit_options($name, \%args);
117 my($attribute_class, @traits) = Mouse::Meta::Attribute->interpolate_class($name, \%args);
118 $args{traits} = \@traits if @traits;
120 $attr = $attribute_class->new($name, \%args);
124 weaken( $attr->{associated_class} = $self );
126 $self->{attributes}{$attr->name} = $attr;
127 $attr->install_accessors();
129 if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
130 Carp::cluck(qq{Attribute (}.$attr->name.qq{) of class }.$self->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
135 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
136 sub get_all_attributes {
140 for my $class ($self->linearized_isa) {
141 my $meta = $self->_metaclass_cache($class)
144 for my $name (keys %{ $meta->get_attribute_map }) {
145 next if $seen{$name}++;
146 push @attr, $meta->get_attribute($name);
153 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
157 my %args = (@_ == 1 ? %{$_[0]} : @_);
159 my $instance = bless {}, $self->name;
161 $self->_initialize_instance($instance, \%args);
165 sub _initialize_instance{
166 my($self, $instance, $args) = @_;
170 foreach my $attribute ($self->get_all_attributes) {
171 my $from = $attribute->init_arg;
172 my $key = $attribute->name;
174 if (defined($from) && exists($args->{$from})) {
175 $args->{$from} = $attribute->coerce_constraint($args->{$from})
176 if $attribute->should_coerce;
178 $attribute->verify_against_type_constraint($args->{$from});
180 $instance->{$key} = $args->{$from};
182 weaken($instance->{$key})
183 if ref($instance->{$key}) && $attribute->is_weak_ref;
185 if ($attribute->has_trigger) {
186 push @triggers_queue, [ $attribute->trigger, $args->{$from} ];
190 if ($attribute->has_default || $attribute->has_builder) {
191 unless ($attribute->is_lazy) {
192 my $default = $attribute->default;
193 my $builder = $attribute->builder;
194 my $value = $attribute->has_builder
195 ? $instance->$builder
196 : ref($default) eq 'CODE'
197 ? $default->($instance)
200 $value = $attribute->coerce_constraint($value)
201 if $attribute->should_coerce;
202 $attribute->verify_against_type_constraint($value);
204 $instance->{$key} = $value;
206 weaken($instance->{$key})
207 if ref($instance->{$key}) && $attribute->is_weak_ref;
211 if ($attribute->is_required) {
212 $self->throw_error("Attribute (".$attribute->name.") is required");
218 foreach my $trigger_and_value(@triggers_queue){
219 my($trigger, $value) = @{$trigger_and_value};
220 $trigger->($instance, $value);
223 if($self->is_anon_class){
224 $instance->{__METACLASS__} = $self;
232 my $instance = shift;
233 my %params = (@_ == 1) ? %{$_[0]} : @_;
235 (blessed($instance) && $instance->isa($class->name))
236 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
238 my $clone = bless { %$instance }, ref $instance;
240 foreach my $attr ($class->get_all_attributes()) {
241 if ( defined( my $init_arg = $attr->init_arg ) ) {
242 if (exists $params{$init_arg}) {
243 $clone->{ $attr->name } = $params{$init_arg};
252 my ($class, $instance, %params) = @_;
254 Carp::cluck('clone_instance has been deprecated. Use clone_object instead');
255 return $class->clone_object($instance, %params);
261 inline_constructor => 1,
262 inline_destructor => 1,
266 $self->{is_immutable}++;
268 if ($args{inline_constructor}) {
269 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
272 if ($args{inline_destructor}) {
273 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
276 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
277 # at the end of a source file.
281 sub make_mutable { not_supported }
283 sub is_immutable { $_[0]->{is_immutable} }
284 sub is_mutable { !$_[0]->{is_immutable} }
286 sub _install_modifier_pp{
287 my( $self, $into, $type, $name, $code ) = @_;
289 my $original = $into->can($name)
290 or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
292 my $modifier_table = $self->{modifiers}{$name};
294 if(!$modifier_table){
295 my(@before, @after, @around, $cache, $modified);
300 for my $c (@before) { $c->(@_) }
302 if(wantarray){ # list context
303 my @rval = $cache->(@_);
305 for my $c(@after){ $c->(@_) }
308 elsif(defined wantarray){ # scalar context
309 my $rval = $cache->(@_);
311 for my $c(@after){ $c->(@_) }
317 for my $c(@after){ $c->(@_) }
322 $self->{modifiers}{$name} = $modifier_table = {
323 original => $original,
329 cache => \$cache, # cache for around modifiers
332 $self->add_method($name => $modified);
335 if($type eq 'before'){
336 unshift @{$modifier_table->{before}}, $code;
338 elsif($type eq 'after'){
339 push @{$modifier_table->{after}}, $code;
342 push @{$modifier_table->{around}}, $code;
344 my $next = ${ $modifier_table->{cache} };
345 ${ $modifier_table->{cache} } = sub{ $code->($next, @_) };
351 sub _install_modifier {
352 my ( $self, $into, $type, $name, $code ) = @_;
354 # load Class::Method::Modifiers first
355 my $no_cmm_fast = do{
357 eval q{ require Class::Method::Modifiers::Fast };
363 $impl = \&_install_modifier_pp;
366 my $install_modifier = Class::Method::Modifiers::Fast->can('_install_modifier');
368 my ( $self, $into, $type, $name, $code ) = @_;
375 $self->{methods}{$name}++; # register it to the method map
380 # replace this method itself :)
382 no warnings 'redefine';
383 *_install_modifier = $impl;
386 $self->$impl( $into, $type, $name, $code );
389 sub add_before_method_modifier {
390 my ( $self, $name, $code ) = @_;
391 $self->_install_modifier( $self->name, 'before', $name, $code );
394 sub add_around_method_modifier {
395 my ( $self, $name, $code ) = @_;
396 $self->_install_modifier( $self->name, 'around', $name, $code );
399 sub add_after_method_modifier {
400 my ( $self, $name, $code ) = @_;
401 $self->_install_modifier( $self->name, 'after', $name, $code );
404 sub add_override_method_modifier {
405 my ($self, $name, $code) = @_;
407 my $package = $self->name;
409 my $body = $package->can($name)
410 or $self->throw_error("You cannot override '$name' because it has no super method");
412 $self->add_method($name => sub { $code->($package, $body, @_) });
416 my ($self, $role_name) = @_;
419 || $self->throw_error("You must supply a role name to look for");
421 for my $class ($self->linearized_isa) {
422 my $meta = Mouse::Meta::Module::class_of($class);
423 next unless $meta && $meta->can('roles');
425 for my $role (@{ $meta->roles }) {
427 return 1 if $role->does_role($role_name);
440 Mouse::Meta::Class - The Mouse class metaclass
444 =head2 initialize ClassName -> Mouse::Meta::Class
446 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
447 one instance should exist for a given class.
449 =head2 name -> ClassName
451 Returns the name of the owner class.
453 =head2 superclasses -> [ClassName]
455 Gets (or sets) the list of superclasses of the owner class.
457 =head2 add_attribute (name => spec | Mouse::Meta::Attribute)
459 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
462 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
464 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
465 this class and its superclasses.
467 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
469 Returns a mapping of attribute names to their corresponding
470 L<Mouse::Meta::Attribute> objects.
472 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
474 This returns a list of attribute names which are defined in the local
475 class. If you want a list of all applicable attributes for a class,
476 use the C<get_all_attributes> method.
478 =head2 has_attribute Name -> Bool
480 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
482 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
484 Returns the L<Mouse::Meta::Attribute> with the given name.
486 =head2 linearized_isa -> [ClassNames]
488 Returns the list of classes in method dispatch order, with duplicates removed.
490 =head2 new_object Parameters -> Instance
492 Create a new instance.
494 =head2 clone_object Instance -> Instance
496 Clones the given C<Instance> which must be an instance governed by this
501 L<Moose::Meta::Class>