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;
234 (blessed($instance) && $instance->isa($class->name))
235 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
237 $class->clone_instance($instance, @_);
241 my ($class, $instance, %params) = @_;
244 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
246 my $clone = bless { %$instance }, ref $instance;
248 foreach my $attr ($class->get_all_attributes()) {
249 if ( defined( my $init_arg = $attr->init_arg ) ) {
250 if (exists $params{$init_arg}) {
251 $clone->{ $attr->name } = $params{$init_arg};
263 inline_constructor => 1,
264 inline_destructor => 1,
268 $self->{is_immutable}++;
270 if ($args{inline_constructor}) {
271 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
274 if ($args{inline_destructor}) {
275 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
278 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
279 # at the end of a source file.
283 sub make_mutable { not_supported }
285 sub is_immutable { $_[0]->{is_immutable} }
286 sub is_mutable { !$_[0]->{is_immutable} }
288 sub _install_modifier {
289 my ( $self, $into, $type, $name, $code ) = @_;
291 # which is modifer class available?
292 my $modifier_class = do {
293 if (eval "require Class::Method::Modifiers::Fast; 1") {
294 'Class::Method::Modifiers::Fast';
295 } elsif (eval "require Class::Method::Modifiers; 1") {
296 'Class::Method::Modifiers';
298 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.");
301 my $modifier = $modifier_class->can('_install_modifier');
303 # replace this method itself :)
305 no warnings 'redefine';
306 *_install_modifier = sub {
307 my ( $self, $into, $type, $name, $code ) = @_;
314 $self->{methods}{$name}++; # register it to the method map
319 # call me. for first time.
320 $self->_install_modifier( $into, $type, $name, $code );
323 sub add_before_method_modifier {
324 my ( $self, $name, $code ) = @_;
325 $self->_install_modifier( $self->name, 'before', $name, $code );
328 sub add_around_method_modifier {
329 my ( $self, $name, $code ) = @_;
330 $self->_install_modifier( $self->name, 'around', $name, $code );
333 sub add_after_method_modifier {
334 my ( $self, $name, $code ) = @_;
335 $self->_install_modifier( $self->name, 'after', $name, $code );
338 sub add_override_method_modifier {
339 my ($self, $name, $code) = @_;
341 my $package = $self->name;
343 my $body = $package->can($name)
344 or $self->throw_error("You cannot override '$name' because it has no super method");
346 $self->add_method($name => sub { $code->($package, $body, @_) });
350 my ($self, $role_name) = @_;
353 || $self->throw_error("You must supply a role name to look for");
355 for my $class ($self->linearized_isa) {
356 my $meta = Mouse::Meta::Module::class_of($class);
357 next unless $meta && $meta->can('roles');
359 for my $role (@{ $meta->roles }) {
361 return 1 if $role->does_role($role_name);
374 Mouse::Meta::Class - hook into the Mouse MOP
378 =head2 initialize ClassName -> Mouse::Meta::Class
380 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
381 one instance should exist for a given class.
383 =head2 new %args -> Mouse::Meta::Class
385 Creates a new Mouse::Meta::Class. Don't call this directly.
387 =head2 name -> ClassName
389 Returns the name of the owner class.
391 =head2 superclasses -> [ClassName]
393 Gets (or sets) the list of superclasses of the owner class.
395 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
397 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
400 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
402 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
403 this class and its superclasses.
405 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
407 Returns a mapping of attribute names to their corresponding
408 L<Mouse::Meta::Attribute> objects.
410 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
412 This returns a list of attribute names which are defined in the local
413 class. If you want a list of all applicable attributes for a class,
414 use the C<get_all_attributes> method.
416 =head2 has_attribute Name -> Bool
418 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
420 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
422 Returns the L<Mouse::Meta::Attribute> with the given name.
424 =head2 linearized_isa -> [ClassNames]
426 Returns the list of classes in method dispatch order, with duplicates removed.
428 =head2 clone_object Instance -> Instance
430 Clones the given C<Instance> which must be an instance governed by this
433 =head2 clone_instance Instance, Parameters -> Instance
435 The clone_instance method has been made private.
436 The public version is deprecated.