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