Commit | Line | Data |
306290e8 |
1 | package Mouse::Meta::Class; |
c3398f5b |
2 | use strict; |
3 | use warnings; |
4 | |
fc1d8369 |
5 | use Mouse::Meta::Method::Constructor; |
bbf64e76 |
6 | use Mouse::Meta::Method::Destructor; |
6c169c50 |
7 | use Scalar::Util qw/blessed/; |
8 | use Mouse::Util qw/get_linear_isa/; |
7a59f4e8 |
9 | use Carp 'confess'; |
72b88a88 |
10 | |
c3398f5b |
11 | do { |
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 | |
31 | sub 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 | |
45 | sub name { $_[0]->{name} } |
46 | |
47 | sub superclasses { |
48 | my $self = shift; |
49 | |
50 | if (@_) { |
51 | Mouse::load_class($_) for @_; |
52 | @{ $self->{superclasses} } = @_; |
53 | } |
54 | |
55 | @{ $self->{superclasses} }; |
56 | } |
57 | |
d16fe7d7 |
58 | sub 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 |
71 | sub 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 |
85 | sub add_attribute { |
86 | my $self = shift; |
87 | my $attr = shift; |
88 | |
89 | $self->{'attributes'}{$attr->name} = $attr; |
90 | } |
91 | |
72b88a88 |
92 | sub 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 |
109 | sub get_attribute_map { $_[0]->{attributes} } |
66eea168 |
110 | sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } |
c3398f5b |
111 | sub get_attribute { $_[0]->{attributes}->{$_[1]} } |
112 | |
00ca1c62 |
113 | sub linearized_isa { @{ get_linear_isa($_[0]->name) } } |
c3398f5b |
114 | |
7a59f4e8 |
115 | sub 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 | |
125 | sub 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 |
145 | sub 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 | |
156 | sub make_mutable { confess "Mouse does not currently support 'make_mutable'" } |
157 | |
fc1d8369 |
158 | sub is_immutable { $_[0]->{is_immutable} } |
84ef660f |
159 | |
160 | sub attribute_metaclass { "Mouse::Meta::Class" } |
7a59f4e8 |
161 | |
50dc6ee5 |
162 | sub 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 | |
173 | sub 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 | |
184 | sub 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 |
195 | sub roles { $_[0]->{roles} } |
196 | |
197 | sub 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 |
210 | sub create { |
1a72f15c |
211 | my ($self, $package_name, %options) = @_; |
ae3edb8a |
212 | |
213 | (ref $options{superclasses} eq 'ARRAY') |
214 | || confess "You must pass an ARRAY ref of superclasses" |
215 | if exists $options{superclasses}; |
ad958001 |
216 | |
ae3edb8a |
217 | (ref $options{attributes} eq 'ARRAY') |
218 | || confess "You must pass an ARRAY ref of attributes" |
ad958001 |
219 | if exists $options{attributes}; |
220 | |
ae3edb8a |
221 | (ref $options{methods} eq 'HASH') |
222 | || confess "You must pass a HASH ref of methods" |
ad958001 |
223 | if exists $options{methods}; |
ae3edb8a |
224 | |
225 | do { |
ae3edb8a |
226 | ( defined $package_name && $package_name ) |
227 | || confess "You must pass a package name"; |
228 | |
229 | my $code = "package $package_name;"; |
230 | $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';" |
231 | if exists $options{version}; |
232 | $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';" |
233 | if exists $options{authority}; |
234 | |
235 | eval $code; |
236 | confess "creation of $package_name failed : $@" if $@; |
237 | }; |
238 | |
dd3c04d7 |
239 | my %initialize_options = %options; |
240 | delete @initialize_options{qw( |
ae3edb8a |
241 | package |
242 | superclasses |
243 | attributes |
244 | methods |
245 | version |
246 | authority |
247 | )}; |
dd3c04d7 |
248 | my $meta = $self->initialize( $package_name => %initialize_options ); |
ae3edb8a |
249 | |
250 | # FIXME totally lame |
251 | $meta->add_method('meta' => sub { |
1a72f15c |
252 | $self->initialize(ref($_[0]) || $_[0]); |
ae3edb8a |
253 | }); |
254 | |
255 | $meta->superclasses(@{$options{superclasses}}) |
256 | if exists $options{superclasses}; |
257 | # NOTE: |
258 | # process attributes first, so that they can |
259 | # install accessors, but locally defined methods |
260 | # can then overwrite them. It is maybe a little odd, but |
261 | # I think this should be the order of things. |
262 | if (exists $options{attributes}) { |
263 | foreach my $attr (@{$options{attributes}}) { |
264 | Mouse::Meta::Attribute->create($meta, $attr->{name}, %$attr); |
265 | } |
266 | } |
267 | if (exists $options{methods}) { |
268 | foreach my $method_name (keys %{$options{methods}}) { |
269 | $meta->add_method($method_name, $options{methods}->{$method_name}); |
270 | } |
271 | } |
272 | return $meta; |
273 | } |
274 | |
275 | { |
276 | my $ANON_CLASS_SERIAL = 0; |
277 | my $ANON_CLASS_PREFIX = 'Mouse::Meta::Class::__ANON__::SERIAL::'; |
278 | sub create_anon_class { |
279 | my ( $class, %options ) = @_; |
280 | my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL; |
281 | return $class->create( $package_name, %options ); |
282 | } |
283 | } |
284 | |
c3398f5b |
285 | 1; |
286 | |
287 | __END__ |
288 | |
289 | =head1 NAME |
290 | |
306290e8 |
291 | Mouse::Meta::Class - hook into the Mouse MOP |
c3398f5b |
292 | |
293 | =head1 METHODS |
294 | |
306290e8 |
295 | =head2 initialize ClassName -> Mouse::Meta::Class |
c3398f5b |
296 | |
306290e8 |
297 | Finds or creates a Mouse::Meta::Class instance for the given ClassName. Only |
298 | one instance should exist for a given class. |
c3398f5b |
299 | |
306290e8 |
300 | =head2 new %args -> Mouse::Meta::Class |
c3398f5b |
301 | |
306290e8 |
302 | Creates a new Mouse::Meta::Class. Don't call this directly. |
c3398f5b |
303 | |
304 | =head2 name -> ClassName |
305 | |
306 | Returns the name of the owner class. |
307 | |
308 | =head2 superclasses -> [ClassName] |
309 | |
310 | Gets (or sets) the list of superclasses of the owner class. |
311 | |
306290e8 |
312 | =head2 add_attribute Mouse::Meta::Attribute |
c3398f5b |
313 | |
306290e8 |
314 | Begins keeping track of the existing L<Mouse::Meta::Attribute> for the owner |
315 | class. |
c3398f5b |
316 | |
72b88a88 |
317 | =head2 compute_all_applicable_attributes -> (Mouse::Meta::Attribute) |
318 | |
319 | Returns the list of all L<Mouse::Meta::Attribute> instances associated with |
320 | this class and its superclasses. |
321 | |
306290e8 |
322 | =head2 get_attribute_map -> { name => Mouse::Meta::Attribute } |
c3398f5b |
323 | |
324 | Returns a mapping of attribute names to their corresponding |
306290e8 |
325 | L<Mouse::Meta::Attribute> objects. |
c3398f5b |
326 | |
66eea168 |
327 | =head2 has_attribute Name -> Boool |
328 | |
329 | Returns whether we have a L<Mouse::Meta::Attribute> with the given name. |
330 | |
306290e8 |
331 | =head2 get_attribute Name -> Mouse::Meta::Attribute | undef |
c3398f5b |
332 | |
306290e8 |
333 | Returns the L<Mouse::Meta::Attribute> with the given name. |
c3398f5b |
334 | |
335 | =head2 linearized_isa -> [ClassNames] |
336 | |
337 | Returns the list of classes in method dispatch order, with duplicates removed. |
338 | |
f7b11a21 |
339 | =head2 clone_object Instance -> Instance |
340 | |
341 | Clones the given C<Instance> which must be an instance governed by this |
342 | metaclass. |
343 | |
344 | =head2 clone_instance Instance, Parameters -> Instance |
345 | |
346 | Clones the given C<Instance> and sets any additional parameters. |
347 | |
c3398f5b |
348 | =cut |
349 | |