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;
84 if (@_ == 1 && blessed($_[0])) {
86 $self->{'attributes'}{$attr->name} = $attr;
90 $names = [$names] if !ref($names);
91 my $metaclass = 'Mouse::Meta::Attribute';
92 my %options = (@_ == 1 ? %{$_[0]} : @_);
94 if ( my $metaclass_name = delete $options{metaclass} ) {
95 my $new_class = Mouse::Util::resolve_metaclass_alias(
99 if ( $metaclass ne $new_class ) {
100 $metaclass = $new_class;
104 for my $name (@$names) {
105 if ($name =~ s/^\+//) {
106 $metaclass->clone_parent($self, $name, %options);
109 $metaclass->create($self, $name, %options);
115 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
116 sub get_all_attributes {
120 for my $class ($self->linearized_isa) {
121 my $meta = $self->_metaclass_cache($class)
124 for my $name (keys %{ $meta->get_attribute_map }) {
125 next if $seen{$name}++;
126 push @attr, $meta->get_attribute($name);
133 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
137 my %args = (@_ == 1 ? %{$_[0]} : @_);
139 my $instance = bless {}, $self->name;
143 foreach my $attribute ($self->get_all_attributes) {
144 my $from = $attribute->init_arg;
145 my $key = $attribute->name;
147 if (defined($from) && exists($args{$from})) {
148 $args{$from} = $attribute->coerce_constraint($args{$from})
149 if $attribute->should_coerce;
150 $attribute->verify_against_type_constraint($args{$from});
152 $instance->{$key} = $args{$from};
154 weaken($instance->{$key})
155 if ref($instance->{$key}) && $attribute->is_weak_ref;
157 if ($attribute->has_trigger) {
158 push @triggers_queue, [ $attribute->trigger, $args{$from} ];
162 if ($attribute->has_default || $attribute->has_builder) {
163 unless ($attribute->is_lazy) {
164 my $default = $attribute->default;
165 my $builder = $attribute->builder;
166 my $value = $attribute->has_builder
167 ? $instance->$builder
168 : ref($default) eq 'CODE'
169 ? $default->($instance)
172 $value = $attribute->coerce_constraint($value)
173 if $attribute->should_coerce;
174 $attribute->verify_against_type_constraint($value);
176 $instance->{$key} = $value;
178 weaken($instance->{$key})
179 if ref($instance->{$key}) && $attribute->is_weak_ref;
183 if ($attribute->is_required) {
184 $self->throw_error("Attribute (".$attribute->name.") is required");
190 foreach my $trigger_and_value(@triggers_queue){
191 my($trigger, $value) = @{$trigger_and_value};
192 $trigger->($instance, $value);
195 if($self->is_anon_class){
196 $instance->{__METACLASS__} = $self;
204 my $instance = shift;
206 (blessed($instance) && $instance->isa($class->name))
207 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
209 $class->clone_instance($instance, @_);
213 my ($class, $instance, %params) = @_;
216 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
218 my $clone = bless { %$instance }, ref $instance;
220 foreach my $attr ($class->get_all_attributes()) {
221 if ( defined( my $init_arg = $attr->init_arg ) ) {
222 if (exists $params{$init_arg}) {
223 $clone->{ $attr->name } = $params{$init_arg};
235 inline_constructor => 1,
236 inline_destructor => 1,
240 $self->{is_immutable}++;
242 if ($args{inline_constructor}) {
243 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
246 if ($args{inline_destructor}) {
247 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
250 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
251 # at the end of a source file.
255 sub make_mutable { not_supported }
257 sub is_immutable { $_[0]->{is_immutable} }
258 sub is_mutable { !$_[0]->{is_immutable} }
260 sub _install_modifier {
261 my ( $self, $into, $type, $name, $code ) = @_;
263 # which is modifer class available?
264 my $modifier_class = do {
265 if (eval "require Class::Method::Modifiers::Fast; 1") {
266 'Class::Method::Modifiers::Fast';
267 } elsif (eval "require Class::Method::Modifiers; 1") {
268 'Class::Method::Modifiers';
270 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.");
273 my $modifier = $modifier_class->can('_install_modifier');
275 # replace this method itself :)
277 no warnings 'redefine';
278 *_install_modifier = sub {
279 my ( $self, $into, $type, $name, $code ) = @_;
286 $self->{methods}{$name}++; # register it to the method map
291 # call me. for first time.
292 $self->_install_modifier( $into, $type, $name, $code );
295 sub add_before_method_modifier {
296 my ( $self, $name, $code ) = @_;
297 $self->_install_modifier( $self->name, 'before', $name, $code );
300 sub add_around_method_modifier {
301 my ( $self, $name, $code ) = @_;
302 $self->_install_modifier( $self->name, 'around', $name, $code );
305 sub add_after_method_modifier {
306 my ( $self, $name, $code ) = @_;
307 $self->_install_modifier( $self->name, 'after', $name, $code );
310 sub add_override_method_modifier {
311 my ($self, $name, $code) = @_;
313 my $package = $self->name;
315 my $body = $package->can($name)
316 or $self->throw_error("You cannot override '$name' because it has no super method");
318 $self->add_method($name => sub { $code->($package, $body, @_) });
322 my ($self, $role_name) = @_;
325 || $self->throw_error("You must supply a role name to look for");
327 for my $class ($self->linearized_isa) {
328 my $meta = Mouse::Meta::Module::class_of($class);
329 next unless $meta && $meta->can('roles');
331 for my $role (@{ $meta->roles }) {
333 return 1 if $role->does_role($role_name);
346 Mouse::Meta::Class - hook into the Mouse MOP
350 =head2 initialize ClassName -> Mouse::Meta::Class
352 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
353 one instance should exist for a given class.
355 =head2 new %args -> Mouse::Meta::Class
357 Creates a new Mouse::Meta::Class. Don't call this directly.
359 =head2 name -> ClassName
361 Returns the name of the owner class.
363 =head2 superclasses -> [ClassName]
365 Gets (or sets) the list of superclasses of the owner class.
367 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
369 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
372 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
374 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
375 this class and its superclasses.
377 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
379 Returns a mapping of attribute names to their corresponding
380 L<Mouse::Meta::Attribute> objects.
382 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
384 This returns a list of attribute names which are defined in the local
385 class. If you want a list of all applicable attributes for a class,
386 use the C<get_all_attributes> method.
388 =head2 has_attribute Name -> Bool
390 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
392 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
394 Returns the L<Mouse::Meta::Attribute> with the given name.
396 =head2 linearized_isa -> [ClassNames]
398 Returns the list of classes in method dispatch order, with duplicates removed.
400 =head2 clone_object Instance -> Instance
402 Clones the given C<Instance> which must be an instance governed by this
405 =head2 clone_instance Instance, Parameters -> Instance
407 The clone_instance method has been made private.
408 The public version is deprecated.