Same API as Moose for Mouse::Meta::Attribute->new
[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}
ad958001 155
156sub make_mutable { confess "Mouse does not currently support 'make_mutable'" }
157
fc1d8369 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) = @_;
ad958001 199
47f36c05 200 (defined $role_name)
201 || confess "You must supply a role name to look for";
ad958001 202
47f36c05 203 for my $role (@{ $self->{roles} }) {
204 return 1 if $role->name eq $role_name;
205 }
ad958001 206
47f36c05 207 return 0;
208}
209
ae3edb8a 210sub create {
211 my ( $class, @args ) = @_;
212
213 unshift @args, 'package' if @args % 2 == 1;
214
215 my (%options) = @args;
216 my $package_name = $options{package};
217
218 (ref $options{superclasses} eq 'ARRAY')
219 || confess "You must pass an ARRAY ref of superclasses"
220 if exists $options{superclasses};
ad958001 221
ae3edb8a 222 (ref $options{attributes} eq 'ARRAY')
223 || confess "You must pass an ARRAY ref of attributes"
ad958001 224 if exists $options{attributes};
225
ae3edb8a 226 (ref $options{methods} eq 'HASH')
227 || confess "You must pass a HASH ref of methods"
ad958001 228 if exists $options{methods};
ae3edb8a 229
230 do {
231 # XXX should I implement Mouse::Meta::Module?
232 my $package_name = $options{package};
233
234 ( defined $package_name && $package_name )
235 || confess "You must pass a package name";
236
237 my $code = "package $package_name;";
238 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
239 if exists $options{version};
240 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
241 if exists $options{authority};
242
243 eval $code;
244 confess "creation of $package_name failed : $@" if $@;
245 };
246
247 my (%initialize_options) = @args;
248 delete @initialize_options{qw(
249 package
250 superclasses
251 attributes
252 methods
253 version
254 authority
255 )};
256 my $meta = $class->initialize( $package_name => %initialize_options );
257
258 # FIXME totally lame
259 $meta->add_method('meta' => sub {
260 $class->initialize(ref($_[0]) || $_[0]);
261 });
262
263 $meta->superclasses(@{$options{superclasses}})
264 if exists $options{superclasses};
265 # NOTE:
266 # process attributes first, so that they can
267 # install accessors, but locally defined methods
268 # can then overwrite them. It is maybe a little odd, but
269 # I think this should be the order of things.
270 if (exists $options{attributes}) {
271 foreach my $attr (@{$options{attributes}}) {
272 Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr);
273 }
274 }
275 if (exists $options{methods}) {
276 foreach my $method_name (keys %{$options{methods}}) {
277 $meta->add_method($method_name, $options{methods}->{$method_name});
278 }
279 }
280 return $meta;
281}
282
283{
284 my $ANON_CLASS_SERIAL = 0;
285 my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::';
286 sub create_anon_class {
287 my ( $class, %options ) = @_;
288 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
289 return $class->create( $package_name, %options );
290 }
291}
292
c3398f5b 2931;
294
295__END__
296
297=head1 NAME
298
306290e8 299Mouse::Meta::Class - hook into the Mouse MOP
c3398f5b 300
301=head1 METHODS
302
306290e8 303=head2 initialize ClassName -> Mouse::Meta::Class
c3398f5b 304
306290e8 305Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only
306one instance should exist for a given class.
c3398f5b 307
306290e8 308=head2 new %args -> Mouse::Meta::Class
c3398f5b 309
306290e8 310Creates a new Mouse::Meta::Class. Don't call this directly.
c3398f5b 311
312=head2 name -> ClassName
313
314Returns the name of the owner class.
315
316=head2 superclasses -> [ClassName]
317
318Gets (or sets) the list of superclasses of the owner class.
319
306290e8 320=head2 add_attribute Mouse::Meta::Attribute
c3398f5b 321
306290e8 322Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner
323class.
c3398f5b 324
72b88a88 325=head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute)
326
327Returns the list of all L<Mouse::Meta::Attribute> instances associated with
328this class and its superclasses.
329
306290e8 330=head2 get_attribute_map -> { name => Mouse::Meta::Attribute }
c3398f5b 331
332Returns a mapping of attribute names to their corresponding
306290e8 333L<Mouse::Meta::Attribute> objects.
c3398f5b 334
66eea168 335=head2 has_attribute Name -> Boool
336
337Returns whether we have a L<Mouse::Meta::Attribute> with the given name.
338
306290e8 339=head2 get_attribute Name -> Mouse::Meta::Attribute | undef
c3398f5b 340
306290e8 341Returns the L<Mouse::Meta::Attribute> with the given name.
c3398f5b 342
343=head2 linearized_isa -> [ClassNames]
344
345Returns the list of classes in method dispatch order, with duplicates removed.
346
f7b11a21 347=head2 clone_object Instance -> Instance
348
349Clones the given C<Instance> which must be an instance governed by this
350metaclass.
351
352=head2 clone_instance Instance, Parameters -> Instance
353
354Clones the given C<Instance> and sets any additional parameters.
355
c3398f5b 356=cut
357