Remove old accessors that are commented out
[gitmo/Class-MOP.git] / lib / Class / MOP / Method.pm
1
2 package Class::MOP::Method;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'weaken', 'reftype', 'blessed';
9
10 our $VERSION   = '0.92';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 use base 'Class::MOP::Object';
15
16 # NOTE:
17 # if poked in the right way,
18 # they should act like CODE refs.
19 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
20
21 # construction
22
23 sub wrap {
24     my ( $class, @args ) = @_;
25
26     unshift @args, 'body' if @args % 2 == 1;
27
28     my %params = @args;
29     my $code = $params{body};
30
31     if (blessed($code) && $code->isa(__PACKAGE__)) {
32         my $method = $code->clone;
33         delete $params{body};
34         Class::MOP::class_of($class)->rebless_instance($method, %params);
35         return $method;
36     }
37     elsif (!ref $code || 'CODE' ne reftype($code)) {
38         confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
39     }
40
41     ($params{package_name} && $params{name})
42         || confess "You must supply the package_name and name parameters";
43
44     my $self = $class->_new(\%params);
45
46     weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
47
48     return $self;
49 }
50
51 sub _new {
52     my $class = shift;
53
54     return Class::MOP::Class->initialize($class)->new_object(@_)
55         if $class ne __PACKAGE__;
56
57     my $params = @_ == 1 ? $_[0] : {@_};
58
59     return bless {
60         'body'                 => $params->{body},
61         'associated_metaclass' => $params->{associated_metaclass},
62         'package_name'         => $params->{package_name},
63         'name'                 => $params->{name},
64         'original_method'      => $params->{original_method},
65     } => $class;
66 }
67
68 sub attach_to_class {
69     my ( $self, $class ) = @_;
70     $self->{associated_metaclass} = $class;
71     weaken($self->{associated_metaclass});
72 }
73
74 sub detach_from_class {
75     my $self = shift;
76     delete $self->{associated_metaclass};
77 }
78
79 sub fully_qualified_name {
80     my $self = shift;
81     $self->package_name . '::' . $self->name;
82 }
83
84
85 # It's possible that this could cause a loop if there is a circular
86 # reference in here. That shouldn't ever happen in normal
87 # circumstances, since original method only gets set when clone is
88 # called. We _could_ check for such a loop, but it'd involve some sort
89 # of package-lexical variable, and wouldn't be terribly subclassable.
90 sub original_package_name {
91     my $self = shift;
92
93     $self->original_method
94         ? $self->original_method->original_package_name
95         : $self->package_name;
96 }
97
98 sub original_name {
99     my $self = shift;
100
101     $self->original_method
102         ? $self->original_method->original_name
103         : $self->name;
104 }
105
106 sub original_fully_qualified_name {
107     my $self = shift;
108
109     $self->original_method
110         ? $self->original_method->original_fully_qualified_name
111         : $self->fully_qualified_name;
112 }
113
114 sub execute {
115     my $self = shift;
116     $self->body->(@_);
117 }
118
119 # NOTE:
120 # the Class::MOP bootstrap
121 # will create this for us
122 # - SL
123 # sub clone { ... }
124
125 1;
126
127 __END__
128
129 =pod
130
131 =head1 NAME
132
133 Class::MOP::Method - Method Meta Object
134
135 =head1 DESCRIPTION
136
137 The Method Protocol is very small, since methods in Perl 5 are just
138 subroutines in a specific package. We provide a very basic
139 introspection interface.
140
141 =head1 METHODS
142
143 =over 4
144
145 =item B<< Class::MOP::Method->wrap($code, %options) >>
146
147 This is the constructor. It accepts a method body in the form of
148 either a code reference or a L<Class::MOP::Method> instance, followed
149 by a hash of options.
150
151 The options are:
152
153 =over 8
154
155 =item * name
156
157 The method name (without a package name). This is required if C<$code>
158 is a coderef.
159
160 =item * package_name
161
162 The package name for the method. This is required if C<$code> is a
163 coderef.
164
165 =item * associated_metaclass
166
167 An optional L<Class::MOP::Class> object. This is the metaclass for the
168 method's class.
169
170 =back
171
172 =item B<< $metamethod->clone(%params) >>
173
174 This makes a shallow clone of the method object. In particular,
175 subroutine reference itself is shared between all clones of a given
176 method.
177
178 When a method is cloned, the original method object will be available
179 by calling C<original_method> on the clone.
180
181 =item B<< $metamethod->body >>
182
183 This returns a reference to the method's subroutine.
184
185 =item B<< $metamethod->name >>
186
187 This returns the method's name
188
189 =item B<< $metamethod->package_name >>
190
191 This returns the method's package name.
192
193 =item B<< $metamethod->fully_qualified_name >>
194
195 This returns the method's fully qualified name (package name and
196 method name).
197
198 =item B<< $metamethod->associated_metaclass >>
199
200 This returns the L<Class::MOP::Class> object for the method, if one
201 exists.
202
203 =item B<< $metamethod->original_method >>
204
205 If this method object was created as a clone of some other method
206 object, this returns the object that was cloned.
207
208 =item B<< $metamethod->original_name >>
209
210 This returns the method's original name, wherever it was first
211 defined.
212
213 If this method is a clone of a clone (of a clone, etc.), this method
214 returns the name from the I<first> method in the chain of clones.
215
216 =item B<< $metamethod->original_package_name >>
217
218 This returns the method's original package name, wherever it was first
219 defined.
220
221 If this method is a clone of a clone (of a clone, etc.), this method
222 returns the package name from the I<first> method in the chain of
223 clones.
224
225 =item B<< $metamethod->original_fully_qualified_name >>
226
227 This returns the method's original fully qualified name, wherever it
228 was first defined.
229
230 If this method is a clone of a clone (of a clone, etc.), this method
231 returns the fully qualified name from the I<first> method in the chain
232 of clones.
233
234 =item B<< $metamethod->attach_to_class($metaclass) >>
235
236 Given a L<Class::MOP::Class> object, this method sets the associated
237 metaclass for the method. This will overwrite any existing associated
238 metaclass.
239
240 =item B<< $metamethod->detach_from_class >>
241
242 Removes any associated metaclass object for the method.
243
244 =item B<< $metamethod->execute(...) >>
245
246 This executes the method. Any arguments provided will be passed on to
247 the method itself.
248
249 =item B<< Class::MOP::Method->meta >>
250
251 This will return a L<Class::MOP::Class> instance for this class.
252
253 It should also be noted that L<Class::MOP> will actually bootstrap
254 this module by installing a number of attribute meta-objects into its
255 metaclass.
256
257 =back
258
259 =head1 AUTHORS
260
261 Stevan Little E<lt>stevan@iinteractive.comE<gt>
262
263 =head1 COPYRIGHT AND LICENSE
264
265 Copyright 2006-2009 by Infinity Interactive, Inc.
266
267 L<http://www.iinteractive.com>
268
269 This library is free software; you can redistribute it and/or modify
270 it under the same terms as Perl itself.
271
272 =cut
273