88e9b55c03447e41ed25a8af5874cb1d22035326
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
1
2 package Moose::Meta::Class;
3
4 use strict;
5 use warnings;
6
7 use Class::MOP;
8
9 use Carp         'confess';
10 use Scalar::Util 'weaken', 'blessed', 'reftype';
11
12 our $VERSION = '0.07';
13
14 use base 'Class::MOP::Class';
15
16 __PACKAGE__->meta->add_attribute('roles' => (
17     reader  => 'roles',
18     default => sub { [] }
19 ));
20
21 sub initialize {
22     my $class = shift;
23     my $pkg   = shift;
24     $class->SUPER::initialize($pkg,
25         ':attribute_metaclass' => 'Moose::Meta::Attribute', 
26         ':instance_metaclass'  => 'Moose::Meta::Instance', 
27         @_);
28 }  
29
30 sub add_role {
31     my ($self, $role) = @_;
32     (blessed($role) && $role->isa('Moose::Meta::Role'))
33         || confess "Roles must be instances of Moose::Meta::Role";
34     push @{$self->roles} => $role;
35 }
36
37 sub calculate_all_roles {
38     my $self = shift;
39     my %seen;
40     grep { !$seen{$_->name}++ } map { $_->calculate_all_roles } @{ $self->roles };
41 }
42
43 sub does_role {
44     my ($self, $role_name) = @_;
45     (defined $role_name)
46         || confess "You must supply a role name to look for";
47     foreach my $class ($self->class_precedence_list) {
48         next unless $class->can('meta');        
49         foreach my $role (@{$class->meta->roles}) {
50             return 1 if $role->does_role($role_name);
51         }
52     }
53     return 0;
54 }
55
56 sub excludes_role {
57     my ($self, $role_name) = @_;
58     (defined $role_name)
59         || confess "You must supply a role name to look for";
60     foreach my $class ($self->class_precedence_list) {  
61         next unless $class->can('meta');      
62         foreach my $role (@{$class->meta->roles}) {
63             return 1 if $role->excludes_role($role_name);
64         }
65     }
66     return 0;
67 }
68
69 sub new_object {
70     my ($class, %params) = @_;
71     my $self = $class->SUPER::new_object(%params);
72     foreach my $attr ($class->compute_all_applicable_attributes()) {
73         # FIXME:
74         # this does not accept undefined
75         # values, nor does it accept false 
76         # values to be passed into the init-arg
77         next unless $params{$attr->init_arg} && $attr->can('has_trigger') && $attr->has_trigger;
78         $attr->trigger->($self, $params{$attr->init_arg}, $attr);
79     }
80     return $self;    
81 }
82
83 sub construct_instance {
84     my ($class, %params) = @_;
85     my $meta_instance = $class->get_meta_instance;
86     # FIXME:
87     # the code below is almost certainly incorrect
88     # but this is foreign inheritence, so we might
89     # have to kludge it in the end. 
90     my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
91     foreach my $attr ($class->compute_all_applicable_attributes()) { 
92         $attr->initialize_instance_slot($meta_instance, $instance, \%params)
93     }
94     return $instance;
95 }
96
97 sub has_method {
98     my ($self, $method_name) = @_;
99     (defined $method_name && $method_name)
100         || confess "You must define a method name";    
101
102     my $sub_name = ($self->name . '::' . $method_name);   
103     
104     # FIXME:
105     # this should use the ::Package code
106     # and not turn off strict refs
107     no strict 'refs';
108     return 0 if !defined(&{$sub_name});        
109         my $method = \&{$sub_name};
110         
111         return 1 if blessed($method) && $method->isa('Moose::Meta::Role::Method');
112     return $self->SUPER::has_method($method_name);    
113 }
114
115 sub add_attribute {
116     my $self = shift;
117     my $name = shift;
118     if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
119         # NOTE:
120         # if it is a HASH ref, we de-ref it.        
121         # this will usually mean that it is 
122         # coming from a role
123         $self->SUPER::add_attribute($name => %{$_[0]});
124     }
125     else {
126         # otherwise we just pass the args
127         $self->SUPER::add_attribute($name => @_);
128     }
129 }
130
131 sub add_override_method_modifier {
132     my ($self, $name, $method, $_super_package) = @_;
133     (!$self->has_method($name))
134         || confess "Cannot add an override method if a local method is already present";
135     # need this for roles ...
136     $_super_package ||= $self->name;
137     my $super = $self->find_next_method_by_name($name);
138     (defined $super)
139         || confess "You cannot override '$name' because it has no super method";    
140     $self->add_method($name => bless sub {
141         my @args = @_;
142         no strict   'refs';
143         no warnings 'redefine';
144         local *{$_super_package . '::super'} = sub { $super->(@args) };
145         return $method->(@args);
146     } => 'Moose::Meta::Method::Overriden');
147 }
148
149 sub add_augment_method_modifier {
150     my ($self, $name, $method) = @_;  
151     (!$self->has_method($name))
152         || confess "Cannot add an augment method if a local method is already present";    
153     my $super = $self->find_next_method_by_name($name);
154     (defined $super)
155         || confess "You cannot augment '$name' because it has no super method";    
156     my $_super_package = $super->package_name;   
157     # BUT!,... if this is an overriden method ....     
158     if ($super->isa('Moose::Meta::Method::Overriden')) {
159         # we need to be sure that we actually 
160         # find the next method, which is not 
161         # an 'override' method, the reason is
162         # that an 'override' method will not 
163         # be the one calling inner()
164         my $real_super = $self->_find_next_method_by_name_which_is_not_overridden($name);        
165         $_super_package = $real_super->package_name;
166     }      
167     $self->add_method($name => sub {
168         my @args = @_;
169         no strict   'refs';
170         no warnings 'redefine';
171         local *{$_super_package . '::inner'} = sub { $method->(@args) };
172         return $super->(@args);
173     });    
174 }
175
176 ## Private Utility methods ...
177
178 sub _find_next_method_by_name_which_is_not_overridden {
179     my ($self, $name) = @_;
180     foreach my $method ($self->find_all_methods_by_name($name)) {
181         return $method->{code} 
182             if blessed($method->{code}) && !$method->{code}->isa('Moose::Meta::Method::Overriden');
183     }
184     return undef;
185 }
186
187 sub _fix_metaclass_incompatability {
188     my ($self, @superclasses) = @_;
189     foreach my $super (@superclasses) {
190         # don't bother if it does not have a meta.
191         next unless $super->can('meta');
192         # if it's meta is a vanilla Moose, 
193         # then we can safely ignore it.
194         next if blessed($super->meta) eq 'Moose::Meta::Class';
195         # but if we have anything else, 
196         # we need to check it out ...
197         unless (# see if of our metaclass is incompatible
198                 ($self->isa(blessed($super->meta)) &&
199                  # and see if our instance metaclass is incompatible
200                  $self->instance_metaclass->isa($super->meta->instance_metaclass)) &&
201                 # ... and if we are just a vanilla Moose
202                 $self->isa('Moose::Meta::Class')) {
203             # re-initialize the meta ...
204             my $super_meta = $super->meta;
205             # NOTE:
206             # We might want to consider actually 
207             # transfering any attributes from the 
208             # original meta into this one, but in 
209             # general you should not have any there
210             # at this point anyway, so it's very 
211             # much an obscure edge case anyway
212             $self = $super_meta->reinitialize($self->name => (
213                 ':attribute_metaclass' => $super_meta->attribute_metaclass,                            
214                 ':method_metaclass'    => $super_meta->method_metaclass,
215                 ':instance_metaclass'  => $super_meta->instance_metaclass,
216             ));
217         }
218     }
219     return $self;    
220 }
221
222 sub _apply_all_roles {
223     my ($self, @roles) = @_;
224     ($_->can('meta') && $_->meta->isa('Moose::Meta::Role'))
225         || confess "You can only consume roles, $_ is not a Moose role"
226             foreach @roles;
227     if (scalar @roles == 1) {
228         $roles[0]->meta->apply($self);
229     }
230     else {
231         # FIXME
232         # we should make a Moose::Meta::Role::Composite
233         # which is a smaller version of Moose::Meta::Role
234         # which does not use any package stuff
235         Moose::Meta::Role->combine(
236             map { $_->meta } @roles
237         )->apply($self);
238     }    
239 }
240
241 sub _process_attribute {
242     my ($self, $name, %options) = @_;
243     if ($name =~ /^\+(.*)/) {
244         my $new_attr = $self->_process_inherited_attribute($1, %options);
245         $self->add_attribute($new_attr);
246     }
247     else {
248         if ($options{metaclass}) {
249             Moose::_load_all_classes($options{metaclass});
250             $self->add_attribute($options{metaclass}->new($name, %options));
251         }
252         else {
253             $self->add_attribute($name, %options);
254         }
255     }    
256 }
257
258 sub _process_inherited_attribute {
259     my ($self, $attr_name, %options) = @_;
260     my $inherited_attr = $self->find_attribute_by_name($attr_name);
261     (defined $inherited_attr)
262         || confess "Could not find an attribute by the name of '$attr_name' to inherit from";
263     my $new_attr;
264     if ($inherited_attr->isa('Moose::Meta::Attribute')) {
265         $new_attr = $inherited_attr->clone_and_inherit_options(%options);
266     }
267     else {
268         # NOTE:
269         # kind of a kludge to handle Class::MOP::Attributes
270         $new_attr = Moose::Meta::Attribute::clone_and_inherit_options(
271             $inherited_attr, %options
272         );                        
273     }    
274     return $new_attr;
275 }
276
277 package Moose::Meta::Method::Overriden;
278
279 use strict;
280 use warnings;
281
282 our $VERSION = '0.01';
283
284 use base 'Class::MOP::Method';
285
286 1;
287
288 __END__
289
290 =pod
291
292 =head1 NAME
293
294 Moose::Meta::Class - The Moose metaclass
295
296 =head1 DESCRIPTION
297
298 This is a subclass of L<Class::MOP::Class> with Moose specific 
299 extensions.
300
301 For the most part, the only time you will ever encounter an 
302 instance of this class is if you are doing some serious deep 
303 introspection. To really understand this class, you need to refer 
304 to the L<Class::MOP::Class> documentation.
305
306 =head1 METHODS
307
308 =over 4
309
310 =item B<initialize>
311
312 =item B<new_object>
313
314 We override this method to support the C<trigger> attribute option.
315
316 =item B<construct_instance>
317
318 This provides some Moose specific extensions to this method, you 
319 almost never call this method directly unless you really know what 
320 you are doing. 
321
322 This method makes sure to handle the moose weak-ref, type-constraint
323 and type coercion features. 
324
325 =item B<has_method ($name)>
326
327 This accommodates Moose::Meta::Role::Method instances, which are 
328 aliased, instead of added, but still need to be counted as valid 
329 methods.
330
331 =item B<add_override_method_modifier ($name, $method)>
332
333 This will create an C<override> method modifier for you, and install 
334 it in the package.
335
336 =item B<add_augment_method_modifier ($name, $method)>
337
338 This will create an C<augment> method modifier for you, and install 
339 it in the package.
340
341 =item B<calculate_all_roles>
342
343 =item B<roles>
344
345 This will return an array of C<Moose::Meta::Role> instances which are 
346 attached to this class.
347
348 =item B<add_role ($role)>
349
350 This takes an instance of C<Moose::Meta::Role> in C<$role>, and adds it 
351 to the list of associated roles.
352
353 =item B<does_role ($role_name)>
354
355 This will test if this class C<does> a given C<$role_name>. It will 
356 not only check it's local roles, but ask them as well in order to 
357 cascade down the role hierarchy.
358
359 =item B<excludes_role ($role_name)>
360
361 This will test if this class C<excludes> a given C<$role_name>. It will 
362 not only check it's local roles, but ask them as well in order to 
363 cascade down the role hierarchy.
364
365 =item B<add_attribute ($attr_name, %params|$params)>
366
367 This method does the same thing as L<Class::MOP::Class::add_attribute>, but adds
368 support for taking the C<$params> as a HASH ref.
369
370 =back
371
372 =head1 BUGS
373
374 All complex software has bugs lurking in it, and this module is no 
375 exception. If you find a bug please either email me, or add the bug
376 to cpan-RT.
377
378 =head1 AUTHOR
379
380 Stevan Little E<lt>stevan@iinteractive.comE<gt>
381
382 =head1 COPYRIGHT AND LICENSE
383
384 Copyright 2006 by Infinity Interactive, Inc.
385
386 L<http://www.iinteractive.com>
387
388 This library is free software; you can redistribute it and/or modify
389 it under the same terms as Perl itself. 
390
391 =cut
392