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 | |
a9f48b4b |
8 | our $VERSION = '1.11'; |
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 | |
9b871d79 |
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 | |
ef8d6eae |
56 | my $package_name = $self->name; |
57 | |
9b871d79 |
58 | my $body; |
59 | if ( blessed($method) ) { |
60 | $body = $method->body; |
ef8d6eae |
61 | if ( $method->package_name ne $package_name ) { |
9b871d79 |
62 | $method = $method->clone( |
ef8d6eae |
63 | package_name => $package_name, |
9b871d79 |
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__/ ) { |
ef8d6eae |
81 | my $full_method_name = ( $package_name . '::' . $method_name ); |
9b871d79 |
82 | subname( $full_method_name => $body ); |
83 | } |
84 | |
85 | $self->add_package_symbol( |
86 | { sigil => '&', type => 'CODE', name => $method_name }, |
87 | $body, |
88 | ); |
89 | } |
90 | |
91 | sub _code_is_mine { |
92 | my ( $self, $code ) = @_; |
93 | |
94 | my ( $code_package, $code_name ) = Class::MOP::get_code_info($code); |
95 | |
96 | return $code_package && $code_package eq $self->name |
97 | || ( $code_package eq 'constant' && $code_name eq '__ANON__' ); |
98 | } |
99 | |
100 | sub has_method { |
101 | my ( $self, $method_name ) = @_; |
102 | |
103 | ( defined $method_name && length $method_name ) |
104 | || confess "You must define a method name"; |
105 | |
5018df3e |
106 | my $method = $self->_get_maybe_raw_method($method_name) |
107 | or return; |
108 | |
109 | return defined($self->_method_map->{$method_name} = $method); |
9b871d79 |
110 | } |
111 | |
112 | sub get_method { |
113 | my ( $self, $method_name ) = @_; |
114 | |
115 | ( defined $method_name && length $method_name ) |
116 | || confess "You must define a method name"; |
117 | |
1865cd32 |
118 | my $method = $self->_get_maybe_raw_method($method_name) |
119 | or return; |
120 | |
121 | return $method if blessed $method; |
122 | |
123 | return $self->_method_map->{$method_name} = $self->wrap_method_body( |
124 | body => $method, |
125 | name => $method_name, |
126 | associated_metaclass => $self, |
127 | ); |
128 | } |
129 | |
130 | sub _get_maybe_raw_method { |
131 | my ( $self, $method_name ) = @_; |
132 | |
67c6080e |
133 | my $map_entry = $self->_method_map->{$method_name}; |
5018df3e |
134 | return $map_entry if defined $map_entry; |
67c6080e |
135 | |
136 | my $code = $self->get_package_symbol( |
9b871d79 |
137 | { |
138 | name => $method_name, |
139 | sigil => '&', |
140 | type => 'CODE', |
141 | } |
142 | ); |
143 | |
9b871d79 |
144 | unless ($map_entry) { |
145 | return unless $code && $self->_code_is_mine($code); |
146 | } |
147 | |
1865cd32 |
148 | return $code; |
9b871d79 |
149 | } |
150 | |
151 | sub remove_method { |
152 | my ( $self, $method_name ) = @_; |
153 | ( defined $method_name && length $method_name ) |
154 | || confess "You must define a method name"; |
155 | |
38bcde3f |
156 | my $removed_method = delete $self->_method_map->{$method_name}; |
9b871d79 |
157 | |
158 | $self->remove_package_symbol( |
159 | { sigil => '&', type => 'CODE', name => $method_name } ); |
160 | |
161 | $removed_method->detach_from_class |
162 | if $removed_method && blessed $removed_method; |
163 | |
164 | # still valid, since we just removed the method from the map |
165 | $self->update_package_cache_flag; |
166 | |
167 | return $removed_method; |
168 | } |
169 | |
170 | sub get_method_list { |
171 | my $self = shift; |
ea6dca17 |
172 | |
39e3013b |
173 | return keys %{ $self->_full_method_map }; |
9b871d79 |
174 | } |
175 | |
fe1b970a |
176 | # This should probably be what get_method_list actually does, instead of just |
177 | # returning names. This was created as a much faster alternative to |
178 | # $meta->get_method($_) for $meta->get_method_list |
179 | sub _get_local_methods { |
180 | my $self = shift; |
181 | |
39e3013b |
182 | return values %{ $self->_full_method_map }; |
fe1b970a |
183 | } |
184 | |
78902d1d |
185 | sub _restore_metamethods_from { |
186 | my $self = shift; |
187 | my ($old_meta) = @_; |
188 | |
189 | for my $method ($old_meta->_get_local_methods) { |
5281e1f9 |
190 | $method->_make_compatible_with($self->method_metaclass); |
78902d1d |
191 | $self->add_method($method->name => $method); |
192 | } |
193 | } |
194 | |
fd05ff5f |
195 | sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef } |
196 | sub update_package_cache_flag { |
197 | my $self = shift; |
198 | # NOTE: |
199 | # we can manually update the cache number |
200 | # since we are actually adding the method |
201 | # to our cache as well. This avoids us |
202 | # having to regenerate the method_map. |
203 | # - SL |
204 | $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name); |
205 | } |
206 | |
39e3013b |
207 | sub _full_method_map { |
208 | my $self = shift; |
209 | |
210 | my $pkg_gen = Class::MOP::check_package_cache_flag($self->name); |
211 | |
212 | if (($self->{_package_cache_flag_full} || -1) != $pkg_gen) { |
213 | # forcibly reify all method map entries |
214 | $self->get_method($_) |
215 | for $self->list_all_package_symbols('CODE'); |
216 | $self->{_package_cache_flag_full} = $pkg_gen; |
217 | } |
218 | |
219 | return $self->_method_map; |
220 | } |
221 | |
9b871d79 |
222 | 1; |
223 | |
224 | __END__ |
225 | |
226 | =pod |
227 | |
228 | =head1 NAME |
229 | |
230 | Class::MOP::Mixin::HasMethods - Methods for metaclasses which have methods |
231 | |
232 | =head1 DESCRIPTION |
233 | |
234 | This class implements methods for metaclasses which have methods |
235 | (L<Class::MOP::Package> and L<Moose::Meta::Role>). See L<Class::MOP::Package> |
236 | for API details. |
237 | |
238 | =head1 AUTHORS |
239 | |
240 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
241 | |
242 | =head1 COPYRIGHT AND LICENSE |
243 | |
3e2c8600 |
244 | Copyright 2006-2010 by Infinity Interactive, Inc. |
9b871d79 |
245 | |
246 | L<http://www.iinteractive.com> |
247 | |
248 | This library is free software; you can redistribute it and/or modify |
249 | it under the same terms as Perl itself. |
250 | |
251 | =cut |