Commit | Line | Data |
9b871d79 |
1 | package Class::MOP::Mixin::HasMethods; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
f1af2436 |
6 | use Class::MOP::Method::Meta; |
7 | |
81b5e774 |
8 | our $VERSION = '1.09'; |
9b871d79 |
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'} } |
f1af2436 |
20 | sub _meta_method_class { 'Class::MOP::Method::Meta' } |
21 | |
22 | sub _add_meta_method { |
23 | my $self = shift; |
37a46507 |
24 | my ($name) = @_; |
f1af2436 |
25 | my $existing_method = $self->can('find_method_by_name') |
37a46507 |
26 | ? $self->find_method_by_name($name) |
27 | : $self->get_method($name); |
f1af2436 |
28 | return if $existing_method |
29 | && $existing_method->isa($self->_meta_method_class); |
30 | $self->add_method( |
37a46507 |
31 | $name => $self->_meta_method_class->wrap( |
32 | name => $name, |
f1af2436 |
33 | package_name => $self->name, |
34 | associated_metaclass => $self, |
35 | ) |
36 | ); |
37 | } |
9b871d79 |
38 | |
39 | # This doesn't always get initialized in a constructor because there is a |
40 | # weird object construction path for subclasses of Class::MOP::Class. At one |
41 | # point, this always got initialized by calling into the XS code first, but |
42 | # that is no longer guaranteed to happen. |
43 | sub _method_map { $_[0]->{'methods'} ||= {} } |
44 | |
45 | sub wrap_method_body { |
46 | my ( $self, %args ) = @_; |
47 | |
48 | ( 'CODE' eq ref $args{body} ) |
49 | || confess "Your code block must be a CODE reference"; |
50 | |
51 | $self->method_metaclass->wrap( |
52 | package_name => $self->name, |
53 | %args, |
54 | ); |
55 | } |
56 | |
57 | sub add_method { |
58 | my ( $self, $method_name, $method ) = @_; |
59 | ( defined $method_name && length $method_name ) |
60 | || confess "You must define a method name"; |
61 | |
ef8d6eae |
62 | my $package_name = $self->name; |
63 | |
9b871d79 |
64 | my $body; |
65 | if ( blessed($method) ) { |
66 | $body = $method->body; |
ef8d6eae |
67 | if ( $method->package_name ne $package_name ) { |
9b871d79 |
68 | $method = $method->clone( |
ef8d6eae |
69 | package_name => $package_name, |
9b871d79 |
70 | name => $method_name, |
71 | ) if $method->can('clone'); |
72 | } |
73 | |
74 | $method->attach_to_class($self); |
75 | } |
76 | else { |
77 | # If a raw code reference is supplied, its method object is not created. |
78 | # The method object won't be created until required. |
79 | $body = $method; |
80 | } |
81 | |
82 | $self->_method_map->{$method_name} = $method; |
83 | |
84 | my ( $current_package, $current_name ) = Class::MOP::get_code_info($body); |
85 | |
86 | if ( !defined $current_name || $current_name =~ /^__ANON__/ ) { |
ef8d6eae |
87 | my $full_method_name = ( $package_name . '::' . $method_name ); |
9b871d79 |
88 | subname( $full_method_name => $body ); |
89 | } |
90 | |
91 | $self->add_package_symbol( |
92 | { sigil => '&', type => 'CODE', name => $method_name }, |
93 | $body, |
94 | ); |
95 | } |
96 | |
97 | sub _code_is_mine { |
98 | my ( $self, $code ) = @_; |
99 | |
100 | my ( $code_package, $code_name ) = Class::MOP::get_code_info($code); |
101 | |
102 | return $code_package && $code_package eq $self->name |
103 | || ( $code_package eq 'constant' && $code_name eq '__ANON__' ); |
104 | } |
105 | |
106 | sub has_method { |
107 | my ( $self, $method_name ) = @_; |
108 | |
109 | ( defined $method_name && length $method_name ) |
110 | || confess "You must define a method name"; |
111 | |
1865cd32 |
112 | return defined( $self->_get_maybe_raw_method($method_name) ); |
9b871d79 |
113 | } |
114 | |
115 | sub get_method { |
116 | my ( $self, $method_name ) = @_; |
117 | |
118 | ( defined $method_name && length $method_name ) |
119 | || confess "You must define a method name"; |
120 | |
1865cd32 |
121 | my $method = $self->_get_maybe_raw_method($method_name) |
122 | or return; |
123 | |
124 | return $method if blessed $method; |
125 | |
126 | return $self->_method_map->{$method_name} = $self->wrap_method_body( |
127 | body => $method, |
128 | name => $method_name, |
129 | associated_metaclass => $self, |
130 | ); |
131 | } |
132 | |
133 | sub _get_maybe_raw_method { |
134 | my ( $self, $method_name ) = @_; |
135 | |
9b871d79 |
136 | my $method_map = $self->_method_map; |
137 | my $map_entry = $method_map->{$method_name}; |
138 | my $code = $self->get_package_symbol( |
139 | { |
140 | name => $method_name, |
141 | sigil => '&', |
142 | type => 'CODE', |
143 | } |
144 | ); |
145 | |
736bf1f0 |
146 | # The !$code case seems to happen in some weird cases where methods |
147 | # modifiers are added via roles or some other such bizareness. Honestly, I |
148 | # don't totally understand this, but returning the entry works, and keeps |
149 | # various MX modules from blowing up. - DR |
6038ad58 |
150 | return $map_entry |
151 | if blessed $map_entry && ( !$code || $map_entry->body == $code ); |
9b871d79 |
152 | |
153 | unless ($map_entry) { |
154 | return unless $code && $self->_code_is_mine($code); |
155 | } |
156 | |
1865cd32 |
157 | return $code; |
9b871d79 |
158 | } |
159 | |
160 | sub remove_method { |
161 | my ( $self, $method_name ) = @_; |
162 | ( defined $method_name && length $method_name ) |
163 | || confess "You must define a method name"; |
164 | |
165 | my $removed_method = delete $self->_full_method_map->{$method_name}; |
166 | |
167 | $self->remove_package_symbol( |
168 | { sigil => '&', type => 'CODE', name => $method_name } ); |
169 | |
170 | $removed_method->detach_from_class |
171 | if $removed_method && blessed $removed_method; |
172 | |
173 | # still valid, since we just removed the method from the map |
174 | $self->update_package_cache_flag; |
175 | |
176 | return $removed_method; |
177 | } |
178 | |
179 | sub get_method_list { |
180 | my $self = shift; |
ea6dca17 |
181 | |
182 | my $namespace = $self->namespace; |
183 | |
1a36fc73 |
184 | # Constants may show up as some sort of non-GLOB reference in the |
185 | # namespace hash ref, depending on the Perl version. |
7f9ef61e |
186 | return grep { |
08d23070 |
187 | defined $namespace->{$_} |
188 | && ( ref( \$namespace->{$_} ) ne 'GLOB' |
189 | || *{ $namespace->{$_} }{CODE} ) |
7f9ef61e |
190 | && $self->has_method($_) |
191 | } |
ea6dca17 |
192 | keys %{$namespace}; |
9b871d79 |
193 | } |
194 | |
fe1b970a |
195 | # This should probably be what get_method_list actually does, instead of just |
196 | # returning names. This was created as a much faster alternative to |
197 | # $meta->get_method($_) for $meta->get_method_list |
198 | sub _get_local_methods { |
199 | my $self = shift; |
200 | |
201 | my $namespace = $self->namespace; |
202 | |
7f9ef61e |
203 | return map { $self->get_method($_) } |
0517ea60 |
204 | grep { |
205 | defined $namespace->{$_} |
206 | && ( ref $namespace->{$_} |
207 | || *{ $namespace->{$_} }{CODE} ) |
208 | } |
fe1b970a |
209 | keys %{$namespace}; |
210 | } |
211 | |
78902d1d |
212 | sub _restore_metamethods_from { |
213 | my $self = shift; |
214 | my ($old_meta) = @_; |
215 | |
216 | for my $method ($old_meta->_get_local_methods) { |
80076f1e |
217 | # XXX: this is pretty gross. the issue here is that |
218 | # CMOP::Method::Wrapped objects are subclasses of CMOP::Method, but |
219 | # when we get to moose, they'll need to be compatible with |
220 | # Moose::Meta::Method, which isn't possible. the right solution here is |
221 | # to make ::Wrapped into a role that gets applied to whatever the |
222 | # method_metaclass happens to be and get rid of |
223 | # wrapped_method_metaclass entirely, but that's not going to happen |
224 | # until we ditch cmop and get roles into the bootstrapping, so. |
225 | # i'm not maintaining the previous behavior of turning them into |
226 | # instances of the new method_metaclass because that's equally broken, |
227 | # and at least this way any issues will at least be detectable and |
228 | # potentially fixable. -doy |
bc2c41b4 |
229 | if (!$method->isa($self->wrapped_method_metaclass) |
230 | # and the same issue occurs with CMOP::Method::Meta objects/: |
231 | && !$method->isa($self->_meta_method_class)) { |
80076f1e |
232 | $method->_make_compatible_with($self->method_metaclass); |
233 | } |
78902d1d |
234 | $self->add_method($method->name => $method); |
235 | } |
236 | } |
237 | |
9b871d79 |
238 | 1; |
239 | |
240 | __END__ |
241 | |
242 | =pod |
243 | |
244 | =head1 NAME |
245 | |
246 | Class::MOP::Mixin::HasMethods - Methods for metaclasses which have methods |
247 | |
248 | =head1 DESCRIPTION |
249 | |
250 | This class implements methods for metaclasses which have methods |
251 | (L<Class::MOP::Package> and L<Moose::Meta::Role>). See L<Class::MOP::Package> |
252 | for API details. |
253 | |
254 | =head1 AUTHORS |
255 | |
256 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
257 | |
258 | =head1 COPYRIGHT AND LICENSE |
259 | |
3e2c8600 |
260 | Copyright 2006-2010 by Infinity Interactive, Inc. |
9b871d79 |
261 | |
262 | L<http://www.iinteractive.com> |
263 | |
264 | This library is free software; you can redistribute it and/or modify |
265 | it under the same terms as Perl itself. |
266 | |
267 | =cut |