1 package Class::MOP::Mixin::HasMethods;
6 use Class::MOP::Method::Meta;
7 use Class::MOP::Method::Overload;
9 use Scalar::Util 'blessed';
11 use Sub::Name 'subname';
15 use base 'Class::MOP::Mixin';
17 sub _meta_method_class { 'Class::MOP::Method::Meta' }
19 sub _add_meta_method {
22 my $existing_method = $self->can('find_method_by_name')
23 ? $self->find_method_by_name($name)
24 : $self->get_method($name);
25 return if $existing_method
26 && $existing_method->isa($self->_meta_method_class);
28 $name => $self->_meta_method_class->wrap(
30 package_name => $self->name,
31 associated_metaclass => $self,
36 sub wrap_method_body {
37 my ( $self, %args ) = @_;
39 ( 'CODE' eq ref $args{body} )
40 || confess "Your code block must be a CODE reference";
42 $self->method_metaclass->wrap(
43 package_name => $self->name,
49 my ( $self, $method_name, $method ) = @_;
50 ( defined $method_name && length $method_name )
51 || confess "You must define a method name";
53 my $package_name = $self->name;
56 if ( blessed($method) ) {
57 $body = $method->body;
58 if ( $method->package_name ne $package_name ) {
59 $method = $method->clone(
60 package_name => $package_name,
65 $method->attach_to_class($self);
68 # If a raw code reference is supplied, its method object is not created.
69 # The method object won't be created until required.
73 $self->_method_map->{$method_name} = $method;
75 my ($current_package, $current_name) = Class::MOP::get_code_info($body);
77 subname($package_name . '::' . $method_name, $body)
78 unless defined $current_name && $current_name !~ /^__ANON__/;
80 $self->add_package_symbol("&$method_name", $body);
82 # we added the method to the method map too, so it's still valid
83 $self->update_package_cache_flag;
87 my ( $self, $code ) = @_;
89 my ( $code_package, $code_name ) = Class::MOP::get_code_info($code);
91 return ( $code_package && $code_package eq $self->name )
92 || ( $code_package eq 'constant' && $code_name eq '__ANON__' );
96 my ( $self, $method_name ) = @_;
98 ( defined $method_name && length $method_name )
99 || confess "You must define a method name";
101 my $method = $self->_get_maybe_raw_method($method_name)
104 return defined($self->_method_map->{$method_name} = $method);
108 my ( $self, $method_name ) = @_;
110 ( defined $method_name && length $method_name )
111 || confess "You must define a method name";
113 my $method = $self->_get_maybe_raw_method($method_name)
116 return $method if blessed $method;
118 return $self->_method_map->{$method_name} = $self->wrap_method_body(
120 name => $method_name,
121 associated_metaclass => $self,
125 sub _get_maybe_raw_method {
126 my ( $self, $method_name ) = @_;
128 my $map_entry = $self->_method_map->{$method_name};
129 return $map_entry if defined $map_entry;
131 my $code = $self->get_package_symbol("&$method_name");
133 return unless $code && $self->_code_is_mine($code);
139 my ( $self, $method_name ) = @_;
141 ( defined $method_name && length $method_name )
142 || confess "You must define a method name";
144 my $removed_method = delete $self->_method_map->{$method_name};
146 $self->remove_package_symbol("&$method_name");
148 $removed_method->detach_from_class
149 if blessed($removed_method);
151 # still valid, since we just removed the method from the map
152 $self->update_package_cache_flag;
154 return $removed_method;
157 sub get_method_list {
160 return keys %{ $self->_full_method_map };
163 sub _get_local_methods {
166 return values %{ $self->_full_method_map };
169 sub _restore_metamethods_from {
173 for my $method ($old_meta->_get_local_methods) {
174 $method->_make_compatible_with($self->method_metaclass);
175 $self->add_method($method->name => $method);
179 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
180 sub update_package_cache_flag {
183 # we can manually update the cache number
184 # since we are actually adding the method
185 # to our cache as well. This avoids us
186 # having to regenerate the method_map.
188 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
191 sub _full_method_map {
194 my $pkg_gen = Class::MOP::check_package_cache_flag($self->name);
196 if (($self->{_package_cache_flag_full} || -1) != $pkg_gen) {
197 # forcibly reify all method map entries
198 $self->get_method($_)
199 for $self->list_all_package_symbols('CODE');
200 $self->{_package_cache_flag_full} = $pkg_gen;
203 return $self->_method_map;
208 my $overload_operators;
209 sub overload_operators {
210 $overload_operators ||= [map { split /\s+/ } values %overload::ops];
211 return @$overload_operators;
216 return overload::Overloaded($self->name);
219 # XXX this could probably stand to be cached, but i figure it should be
220 # uncommon enough to not particularly matter
224 return {} unless $self->is_overloaded;
227 for my $op ($self->overload_operators) {
228 my $body = $self->_get_overloaded_operator_body($op);
229 next unless defined $body;
236 sub get_overload_list {
238 return keys %{ $self->_overload_map };
241 sub get_all_overloaded_operators {
243 my $map = $self->_overload_map;
244 return map { $self->_wrap_overload($_, $map->{$_}) } keys %$map;
247 sub has_overloaded_operator {
250 return defined $self->_get_overloaded_operator_body($op);
253 sub get_overloaded_operator {
256 my $body = $self->_get_overloaded_operator_body($op);
257 return unless defined $body;
258 return $self->_wrap_overload($op, $body);
261 sub add_overloaded_operator {
263 my ($op, $body) = @_;
264 $self->name->overload::OVERLOAD($op => $body);
267 sub remove_overloaded_operator {
270 # ugh, overload.pm provides no api for this
271 $self->get_or_add_package_symbol('%OVERLOAD')->{dummy}++;
272 $self->remove_package_symbol('&(' . $op);
275 sub _get_overloaded_operator_body {
278 return overload::Method($self->name, $op);
283 my ($op, $body) = @_;
284 return Class::MOP::Method::Overload->wrap(
286 package_name => $self->name,
287 associated_metaclass => $self,
294 # ABSTRACT: Methods for metaclasses which have methods
302 This class implements methods for metaclasses which have methods
303 (L<Class::MOP::Package> and L<Moose::Meta::Role>). See L<Class::MOP::Package>