939cd29fc5e3f49c764fde1eb534f69a045a286c
[gitmo/Class-MOP.git] / lib / Class / MOP / Mixin / HasMethods.pm
1 package Class::MOP::Mixin::HasMethods;
2
3 use strict;
4 use warnings;
5
6 use Class::MOP::Method::Meta;
7
8 our $VERSION   = '1.11';
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
11
12 use Scalar::Util 'blessed';
13 use Carp         'confess';
14 use Sub::Name    'subname';
15
16 use base 'Class::MOP::Mixin';
17
18 sub method_metaclass         { $_[0]->{'method_metaclass'}            }
19 sub wrapped_method_metaclass { $_[0]->{'wrapped_method_metaclass'}    }
20 sub _meta_method_class       { 'Class::MOP::Method::Meta'             }
21
22 sub _add_meta_method {
23     my $self = shift;
24     my ($name) = @_;
25     my $existing_method = $self->can('find_method_by_name')
26                               ? $self->find_method_by_name($name)
27                               : $self->get_method($name);
28     return if $existing_method
29            && $existing_method->isa($self->_meta_method_class);
30     $self->add_method(
31         $name => $self->_meta_method_class->wrap(
32             name                 => $name,
33             package_name         => $self->name,
34             associated_metaclass => $self,
35         )
36     );
37 }
38
39 sub wrap_method_body {
40     my ( $self, %args ) = @_;
41
42     ( 'CODE' eq ref $args{body} )
43         || confess "Your code block must be a CODE reference";
44
45     $self->method_metaclass->wrap(
46         package_name => $self->name,
47         %args,
48     );
49 }
50
51 sub add_method {
52     my ( $self, $method_name, $method ) = @_;
53     ( defined $method_name && length $method_name )
54         || confess "You must define a method name";
55
56     my $package_name = $self->name;
57
58     my $body;
59     if ( blessed($method) ) {
60         $body = $method->body;
61         if ( $method->package_name ne $package_name ) {
62             $method = $method->clone(
63                 package_name => $package_name,
64                 name         => $method_name,
65             ) if $method->can('clone');
66         }
67
68         $method->attach_to_class($self);
69     }
70     else {
71         # If a raw code reference is supplied, its method object is not created.
72         # The method object won't be created until required.
73         $body = $method;
74     }
75
76     $self->_method_map->{$method_name} = $method;
77
78     my ( $current_package, $current_name ) = Class::MOP::get_code_info($body);
79
80     if ( !defined $current_name || $current_name =~ /^__ANON__/ ) {
81         my $full_method_name = ( $package_name . '::' . $method_name );
82         subname( $full_method_name => $body );
83     }
84
85     $self->add_package_symbol("&$method_name", $body);
86
87     # if we added the actual method object to the method map, we're still valid
88     $self->update_package_cache_flag
89         if blessed($method);
90 }
91
92 sub _code_is_mine {
93     my ( $self, $code ) = @_;
94
95     my ( $code_package, $code_name ) = Class::MOP::get_code_info($code);
96
97     return $code_package && $code_package eq $self->name
98         || ( $code_package eq 'constant' && $code_name eq '__ANON__' );
99 }
100
101 sub has_method {
102     my ( $self, $method_name ) = @_;
103
104     ( defined $method_name && length $method_name )
105         || confess "You must define a method name";
106
107     my $method = $self->_get_maybe_raw_method($method_name)
108         or return;
109
110     return defined($self->_method_map->{$method_name} = $method);
111 }
112
113 sub get_method {
114     my ( $self, $method_name ) = @_;
115
116     ( defined $method_name && length $method_name )
117         || confess "You must define a method name";
118
119     my $method = $self->_get_maybe_raw_method($method_name)
120         or return;
121
122     return $method if blessed $method;
123
124     return $self->_method_map->{$method_name} = $self->wrap_method_body(
125         body                 => $method,
126         name                 => $method_name,
127         associated_metaclass => $self,
128     );
129 }
130
131 sub _get_maybe_raw_method {
132     my ( $self, $method_name ) = @_;
133
134     my $map_entry = $self->_method_map->{$method_name};
135     return $map_entry if defined $map_entry;
136
137     my $code = $self->get_package_symbol("&$method_name");
138
139     unless ($map_entry) {
140         return unless $code && $self->_code_is_mine($code);
141     }
142
143     return $code;
144 }
145
146 sub remove_method {
147     my ( $self, $method_name ) = @_;
148     ( defined $method_name && length $method_name )
149         || confess "You must define a method name";
150
151     my $removed_method = delete $self->_method_map->{$method_name};
152
153     $self->remove_package_symbol("&$method_name");
154
155     $removed_method->detach_from_class
156         if $removed_method && blessed $removed_method;
157
158     # still valid, since we just removed the method from the map
159     $self->update_package_cache_flag;
160
161     return $removed_method;
162 }
163
164 sub get_method_list {
165     my $self = shift;
166
167     return keys %{ $self->_full_method_map };
168 }
169
170 # This should probably be what get_method_list actually does, instead of just
171 # returning names. This was created as a much faster alternative to
172 # $meta->get_method($_) for $meta->get_method_list
173 sub _get_local_methods {
174     my $self = shift;
175
176     return values %{ $self->_full_method_map };
177 }
178
179 sub _restore_metamethods_from {
180     my $self = shift;
181     my ($old_meta) = @_;
182
183     for my $method ($old_meta->_get_local_methods) {
184         $method->_make_compatible_with($self->method_metaclass);
185         $self->add_method($method->name => $method);
186     }
187 }
188
189 sub reset_package_cache_flag  { (shift)->{'_package_cache_flag'} = undef }
190 sub update_package_cache_flag {
191     my $self = shift;
192     # NOTE:
193     # we can manually update the cache number
194     # since we are actually adding the method
195     # to our cache as well. This avoids us
196     # having to regenerate the method_map.
197     # - SL
198     $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
199 }
200
201 sub _full_method_map {
202     my $self = shift;
203
204     my $pkg_gen = Class::MOP::check_package_cache_flag($self->name);
205
206     if (($self->{_package_cache_flag_full} || -1) != $pkg_gen) {
207         # forcibly reify all method map entries
208         $self->get_method($_)
209             for $self->list_all_package_symbols('CODE');
210         $self->{_package_cache_flag_full} = $pkg_gen;
211     }
212
213     return $self->_method_map;
214 }
215
216 1;
217
218 __END__
219
220 =pod
221
222 =head1 NAME
223
224 Class::MOP::Mixin::HasMethods - Methods for metaclasses which have methods
225
226 =head1 DESCRIPTION
227
228 This class implements methods for metaclasses which have methods
229 (L<Class::MOP::Package> and L<Moose::Meta::Role>). See L<Class::MOP::Package>
230 for API details.
231
232 =head1 AUTHORS
233
234 Dave Rolsky E<lt>autarch@urth.orgE<gt>
235
236 =head1 COPYRIGHT AND LICENSE
237
238 Copyright 2006-2010 by Infinity Interactive, Inc.
239
240 L<http://www.iinteractive.com>
241
242 This library is free software; you can redistribute it and/or modify
243 it under the same terms as Perl itself.
244
245 =cut