2 package Mouse::Meta::Class;
6 use Mouse::Util qw/get_linear_isa blessed/;
12 # because Mouse doesn't introspect existing classes, we're forced to
13 # only pay attention to other Mouse classes
14 sub _metaclass_cache {
17 return $METACLASS_CACHE{$name};
23 $METACLASS_CACHE{$name} = $class->new(name => $name)
24 if !exists($METACLASS_CACHE{$name});
25 return $METACLASS_CACHE{$name};
33 $args{attributes} = {};
34 $args{superclasses} = do {
36 \@{ $args{name} . '::ISA' };
42 sub name { $_[0]->{name} }
48 Mouse::load_class($_) for @_;
49 @{ $self->{superclasses} } = @_;
52 @{ $self->{superclasses} };
60 my $pkg = $self->name;
63 *{ $pkg . '::' . $name } = $code;
70 $self->{'attributes'}{$attr->name} = $attr;
73 sub compute_all_applicable_attributes {
77 for my $class ($self->linearized_isa) {
78 my $meta = $self->_metaclass_cache($class)
81 for my $name (keys %{ $meta->get_attribute_map }) {
82 next if $seen{$name}++;
83 push @attr, $meta->get_attribute($name);
90 sub get_attribute_map { $_[0]->{attributes} }
91 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
92 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
94 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
100 (blessed($instance) && $instance->isa($class->name))
101 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
103 $class->clone_instance($instance, @_);
107 my ($class, $instance, %params) = @_;
110 || confess "You can only clone instances, ($instance) is not a blessed instance";
112 my $clone = bless { %$instance }, ref $instance;
114 foreach my $attr ($class->compute_all_applicable_attributes()) {
115 if ( defined( my $init_arg = $attr->init_arg ) ) {
116 if (exists $params{$init_arg}) {
117 $clone->{ $attr->name } = $params{$init_arg};
126 sub make_immutable {}
127 sub is_immutable { 0 }
129 sub attribute_metaclass { "Mouse::Meta::Class" }
131 sub add_before_method_modifier {
132 my ($self, $name, $code) = @_;
133 require Class::Method::Modifiers;
134 Class::Method::Modifiers::_install_modifier(
142 sub add_around_method_modifier {
143 my ($self, $name, $code) = @_;
144 require Class::Method::Modifiers;
145 Class::Method::Modifiers::_install_modifier(
153 sub add_after_method_modifier {
154 my ($self, $name, $code) = @_;
155 require Class::Method::Modifiers;
156 Class::Method::Modifiers::_install_modifier(
170 Mouse::Meta::Class - hook into the Mouse MOP
174 =head2 initialize ClassName -> Mouse::Meta::Class
176 Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
177 one instance should exist for a given class.
179 =head2 new %args -> Mouse::Meta::Class
181 Creates a new Mouse::Meta::Class. Don't call this directly.
183 =head2 name -> ClassName
185 Returns the name of the owner class.
187 =head2 superclasses -> [ClassName]
189 Gets (or sets) the list of superclasses of the owner class.
191 =head2 add_attribute Mouse::Meta::Attribute
193 Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
196 =head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
198 Returns the list of all L<Mouse::Meta::Attribute> instances associated with
199 this class and its superclasses.
201 =head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
203 Returns a mapping of attribute names to their corresponding
204 L<Mouse::Meta::Attribute> objects.
206 =head2 has_attribute Name -> Boool
208 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
210 =head2 get_attribute Name -> Mouse::Meta::Attribute | undef
212 Returns the L<Mouse::Meta::Attribute> with the given name.
214 =head2 linearized_isa -> [ClassNames]
216 Returns the list of classes in method dispatch order, with duplicates removed.
218 =head2 clone_object Instance -> Instance
220 Clones the given C<Instance> which must be an instance governed by this
223 =head2 clone_instance Instance, Parameters -> Instance
225 Clones the given C<Instance> and sets any additional parameters.