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} };
62 my $pkg = $self->name;
65 *{ $pkg . '::' . $name } = $code;
72 $self->{'attributes'}{$attr->name} = $attr;
75 sub compute_all_applicable_attributes {
79 for my $class ($self->linearized_isa) {
80 my $meta = $self->_metaclass_cache($class)
83 for my $name (keys %{ $meta->get_attribute_map }) {
84 next if $seen{$name}++;
85 push @attr, $meta->get_attribute($name);
92 sub get_attribute_map { $_[0]->{attributes} }
93 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
94 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
96 sub linearized_isa { @{ mro::get_linear_isa($_[0]->name) } }
100 my $instance = shift;
102 (blessed($instance) && $instance->isa($class->name))
103 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
105 $class->clone_instance($instance, @_);
109 my ($class, $instance, %params) = @_;
112 || confess "You can only clone instances, ($instance) is not a blessed instance";
114 my $clone = bless { %$instance }, ref $instance;
116 foreach my $attr ($class->compute_all_applicable_attributes()) {
117 if ( defined( my $init_arg = $attr->init_arg ) ) {
118 if (exists $params{$init_arg}) {
119 $clone->{ $attr->name } = $params{$init_arg};
128 sub make_immutable {}
136 Mouse::Meta::Class - hook into the Mouse MOP
140 =head2 initialize ClassName -> Mouse::Meta::Class
142 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
143 one instance should exist for a given class.
145 =head2 new %args -> Mouse::Meta::Class
147 Creates a new Mouse::Meta::Class. Don't call this directly.
149 =head2 name -> ClassName
151 Returns the name of the owner class.
153 =head2 superclasses -> [ClassName]
155 Gets (or sets) the list of superclasses of the owner class.
157 =head2 add_attribute Mouse::Meta::Attribute
159 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
162 =head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
164 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
165 this class and its superclasses.
167 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
169 Returns a mapping of attribute names to their corresponding
170 L<Mouse::Meta::Attribute> objects.
172 =head2 has_attribute Name -> Boool
174 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
176 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
178 Returns the L<Mouse::Meta::Attribute> with the given name.
180 =head2 linearized_isa -> [ClassNames]
182 Returns the list of classes in method dispatch order, with duplicates removed.
184 =head2 clone_object Instance -> Instance
186 Clones the given C<Instance> which must be an instance governed by this
189 =head2 clone_instance Instance, Parameters -> Instance
191 Clones the given C<Instance> and sets any additional parameters.