add associated_metaclass to Method
[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.65';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Object';
14
15 # NOTE:
16 # if poked in the right way,
17 # they should act like CODE refs.
18 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
19
20 our $UPGRADE_ERROR_TEXT = q{
21 ---------------------------------------------------------
22 NOTE: this error is likely not an error, but a regression
23 caused by the latest upgrade to Moose/Class::MOP. Consider
24 upgrading any MooseX::* modules to their latest versions
25 before spending too much time chasing this one down.
26 ---------------------------------------------------------
27 };
28
29 # construction
30
31 sub wrap {
32     my ( $class, @args ) = @_;
33
34     unshift @args, 'body' if @args % 2 == 1;
35
36     my %params = @args;
37     my $code = $params{body};
38
39     ('CODE' eq ref($code))
40         || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
41
42     ($params{package_name} && $params{name})
43         || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
44
45     my $self = bless {
46         'body'                 => $code,
47         'associated_metaclass' => $params{associated_metaclass},
48         'package_name'         => $params{package_name},
49         'name'                 => $params{name},
50     } => ref($class) || $class;
51
52     weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
53
54     return $self;
55 }
56
57 ## accessors
58
59 sub body { (shift)->{'body'} }
60
61 sub associated_metaclass { shift->{'associated_metaclass'} }
62
63 sub attach_to_class {
64     my ( $self, $class ) = @_;
65     $self->{associated_metaclass} = $class;
66     weaken($self->{associated_metaclass});
67 }
68
69 sub detach_from_class {
70     my $self = shift;
71     delete $self->{associated_metaclass};
72 }
73
74 sub package_name {
75     my $self = shift;
76     $self->{'package_name'} ||= (Class::MOP::get_code_info($self->body))[0];
77 }
78
79 sub name {
80     my $self = shift;
81     $self->{'name'} ||= (Class::MOP::get_code_info($self->body))[1];
82 }
83
84 sub fully_qualified_name {
85     my $code = shift;
86     $code->package_name . '::' . $code->name;
87 }
88
89 # NOTE:
90 # the Class::MOP bootstrap
91 # will create this for us
92 # - SL
93 # sub clone { ... }
94
95 1;
96
97 __END__
98
99 =pod
100
101 =head1 NAME
102
103 Class::MOP::Method - Method Meta Object
104
105 =head1 DESCRIPTION
106
107 The Method Protocol is very small, since methods in Perl 5 are just
108 subroutines within the particular package. We provide a very basic
109 introspection interface.
110
111 =head1 METHODS
112
113 =head2 Introspection
114
115 =over 4
116
117 =item B<meta>
118
119 This will return a B<Class::MOP::Class> instance which is related
120 to this class.
121
122 =back
123
124 =head2 Construction
125
126 =over 4
127
128 =item B<wrap ($code, %params)>
129
130 This is the basic constructor, it returns a B<Class::MOP::Method>
131 instance which wraps the given C<$code> reference. You can also
132 set the C<package_name> and C<name> attributes using the C<%params>.
133 If these are not set, then thier accessors will attempt to figure
134 it out using the C<Class::MOP::get_code_info> function.
135
136 =item B<clone (%params)>
137
138 This will make a copy of the object, allowing you to override
139 any values by stuffing them in C<%params>.
140
141 =back
142
143 =head2 Informational
144
145 =over 4
146
147 =item B<body>
148
149 This returns the actual CODE reference of the particular instance.
150
151 =item B<name>
152
153 This returns the name of the CODE reference.
154
155 =item B<associated_metaclass>
156
157 The metaclass of the method
158
159 =item B<package_name>
160
161 This returns the package name that the CODE reference is attached to.
162
163 =item B<fully_qualified_name>
164
165 This returns the fully qualified name of the CODE reference.
166
167 =back
168
169 =head2 Metaclass
170
171 =over 4
172
173 =item B<attach_to_class>
174
175 Sets the associated metaclass
176
177 =item B<detach_from_class>
178
179 Disassociates the method from the metaclass
180
181 =back
182
183 =head1 AUTHORS
184
185 Stevan Little E<lt>stevan@iinteractive.comE<gt>
186
187 =head1 COPYRIGHT AND LICENSE
188
189 Copyright 2006-2008 by Infinity Interactive, Inc.
190
191 L<http://www.iinteractive.com>
192
193 This library is free software; you can redistribute it and/or modify
194 it under the same terms as Perl itself.
195
196 =cut
197