Anchor this exclusion regex so "has_list" etc will work
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
CommitLineData
306290e8 1package Mouse::Meta::Class;
c3398f5b 2use strict;
3use warnings;
4
fc1d8369 5use Mouse::Meta::Method::Constructor;
bbf64e76 6use Mouse::Meta::Method::Destructor;
6c169c50 7use Scalar::Util qw/blessed/;
8use Mouse::Util qw/get_linear_isa/;
7a59f4e8 9use Carp 'confess';
72b88a88 10
c3398f5b 11do {
12 my %METACLASS_CACHE;
72b88a88 13
14 # because Mouse doesn't introspect existing classes, we're forced to
15 # only pay attention to other Mouse classes
16 sub _metaclass_cache {
17 my $class = shift;
18 my $name = shift;
19 return $METACLASS_CACHE{$name};
20 }
21
c3398f5b 22 sub initialize {
23 my $class = shift;
24 my $name = shift;
25 $METACLASS_CACHE{$name} = $class->new(name => $name)
26 if !exists($METACLASS_CACHE{$name});
27 return $METACLASS_CACHE{$name};
28 }
29};
30
31sub new {
32 my $class = shift;
6cbacbf6 33 my %args = @_;
c3398f5b 34
35 $args{attributes} = {};
36 $args{superclasses} = do {
37 no strict 'refs';
38 \@{ $args{name} . '::ISA' };
39 };
47f36c05 40 $args{roles} ||= [];
c3398f5b 41
42 bless \%args, $class;
43}
44
45sub name { $_[0]->{name} }
46
47sub superclasses {
48 my $self = shift;
49
50 if (@_) {
51 Mouse::load_class($_) for @_;
52 @{ $self->{superclasses} } = @_;
53 }
54
55 @{ $self->{superclasses} };
56}
57
d16fe7d7 58sub add_method {
59 my $self = shift;
60 my $name = shift;
61 my $code = shift;
62
63 my $pkg = $self->name;
64
65 no strict 'refs';
8632b6fe 66 $self->{'methods'}->{$name}++; # Moose stores meta object here.
d16fe7d7 67 *{ $pkg . '::' . $name } = $code;
68}
69
2e92bb89 70# copied from Class::Inspector
71sub get_method_list {
72 my $self = shift;
73 my $name = $self->name;
74
75 no strict 'refs';
76 # Get all the CODE symbol table entries
8632b6fe 77 my @functions =
a56aa0ee 78 grep !/^(?:has|with|around|before|after|blessed|extends|confess)$/,
2e92bb89 79 grep { defined &{"${name}::$_"} }
80 keys %{"${name}::"};
8632b6fe 81 push @functions, keys %{$self->{'methods'}->{$name}};
2e92bb89 82 wantarray ? @functions : \@functions;
83}
84
c3398f5b 85sub add_attribute {
86 my $self = shift;
87 my $attr = shift;
88
89 $self->{'attributes'}{$attr->name} = $attr;
90}
91
72b88a88 92sub compute_all_applicable_attributes {
93 my $self = shift;
94 my (@attr, %seen);
95
96 for my $class ($self->linearized_isa) {
97 my $meta = $self->_metaclass_cache($class)
98 or next;
99
100 for my $name (keys %{ $meta->get_attribute_map }) {
101 next if $seen{$name}++;
102 push @attr, $meta->get_attribute($name);
103 }
104 }
105
106 return @attr;
107}
108
c3398f5b 109sub get_attribute_map { $_[0]->{attributes} }
66eea168 110sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
c3398f5b 111sub get_attribute { $_[0]->{attributes}->{$_[1]} }
112
00ca1c62 113sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
c3398f5b 114
7a59f4e8 115sub clone_object {
116 my $class = shift;
117 my $instance = shift;
118
119 (blessed($instance) && $instance->isa($class->name))
1a0f0802 120 || confess "You must pass an instance of the metaclass (" . $class->name . "), not ($instance)";
7a59f4e8 121
122 $class->clone_instance($instance, @_);
123}
124
125sub clone_instance {
126 my ($class, $instance, %params) = @_;
127
128 (blessed($instance))
e42bee44 129 || confess "You can only clone instances, ($instance) is not a blessed instance";
7a59f4e8 130
131 my $clone = bless { %$instance }, ref $instance;
132
133 foreach my $attr ($class->compute_all_applicable_attributes()) {
134 if ( defined( my $init_arg = $attr->init_arg ) ) {
135 if (exists $params{$init_arg}) {
136 $clone->{ $attr->name } = $params{$init_arg};
137 }
138 }
139 }
140
141 return $clone;
142
143}
144
fc1d8369 145sub make_immutable {
146 my $self = shift;
8632b6fe 147 my %args = @_;
fc1d8369 148 my $name = $self->name;
149 $self->{is_immutable}++;
8632b6fe 150 $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
151 if ($args{inline_destructor}) {
152 $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
153 }
fc1d8369 154}
155sub make_mutable {
156 Carp::croak "Mouse::Meta::Class->make_mutable does not supported by Mouse";
157}
158sub is_immutable { $_[0]->{is_immutable} }
84ef660f 159
160sub attribute_metaclass { "Mouse::Meta::Class" }
7a59f4e8 161
50dc6ee5 162sub add_before_method_modifier {
163 my ($self, $name, $code) = @_;
4e31595c 164 require Class::Method::Modifiers;
50dc6ee5 165 Class::Method::Modifiers::_install_modifier(
166 $self->name,
167 'before',
168 $name,
169 $code,
170 );
171}
172
173sub add_around_method_modifier {
174 my ($self, $name, $code) = @_;
4e31595c 175 require Class::Method::Modifiers;
50dc6ee5 176 Class::Method::Modifiers::_install_modifier(
177 $self->name,
178 'around',
179 $name,
180 $code,
181 );
182}
183
184sub add_after_method_modifier {
185 my ($self, $name, $code) = @_;
4e31595c 186 require Class::Method::Modifiers;
50dc6ee5 187 Class::Method::Modifiers::_install_modifier(
188 $self->name,
189 'after',
190 $name,
191 $code,
192 );
193}
194
47f36c05 195sub roles { $_[0]->{roles} }
196
197sub does_role {
198 my ($self, $role_name) = @_;
199 (defined $role_name)
200 || confess "You must supply a role name to look for";
201 for my $role (@{ $self->{roles} }) {
202 return 1 if $role->name eq $role_name;
203 }
204 return 0;
205}
206
ae3edb8a 207sub create {
208 my ( $class, @args ) = @_;
209
210 unshift @args, 'package' if @args % 2 == 1;
211
212 my (%options) = @args;
213 my $package_name = $options{package};
214
215 (ref $options{superclasses} eq 'ARRAY')
216 || confess "You must pass an ARRAY ref of superclasses"
217 if exists $options{superclasses};
218
219 (ref $options{attributes} eq 'ARRAY')
220 || confess "You must pass an ARRAY ref of attributes"
221 if exists $options{attributes};
222
223 (ref $options{methods} eq 'HASH')
224 || confess "You must pass a HASH ref of methods"
225 if exists $options{methods};
226
227 do {
228 # XXX should I implement Mouse::Meta::Module?
229 my $package_name = $options{package};
230
231 ( defined $package_name && $package_name )
232 || confess "You must pass a package name";
233
234 my $code = "package $package_name;";
235 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
236 if exists $options{version};
237 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
238 if exists $options{authority};
239
240 eval $code;
241 confess "creation of $package_name failed : $@" if $@;
242 };
243
244 my (%initialize_options) = @args;
245 delete @initialize_options{qw(
246 package
247 superclasses
248 attributes
249 methods
250 version
251 authority
252 )};
253 my $meta = $class->initialize( $package_name => %initialize_options );
254
255 # FIXME totally lame
256 $meta->add_method('meta' => sub {
257 $class->initialize(ref($_[0]) || $_[0]);
258 });
259
260 $meta->superclasses(@{$options{superclasses}})
261 if exists $options{superclasses};
262 # NOTE:
263 # process attributes first, so that they can
264 # install accessors, but locally defined methods
265 # can then overwrite them. It is maybe a little odd, but
266 # I think this should be the order of things.
267 if (exists $options{attributes}) {
268 foreach my $attr (@{$options{attributes}}) {
269 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
270 }
271 }
272 if (exists $options{methods}) {
273 foreach my $method_name (keys %{$options{methods}}) {
274 $meta->add_method($method_name, $options{methods}->{$method_name});
275 }
276 }
277 return $meta;
278}
279
280{
281 my $ANON_CLASS_SERIAL = 0;
282 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
283 sub create_anon_class {
284 my ( $class, %options ) = @_;
285 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
286 return $class->create( $package_name, %options );
287 }
288}
289
c3398f5b 2901;
291
292__END__
293
294=head1 NAME
295
306290e8 296Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 297
298=head1 METHODS
299
306290e8 300=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 301
306290e8 302Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
303one instance should exist for a given class.
c3398f5b 304
306290e8 305=head2 new %args -> Mouse::Meta::Class
c3398f5b 306
306290e8 307Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 308
309=head2 name -> ClassName
310
311Returns the name of the owner class.
312
313=head2 superclasses -> [ClassName]
314
315Gets (or sets) the list of superclasses of the owner class.
316
306290e8 317=head2 add_attribute Mouse::Meta::Attribute
c3398f5b 318
306290e8 319Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
320class.
c3398f5b 321
72b88a88 322=head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
323
324Returns the list of all L<Mouse::Meta::Attribute> instances associated with
325this class and its superclasses.
326
306290e8 327=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 328
329Returns a mapping of attribute names to their corresponding
306290e8 330L<Mouse::Meta::Attribute> objects.
c3398f5b 331
66eea168 332=head2 has_attribute Name -> Boool
333
334Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
335
306290e8 336=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 337
306290e8 338Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 339
340=head2 linearized_isa -> [ClassNames]
341
342Returns the list of classes in method dispatch order, with duplicates removed.
343
f7b11a21 344=head2 clone_object Instance -> Instance
345
346Clones the given C<Instance> which must be an instance governed by this
347metaclass.
348
349=head2 clone_instance Instance, Parameters -> Instance
350
351Clones the given C<Instance> and sets any additional parameters.
352
c3398f5b 353=cut
354