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