clone and rebless when wrapping a method object
[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.90';
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 ## accessors
69
70 sub associated_metaclass { shift->{'associated_metaclass'} }
71
72 sub attach_to_class {
73     my ( $self, $class ) = @_;
74     $self->{associated_metaclass} = $class;
75     weaken($self->{associated_metaclass});
76 }
77
78 sub detach_from_class {
79     my $self = shift;
80     delete $self->{associated_metaclass};
81 }
82
83 sub fully_qualified_name {
84     my $self = shift;
85     $self->package_name . '::' . $self->name;
86 }
87
88 sub original_method { (shift)->{'original_method'} }
89
90 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
91
92 # It's possible that this could cause a loop if there is a circular
93 # reference in here. That shouldn't ever happen in normal
94 # circumstances, since original method only gets set when clone is
95 # called. We _could_ check for such a loop, but it'd involve some sort
96 # of package-lexical variable, and wouldn't be terribly subclassable.
97 sub original_package_name {
98     my $self = shift;
99
100     $self->original_method
101         ? $self->original_method->original_package_name
102         : $self->package_name;
103 }
104
105 sub original_name {
106     my $self = shift;
107
108     $self->original_method
109         ? $self->original_method->original_name
110         : $self->name;
111 }
112
113 sub original_fully_qualified_name {
114     my $self = shift;
115
116     $self->original_method
117         ? $self->original_method->original_fully_qualified_name
118         : $self->fully_qualified_name;
119 }
120
121 sub execute {
122     my $self = shift;
123     $self->body->(@_);
124 }
125
126 # NOTE:
127 # the Class::MOP bootstrap
128 # will create this for us
129 # - SL
130 # sub clone { ... }
131
132 1;
133
134 __END__
135
136 =pod
137
138 =head1 NAME
139
140 Class::MOP::Method - Method Meta Object
141
142 =head1 DESCRIPTION
143
144 The Method Protocol is very small, since methods in Perl 5 are just
145 subroutines in a specific package. We provide a very basic
146 introspection interface.
147
148 =head1 METHODS
149
150 =over 4
151
152 =item B<< Class::MOP::Method->wrap($code, %options) >>
153
154 This is the constructor. It accepts a subroutine reference and a hash
155 of options.
156
157 The options are:
158
159 =over 8
160
161 =item * name
162
163 The method name (without a package name). This is required.
164
165 =item * package_name
166
167 The package name for the method. This is required.
168
169 =item * associated_metaclass
170
171 An optional L<Class::MOP::Class> object. This is the metaclass for the
172 method's class.
173
174 =back
175
176 =item B<< $metamethod->clone(%params) >>
177
178 This makes a shallow clone of the method object. In particular,
179 subroutine reference itself is shared between all clones of a given
180 method.
181
182 When a method is cloned, the original method object will be available
183 by calling C<original_method> on the clone.
184
185 =item B<< $metamethod->body >>
186
187 This returns a reference to the method's subroutine.
188
189 =item B<< $metamethod->name >>
190
191 This returns the method's name
192
193 =item B<< $metamethod->package_name >>
194
195 This returns the method's package name.
196
197 =item B<< $metamethod->fully_qualified_name >>
198
199 This returns the method's fully qualified name (package name and
200 method name).
201
202 =item B<< $metamethod->associated_metaclass >>
203
204 This returns the L<Class::MOP::Class> object for the method, if one
205 exists.
206
207 =item B<< $metamethod->original_method >>
208
209 If this method object was created as a clone of some other method
210 object, this returns the object that was cloned.
211
212 =item B<< $metamethod->original_name >>
213
214 This returns the method's original name, wherever it was first
215 defined.
216
217 If this method is a clone of a clone (of a clone, etc.), this method
218 returns the name from the I<first> method in the chain of clones.
219
220 =item B<< $metamethod->original_package_name >>
221
222 This returns the method's original package name, wherever it was first
223 defined.
224
225 If this method is a clone of a clone (of a clone, etc.), this method
226 returns the package name from the I<first> method in the chain of
227 clones.
228
229 =item B<< $metamethod->original_fully_qualified_name >>
230
231 This returns the method's original fully qualified name, wherever it
232 was first defined.
233
234 If this method is a clone of a clone (of a clone, etc.), this method
235 returns the fully qualified name from the I<first> method in the chain
236 of clones.
237
238 =item B<< $metamethod->attach_to_class($metaclass) >>
239
240 Given a L<Class::MOP::Class> object, this method sets the associated
241 metaclass for the method. This will overwrite any existing associated
242 metaclass.
243
244 =item B<< $metamethod->detach_from_class >>
245
246 Removes any associated metaclass object for the method.
247
248 =item B<< $metamethod->execute(...) >>
249
250 This executes the method. Any arguments provided will be passed on to
251 the method itself.
252
253 =item B<< Class::MOP::Method->meta >>
254
255 This will return a L<Class::MOP::Class> instance for this class.
256
257 It should also be noted that L<Class::MOP> will actually bootstrap
258 this module by installing a number of attribute meta-objects into its
259 metaclass.
260
261 =back
262
263 =head1 AUTHORS
264
265 Stevan Little E<lt>stevan@iinteractive.comE<gt>
266
267 =head1 COPYRIGHT AND LICENSE
268
269 Copyright 2006-2009 by Infinity Interactive, Inc.
270
271 L<http://www.iinteractive.com>
272
273 This library is free software; you can redistribute it and/or modify
274 it under the same terms as Perl itself.
275
276 =cut
277