1 package Class::MOP::Mixin::HasMethods;
6 use Class::MOP::Method::Meta;
9 $VERSION = eval $VERSION;
10 our $AUTHORITY = 'cpan:STEVAN';
12 use Scalar::Util 'blessed';
14 use Sub::Name 'subname';
16 use base 'Class::MOP::Mixin';
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' }
22 sub _add_meta_method {
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);
31 $name => $self->_meta_method_class->wrap(
33 package_name => $self->name,
34 associated_metaclass => $self,
39 sub wrap_method_body {
40 my ( $self, %args ) = @_;
42 ( 'CODE' eq ref $args{body} )
43 || confess "Your code block must be a CODE reference";
45 $self->method_metaclass->wrap(
46 package_name => $self->name,
52 my ( $self, $method_name, $method ) = @_;
53 ( defined $method_name && length $method_name )
54 || confess "You must define a method name";
56 my $package_name = $self->name;
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,
65 ) if $method->can('clone');
68 $method->attach_to_class($self);
71 # If a raw code reference is supplied, its method object is not created.
72 # The method object won't be created until required.
76 $self->_method_map->{$method_name} = $method;
78 my ( $current_package, $current_name ) = Class::MOP::get_code_info($body);
80 if ( !defined $current_name || $current_name =~ /^__ANON__/ ) {
81 my $full_method_name = ( $package_name . '::' . $method_name );
82 subname( $full_method_name => $body );
85 $self->add_package_symbol("&$method_name", $body);
89 my ( $self, $code ) = @_;
91 my ( $code_package, $code_name ) = Class::MOP::get_code_info($code);
93 return $code_package && $code_package eq $self->name
94 || ( $code_package eq 'constant' && $code_name eq '__ANON__' );
98 my ( $self, $method_name ) = @_;
100 ( defined $method_name && length $method_name )
101 || confess "You must define a method name";
103 my $method = $self->_get_maybe_raw_method($method_name)
106 return defined($self->_method_map->{$method_name} = $method);
110 my ( $self, $method_name ) = @_;
112 ( defined $method_name && length $method_name )
113 || confess "You must define a method name";
115 my $method = $self->_get_maybe_raw_method($method_name)
118 return $method if blessed $method;
120 return $self->_method_map->{$method_name} = $self->wrap_method_body(
122 name => $method_name,
123 associated_metaclass => $self,
127 sub _get_maybe_raw_method {
128 my ( $self, $method_name ) = @_;
130 my $map_entry = $self->_method_map->{$method_name};
131 return $map_entry if defined $map_entry;
133 my $code = $self->get_package_symbol("&$method_name");
135 unless ($map_entry) {
136 return unless $code && $self->_code_is_mine($code);
143 my ( $self, $method_name ) = @_;
144 ( defined $method_name && length $method_name )
145 || confess "You must define a method name";
147 my $removed_method = delete $self->_method_map->{$method_name};
149 $self->remove_package_symbol("&$method_name");
151 $removed_method->detach_from_class
152 if $removed_method && blessed $removed_method;
154 # still valid, since we just removed the method from the map
155 $self->update_package_cache_flag;
157 return $removed_method;
160 sub get_method_list {
163 return keys %{ $self->_full_method_map };
166 # This should probably be what get_method_list actually does, instead of just
167 # returning names. This was created as a much faster alternative to
168 # $meta->get_method($_) for $meta->get_method_list
169 sub _get_local_methods {
172 return values %{ $self->_full_method_map };
175 sub _restore_metamethods_from {
179 for my $method ($old_meta->_get_local_methods) {
180 $method->_make_compatible_with($self->method_metaclass);
181 $self->add_method($method->name => $method);
185 sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
186 sub update_package_cache_flag {
189 # we can manually update the cache number
190 # since we are actually adding the method
191 # to our cache as well. This avoids us
192 # having to regenerate the method_map.
194 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
197 sub _full_method_map {
200 my $pkg_gen = Class::MOP::check_package_cache_flag($self->name);
202 if (($self->{_package_cache_flag_full} || -1) != $pkg_gen) {
203 # forcibly reify all method map entries
204 $self->get_method($_)
205 for $self->list_all_package_symbols('CODE');
206 $self->{_package_cache_flag_full} = $pkg_gen;
209 return $self->_method_map;
220 Class::MOP::Mixin::HasMethods - Methods for metaclasses which have methods
224 This class implements methods for metaclasses which have methods
225 (L<Class::MOP::Package> and L<Moose::Meta::Role>). See L<Class::MOP::Package>
230 Dave Rolsky E<lt>autarch@urth.orgE<gt>
232 =head1 COPYRIGHT AND LICENSE
234 Copyright 2006-2010 by Infinity Interactive, Inc.
236 L<http://www.iinteractive.com>
238 This library is free software; you can redistribute it and/or modify
239 it under the same terms as Perl itself.