1 package Mouse::Meta::Class;
5 use Mouse::Meta::Method::Constructor;
6 use Mouse::Meta::Method::Destructor;
7 use Scalar::Util qw/blessed weaken/;
8 use Mouse::Util qw/get_linear_isa not_supported/;
10 use base qw(Mouse::Meta::Module);
12 sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method()
15 my($class, %args) = @_;
17 $args{attributes} ||= {};
18 $args{methods} ||= {};
21 $args{superclasses} = do {
23 \@{ $args{package} . '::ISA' };
26 #return Mouse::Meta::Class->initialize($class)->new_object(%args)
27 # if $class ne __PACKAGE__;
29 return bless \%args, $class;
32 sub create_anon_class{
34 return $self->create(undef, @_);
38 return exists $_[0]->{anon_serial_id};
41 sub roles { $_[0]->{roles} }
47 Mouse::load_class($_) for @_;
48 @{ $self->{superclasses} } = @_;
51 @{ $self->{superclasses} };
54 sub get_all_method_names {
57 return grep { $uniq{$_}++ == 0 }
58 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
59 $self->linearized_isa;
65 if (@_ == 1 && blessed($_[0])) {
67 $self->{'attributes'}{$attr->name} = $attr;
71 $names = [$names] if !ref($names);
72 my $metaclass = 'Mouse::Meta::Attribute';
73 my %options = (@_ == 1 ? %{$_[0]} : @_);
75 if ( my $metaclass_name = delete $options{metaclass} ) {
76 my $new_class = Mouse::Util::resolve_metaclass_alias(
80 if ( $metaclass ne $new_class ) {
81 $metaclass = $new_class;
85 for my $name (@$names) {
86 if ($name =~ s/^\+//) {
87 $metaclass->clone_parent($self, $name, %options);
90 $metaclass->create($self, $name, %options);
96 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
97 sub get_all_attributes {
101 for my $class ($self->linearized_isa) {
102 my $meta = $self->_metaclass_cache($class)
105 for my $name (keys %{ $meta->get_attribute_map }) {
106 next if $seen{$name}++;
107 push @attr, $meta->get_attribute($name);
114 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
118 my %args = (@_ == 1 ? %{$_[0]} : @_);
120 my $instance = bless {}, $self->name;
124 foreach my $attribute ($self->get_all_attributes) {
125 my $from = $attribute->init_arg;
126 my $key = $attribute->name;
128 if (defined($from) && exists($args{$from})) {
129 $args{$from} = $attribute->coerce_constraint($args{$from})
130 if $attribute->should_coerce;
131 $attribute->verify_against_type_constraint($args{$from});
133 $instance->{$key} = $args{$from};
135 weaken($instance->{$key})
136 if ref($instance->{$key}) && $attribute->is_weak_ref;
138 if ($attribute->has_trigger) {
139 push @triggers_queue, [ $attribute->trigger, $args{$from} ];
143 if ($attribute->has_default || $attribute->has_builder) {
144 unless ($attribute->is_lazy) {
145 my $default = $attribute->default;
146 my $builder = $attribute->builder;
147 my $value = $attribute->has_builder
148 ? $instance->$builder
149 : ref($default) eq 'CODE'
150 ? $default->($instance)
153 $value = $attribute->coerce_constraint($value)
154 if $attribute->should_coerce;
155 $attribute->verify_against_type_constraint($value);
157 $instance->{$key} = $value;
159 weaken($instance->{$key})
160 if ref($instance->{$key}) && $attribute->is_weak_ref;
164 if ($attribute->is_required) {
165 $self->throw_error("Attribute (".$attribute->name.") is required");
171 foreach my $trigger_and_value(@triggers_queue){
172 my($trigger, $value) = @{$trigger_and_value};
173 $trigger->($instance, $value);
181 my $instance = shift;
183 (blessed($instance) && $instance->isa($class->name))
184 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
186 $class->clone_instance($instance, @_);
190 my ($class, $instance, %params) = @_;
193 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
195 my $clone = bless { %$instance }, ref $instance;
197 foreach my $attr ($class->get_all_attributes()) {
198 if ( defined( my $init_arg = $attr->init_arg ) ) {
199 if (exists $params{$init_arg}) {
200 $clone->{ $attr->name } = $params{$init_arg};
212 inline_constructor => 1,
213 inline_destructor => 1,
217 $self->{is_immutable}++;
219 if ($args{inline_constructor}) {
220 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
223 if ($args{inline_destructor}) {
224 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
227 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
228 # at the end of a source file.
232 sub make_mutable { not_supported }
234 sub is_immutable { $_[0]->{is_immutable} }
235 sub is_mutable { !$_[0]->{is_immutable} }
237 sub _install_modifier {
238 my ( $self, $into, $type, $name, $code ) = @_;
240 # which is modifer class available?
241 my $modifier_class = do {
242 if (eval "require Class::Method::Modifiers::Fast; 1") {
243 'Class::Method::Modifiers::Fast';
244 } elsif (eval "require Class::Method::Modifiers; 1") {
245 'Class::Method::Modifiers';
247 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.");
250 my $modifier = $modifier_class->can('_install_modifier');
252 # replace this method itself :)
254 no warnings 'redefine';
255 *_install_modifier = sub {
256 my ( $self, $into, $type, $name, $code ) = @_;
263 $self->{methods}{$name}++; # register it to the method map
268 # call me. for first time.
269 $self->_install_modifier( $into, $type, $name, $code );
272 sub add_before_method_modifier {
273 my ( $self, $name, $code ) = @_;
274 $self->_install_modifier( $self->name, 'before', $name, $code );
277 sub add_around_method_modifier {
278 my ( $self, $name, $code ) = @_;
279 $self->_install_modifier( $self->name, 'around', $name, $code );
282 sub add_after_method_modifier {
283 my ( $self, $name, $code ) = @_;
284 $self->_install_modifier( $self->name, 'after', $name, $code );
287 sub add_override_method_modifier {
288 my ($self, $name, $code) = @_;
290 my $package = $self->name;
292 my $body = $package->can($name)
293 or $self->throw_error("You cannot override '$name' because it has no super method");
295 $self->add_method($name => sub { $code->($package, $body, @_) });
299 my ($self, $role_name) = @_;
302 || $self->throw_error("You must supply a role name to look for");
304 for my $class ($self->linearized_isa) {
305 my $meta = Mouse::Meta::Module::class_of($class);
306 next unless $meta && $meta->can('roles');
308 for my $role (@{ $meta->roles }) {
310 return 1 if $role->does_role($role_name);
323 Mouse::Meta::Class - hook into the Mouse MOP
327 =head2 initialize ClassName -> Mouse::Meta::Class
329 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
330 one instance should exist for a given class.
332 =head2 new %args -> Mouse::Meta::Class
334 Creates a new Mouse::Meta::Class. Don't call this directly.
336 =head2 name -> ClassName
338 Returns the name of the owner class.
340 =head2 superclasses -> [ClassName]
342 Gets (or sets) the list of superclasses of the owner class.
344 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
346 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
349 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
351 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
352 this class and its superclasses.
354 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
356 Returns a mapping of attribute names to their corresponding
357 L<Mouse::Meta::Attribute> objects.
359 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
361 This returns a list of attribute names which are defined in the local
362 class. If you want a list of all applicable attributes for a class,
363 use the C<get_all_attributes> method.
365 =head2 has_attribute Name -> Bool
367 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
369 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
371 Returns the L<Mouse::Meta::Attribute> with the given name.
373 =head2 linearized_isa -> [ClassNames]
375 Returns the list of classes in method dispatch order, with duplicates removed.
377 =head2 clone_object Instance -> Instance
379 Clones the given C<Instance> which must be an instance governed by this
382 =head2 clone_instance Instance, Parameters -> Instance
384 The clone_instance method has been made private.
385 The public version is deprecated.