2 package Mouse::Meta::Class;
6 use Scalar::Util 'blessed';
14 # because Mouse doesn't introspect existing classes, we're forced to
15 # only pay attention to other Mouse classes
16 sub _metaclass_cache {
19 return $METACLASS_CACHE{$name};
25 $METACLASS_CACHE{$name} = $class->new(name => $name)
26 if !exists($METACLASS_CACHE{$name});
27 return $METACLASS_CACHE{$name};
35 $args{attributes} = {};
36 $args{superclasses} = do {
38 \@{ $args{name} . '::ISA' };
44 sub name { $_[0]->{name} }
50 Mouse::load_class($_) for @_;
51 @{ $self->{superclasses} } = @_;
54 @{ $self->{superclasses} };
61 $self->{'attributes'}{$attr->name} = $attr;
64 sub compute_all_applicable_attributes {
68 for my $class ($self->linearized_isa) {
69 my $meta = $self->_metaclass_cache($class)
72 for my $name (keys %{ $meta->get_attribute_map }) {
73 next if $seen{$name}++;
74 push @attr, $meta->get_attribute($name);
81 sub get_attribute_map { $_[0]->{attributes} }
82 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
83 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
85 sub linearized_isa { @{ mro::get_linear_isa($_[0]->name) } }
91 (blessed($instance) && $instance->isa($class->name))
92 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
94 $class->clone_instance($instance, @_);
98 my ($class, $instance, %params) = @_;
101 || confess "You can only clone instances, ($instance) is not a blessed instance";
103 my $clone = bless { %$instance }, ref $instance;
105 foreach my $attr ($class->compute_all_applicable_attributes()) {
106 if ( defined( my $init_arg = $attr->init_arg ) ) {
107 if (exists $params{$init_arg}) {
108 $clone->{ $attr->name } = $params{$init_arg};
124 Mouse::Meta::Class - hook into the Mouse MOP
128 =head2 initialize ClassName -> Mouse::Meta::Class
130 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
131 one instance should exist for a given class.
133 =head2 new %args -> Mouse::Meta::Class
135 Creates a new Mouse::Meta::Class. Don't call this directly.
137 =head2 name -> ClassName
139 Returns the name of the owner class.
141 =head2 superclasses -> [ClassName]
143 Gets (or sets) the list of superclasses of the owner class.
145 =head2 add_attribute Mouse::Meta::Attribute
147 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
150 =head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
152 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
153 this class and its superclasses.
155 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
157 Returns a mapping of attribute names to their corresponding
158 L<Mouse::Meta::Attribute> objects.
160 =head2 has_attribute Name -> Boool
162 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
164 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
166 Returns the L<Mouse::Meta::Attribute> with the given name.
168 =head2 linearized_isa -> [ClassNames]
170 Returns the list of classes in method dispatch order, with duplicates removed.
172 =head2 clone_object Instance -> Instance
174 Clones the given C<Instance> which must be an instance governed by this
177 =head2 clone_instance Instance, Parameters -> Instance
179 Clones the given C<Instance> and sets any additional parameters.