141bfa71143053dc4e34edb1877d5fd623f40a09
[gitmo/Class-MOP.git] / lib / Class / MOP / Mixin / HasMethods.pm
1 package Class::MOP::Mixin::HasMethods;
2
3 use strict;
4 use warnings;
5
6 use Class::MOP::Method::Meta;
7
8 our $VERSION   = '1.11';
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 _meta_method_class       { 'Class::MOP::Method::Meta'             }
19
20 sub _add_meta_method {
21     my $self = shift;
22     my ($name) = @_;
23     my $existing_method = $self->can('find_method_by_name')
24                               ? $self->find_method_by_name($name)
25                               : $self->get_method($name);
26     return if $existing_method
27            && $existing_method->isa($self->_meta_method_class);
28     $self->add_method(
29         $name => $self->_meta_method_class->wrap(
30             name                 => $name,
31             package_name         => $self->name,
32             associated_metaclass => $self,
33         )
34     );
35 }
36
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
54     my $package_name = $self->name;
55
56     my $body;
57     if ( blessed($method) ) {
58         $body = $method->body;
59         if ( $method->package_name ne $package_name ) {
60             $method = $method->clone(
61                 package_name => $package_name,
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__/ ) {
79         my $full_method_name = ( $package_name . '::' . $method_name );
80         subname( $full_method_name => $body );
81     }
82
83     $self->add_package_symbol("&$method_name", $body);
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);
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
105     my $method = $self->_get_maybe_raw_method($method_name)
106         or return;
107
108     return defined($self->_method_map->{$method_name} = $method);
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
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
132     my $map_entry = $self->_method_map->{$method_name};
133     return $map_entry if defined $map_entry;
134
135     my $code = $self->get_package_symbol("&$method_name");
136
137     unless ($map_entry) {
138         return unless $code && $self->_code_is_mine($code);
139     }
140
141     return $code;
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
149     my $removed_method = delete $self->_method_map->{$method_name};
150
151     $self->remove_package_symbol("&$method_name");
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;
164
165     return keys %{ $self->_full_method_map };
166 }
167
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
174     return values %{ $self->_full_method_map };
175 }
176
177 sub _restore_metamethods_from {
178     my $self = shift;
179     my ($old_meta) = @_;
180
181     for my $method ($old_meta->_get_local_methods) {
182         $method->_make_compatible_with($self->method_metaclass);
183         $self->add_method($method->name => $method);
184     }
185 }
186
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
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
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
236 Copyright 2006-2010 by Infinity Interactive, Inc.
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