bump version and update Changes for a release
[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';
9
10 our $VERSION   = '0.78';
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 our $UPGRADE_ERROR_TEXT = q{
22 ---------------------------------------------------------
23 NOTE: this error is likely not an error, but a regression
24 caused by the latest upgrade to Moose/Class::MOP. Consider
25 upgrading any MooseX::* modules to their latest versions
26 before spending too much time chasing this one down.
27 ---------------------------------------------------------
28 };
29
30 # construction
31
32 sub wrap {
33     my ( $class, @args ) = @_;
34
35     unshift @args, 'body' if @args % 2 == 1;
36
37     my %params = @args;
38     my $code = $params{body};
39
40     ('CODE' eq ref($code))
41         || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
42
43     ($params{package_name} && $params{name})
44         || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
45
46     my $self = $class->_new(\%params);
47
48     weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
49
50     return $self;
51 }
52
53 sub _new {
54     my $class = shift;
55     my $params = @_ == 1 ? $_[0] : {@_};
56
57     my $self = bless {
58         'body'                 => $params->{body},
59         'associated_metaclass' => $params->{associated_metaclass},
60         'package_name'         => $params->{package_name},
61         'name'                 => $params->{name},
62     } => $class;
63 }
64
65 ## accessors
66
67 sub associated_metaclass { shift->{'associated_metaclass'} }
68
69 sub attach_to_class {
70     my ( $self, $class ) = @_;
71     $self->{associated_metaclass} = $class;
72     weaken($self->{associated_metaclass});
73 }
74
75 sub detach_from_class {
76     my $self = shift;
77     delete $self->{associated_metaclass};
78 }
79
80 sub fully_qualified_name {
81     my $self = shift;
82     $self->package_name . '::' . $self->name;
83 }
84
85 sub original_method { (shift)->{'original_method'} }
86
87 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
88
89 # It's possible that this could cause a loop if there is a circular
90 # reference in here. That shouldn't ever happen in normal
91 # circumstances, since original method only gets set when clone is
92 # called. We _could_ check for such a loop, but it'd involve some sort
93 # of package-lexical variable, and wouldn't be terribly subclassable.
94 sub original_package_name {
95     my $self = shift;
96
97     $self->original_method
98         ? $self->original_method->original_package_name
99         : $self->package_name;
100 }
101
102 sub original_name {
103     my $self = shift;
104
105     $self->original_method
106         ? $self->original_method->original_name
107         : $self->name;
108 }
109
110 sub original_fully_qualified_name {
111     my $self = shift;
112
113     $self->original_method
114         ? $self->original_method->original_fully_qualified_name
115         : $self->fully_qualified_name;
116 }
117
118 sub execute {
119     my $self = shift;
120     $self->body->(@_);
121 }
122
123 # NOTE:
124 # the Class::MOP bootstrap
125 # will create this for us
126 # - SL
127 # sub clone { ... }
128
129 1;
130
131 __END__
132
133 =pod
134
135 =head1 NAME
136
137 Class::MOP::Method - Method Meta Object
138
139 =head1 DESCRIPTION
140
141 The Method Protocol is very small, since methods in Perl 5 are just
142 subroutines within the particular package. We provide a very basic
143 introspection interface.
144
145 =head1 METHODS
146
147 =head2 Introspection
148
149 =over 4
150
151 =item B<meta>
152
153 This will return a B<Class::MOP::Class> instance which is related
154 to this class.
155
156 =back
157
158 =head2 Construction
159
160 =over 4
161
162 =item B<wrap ($code, %params)>
163
164 This is the basic constructor, it returns a B<Class::MOP::Method>
165 instance which wraps the given C<$code> reference. You must also set
166 the C<package_name> and C<name> attributes in C<%params>.
167
168 =item B<clone (%params)>
169
170 This will make a copy of the object, allowing you to override
171 any values by stuffing them in C<%params>.
172
173 =back
174
175 =head2 Informational
176
177 =over 4
178
179 =item B<body>
180
181 This returns the actual CODE reference of the particular instance.
182
183 =item B<name>
184
185 This returns the name of the CODE reference.
186
187 =item B<associated_metaclass>
188
189 The metaclass of the method
190
191 =item B<package_name>
192
193 This returns the package name that the CODE reference is attached to.
194
195 =item B<fully_qualified_name>
196
197 This returns the fully qualified name of the CODE reference.
198
199 =item B<original_method>
200
201 If this method object was created as a clone of some other method
202 object, this returns the object that was cloned.
203
204 =item B<original_name>
205
206 This returns the original name of the CODE reference, wherever it was
207 first defined.
208
209 If this method is a clone of a clone (of a clone, etc.), this method
210 returns the name from the I<first> method in the chain of clones.
211
212 =item B<original_package_name>
213
214 This returns the original package name that the CODE reference is
215 attached to, wherever it was first defined.
216
217 If this method is a clone of a clone (of a clone, etc.), this method
218 returns the package name from the I<first> method in the chain of
219 clones.
220
221 =item B<original_fully_qualified_name>
222
223 This returns the original fully qualified name of the CODE reference,
224 wherever it was first defined.
225
226 If this method is a clone of a clone (of a clone, etc.), this method
227 returns the fully qualified name from the I<first> method in the chain
228 of clones.
229
230 =back
231
232 =head2 Metaclass
233
234 =over 4
235
236 =item B<attach_to_class>
237
238 Sets the associated metaclass
239
240 =item B<detach_from_class>
241
242 Disassociates the method from the metaclass
243
244 =back
245
246 =head2 Miscellaneous
247
248 =over 4
249
250 =item B<execute>
251
252 Executes the method. Be sure to pass in the instance, since the
253 method expects it.
254
255 =back
256
257 =head1 AUTHORS
258
259 Stevan Little E<lt>stevan@iinteractive.comE<gt>
260
261 =head1 COPYRIGHT AND LICENSE
262
263 Copyright 2006-2008 by Infinity Interactive, Inc.
264
265 L<http://www.iinteractive.com>
266
267 This library is free software; you can redistribute it and/or modify
268 it under the same terms as Perl itself.
269
270 =cut
271