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, $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 @{ $self->{superclasses} };
57 sub get_all_method_names {
60 return grep { $uniq{$_}++ == 0 }
61 map { Mouse::Meta::Class->initialize($_)->get_method_list() }
62 $self->linearized_isa;
68 if (@_ == 1 && blessed($_[0])) {
70 $self->{'attributes'}{$attr->name} = $attr;
74 $names = [$names] if !ref($names);
75 my $metaclass = 'Mouse::Meta::Attribute';
76 my %options = (@_ == 1 ? %{$_[0]} : @_);
78 if ( my $metaclass_name = delete $options{metaclass} ) {
79 my $new_class = Mouse::Util::resolve_metaclass_alias(
83 if ( $metaclass ne $new_class ) {
84 $metaclass = $new_class;
88 for my $name (@$names) {
89 if ($name =~ s/^\+//) {
90 $metaclass->clone_parent($self, $name, %options);
93 $metaclass->create($self, $name, %options);
99 sub compute_all_applicable_attributes { shift->get_all_attributes(@_) }
100 sub get_all_attributes {
104 for my $class ($self->linearized_isa) {
105 my $meta = $self->_metaclass_cache($class)
108 for my $name (keys %{ $meta->get_attribute_map }) {
109 next if $seen{$name}++;
110 push @attr, $meta->get_attribute($name);
117 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
121 my %args = (@_ == 1 ? %{$_[0]} : @_);
123 my $instance = bless {}, $self->name;
127 foreach my $attribute ($self->get_all_attributes) {
128 my $from = $attribute->init_arg;
129 my $key = $attribute->name;
131 if (defined($from) && exists($args{$from})) {
132 $args{$from} = $attribute->coerce_constraint($args{$from})
133 if $attribute->should_coerce;
134 $attribute->verify_against_type_constraint($args{$from});
136 $instance->{$key} = $args{$from};
138 weaken($instance->{$key})
139 if ref($instance->{$key}) && $attribute->is_weak_ref;
141 if ($attribute->has_trigger) {
142 push @triggers_queue, [ $attribute->trigger, $args{$from} ];
146 if ($attribute->has_default || $attribute->has_builder) {
147 unless ($attribute->is_lazy) {
148 my $default = $attribute->default;
149 my $builder = $attribute->builder;
150 my $value = $attribute->has_builder
151 ? $instance->$builder
152 : ref($default) eq 'CODE'
153 ? $default->($instance)
156 $value = $attribute->coerce_constraint($value)
157 if $attribute->should_coerce;
158 $attribute->verify_against_type_constraint($value);
160 $instance->{$key} = $value;
162 weaken($instance->{$key})
163 if ref($instance->{$key}) && $attribute->is_weak_ref;
167 if ($attribute->is_required) {
168 $self->throw_error("Attribute (".$attribute->name.") is required");
174 foreach my $trigger_and_value(@triggers_queue){
175 my($trigger, $value) = @{$trigger_and_value};
176 $trigger->($instance, $value);
184 my $instance = shift;
186 (blessed($instance) && $instance->isa($class->name))
187 || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($instance)");
189 $class->clone_instance($instance, @_);
193 my ($class, $instance, %params) = @_;
196 || $class->throw_error("You can only clone instances, ($instance) is not a blessed instance");
198 my $clone = bless { %$instance }, ref $instance;
200 foreach my $attr ($class->get_all_attributes()) {
201 if ( defined( my $init_arg = $attr->init_arg ) ) {
202 if (exists $params{$init_arg}) {
203 $clone->{ $attr->name } = $params{$init_arg};
215 inline_constructor => 1,
216 inline_destructor => 1,
220 $self->{is_immutable}++;
222 if ($args{inline_constructor}) {
223 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
226 if ($args{inline_destructor}) {
227 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
230 # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
231 # at the end of a source file.
235 sub make_mutable { not_supported }
237 sub is_immutable { $_[0]->{is_immutable} }
238 sub is_mutable { !$_[0]->{is_immutable} }
240 sub _install_modifier {
241 my ( $self, $into, $type, $name, $code ) = @_;
243 # which is modifer class available?
244 my $modifier_class = do {
245 if (eval "require Class::Method::Modifiers::Fast; 1") {
246 'Class::Method::Modifiers::Fast';
247 } elsif (eval "require Class::Method::Modifiers; 1") {
248 'Class::Method::Modifiers';
250 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.");
253 my $modifier = $modifier_class->can('_install_modifier');
255 # replace this method itself :)
257 no warnings 'redefine';
258 *_install_modifier = sub {
259 my ( $self, $into, $type, $name, $code ) = @_;
266 $self->{methods}{$name}++; # register it to the method map
271 # call me. for first time.
272 $self->_install_modifier( $into, $type, $name, $code );
275 sub add_before_method_modifier {
276 my ( $self, $name, $code ) = @_;
277 $self->_install_modifier( $self->name, 'before', $name, $code );
280 sub add_around_method_modifier {
281 my ( $self, $name, $code ) = @_;
282 $self->_install_modifier( $self->name, 'around', $name, $code );
285 sub add_after_method_modifier {
286 my ( $self, $name, $code ) = @_;
287 $self->_install_modifier( $self->name, 'after', $name, $code );
290 sub add_override_method_modifier {
291 my ($self, $name, $code) = @_;
293 my $package = $self->name;
295 my $body = $package->can($name)
296 or $self->throw_error("You cannot override '$name' because it has no super method");
298 $self->add_method($name => sub { $code->($package, $body, @_) });
302 my ($self, $role_name) = @_;
305 || $self->throw_error("You must supply a role name to look for");
307 for my $class ($self->linearized_isa) {
308 my $meta = Mouse::Meta::Module::class_of($class);
309 next unless $meta && $meta->can('roles');
311 for my $role (@{ $meta->roles }) {
313 return 1 if $role->does_role($role_name);
326 Mouse::Meta::Class - hook into the Mouse MOP
330 =head2 initialize ClassName -> Mouse::Meta::Class
332 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
333 one instance should exist for a given class.
335 =head2 new %args -> Mouse::Meta::Class
337 Creates a new Mouse::Meta::Class. Don't call this directly.
339 =head2 name -> ClassName
341 Returns the name of the owner class.
343 =head2 superclasses -> [ClassName]
345 Gets (or sets) the list of superclasses of the owner class.
347 =head2 add_attribute (Mouse::Meta::Attribute| name => spec)
349 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
352 =head2 get_all_attributes -> (Mouse::Meta::Attribute)
354 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
355 this class and its superclasses.
357 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
359 Returns a mapping of attribute names to their corresponding
360 L<Mouse::Meta::Attribute> objects.
362 =head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
364 This returns a list of attribute names which are defined in the local
365 class. If you want a list of all applicable attributes for a class,
366 use the C<get_all_attributes> method.
368 =head2 has_attribute Name -> Bool
370 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
372 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
374 Returns the L<Mouse::Meta::Attribute> with the given name.
376 =head2 linearized_isa -> [ClassNames]
378 Returns the list of classes in method dispatch order, with duplicates removed.
380 =head2 clone_object Instance -> Instance
382 Clones the given C<Instance> which must be an instance governed by this
385 =head2 clone_instance Instance, Parameters -> Instance
387 The clone_instance method has been made private.
388 The public version is deprecated.