37af5a1ae869a8767026bef5e05173937c36ae53
[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 $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 = (ref($class) || $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 body { (shift)->{'body'} }
68
69 sub associated_metaclass { shift->{'associated_metaclass'} }
70
71 sub attach_to_class {
72     my ( $self, $class ) = @_;
73     $self->{associated_metaclass} = $class;
74     weaken($self->{associated_metaclass});
75 }
76
77 sub detach_from_class {
78     my $self = shift;
79     delete $self->{associated_metaclass};
80 }
81
82 sub package_name { (shift)->{'package_name'} }
83
84 sub name { (shift)->{'name'} }
85
86 sub fully_qualified_name {
87     my $self = shift;
88     $self->package_name . '::' . $self->name;
89 }
90
91 # NOTE:
92 # the Class::MOP bootstrap
93 # will create this for us
94 # - SL
95 # sub clone { ... }
96
97 1;
98
99 __END__
100
101 =pod
102
103 =head1 NAME
104
105 Class::MOP::Method - Method Meta Object
106
107 =head1 DESCRIPTION
108
109 The Method Protocol is very small, since methods in Perl 5 are just
110 subroutines within the particular package. We provide a very basic
111 introspection interface.
112
113 =head1 METHODS
114
115 =head2 Introspection
116
117 =over 4
118
119 =item B<meta>
120
121 This will return a B<Class::MOP::Class> instance which is related
122 to this class.
123
124 =back
125
126 =head2 Construction
127
128 =over 4
129
130 =item B<wrap ($code, %params)>
131
132 This is the basic constructor, it returns a B<Class::MOP::Method>
133 instance which wraps the given C<$code> reference. You can also
134 set the C<package_name> and C<name> attributes using the C<%params>.
135 If these are not set, then thier accessors will attempt to figure
136 it out using the C<Class::MOP::get_code_info> function.
137
138 =item B<clone (%params)>
139
140 This will make a copy of the object, allowing you to override
141 any values by stuffing them in C<%params>.
142
143 =back
144
145 =head2 Informational
146
147 =over 4
148
149 =item B<body>
150
151 This returns the actual CODE reference of the particular instance.
152
153 =item B<name>
154
155 This returns the name of the CODE reference.
156
157 =item B<associated_metaclass>
158
159 The metaclass of the method
160
161 =item B<package_name>
162
163 This returns the package name that the CODE reference is attached to.
164
165 =item B<fully_qualified_name>
166
167 This returns the fully qualified name of the CODE reference.
168
169 =back
170
171 =head2 Metaclass
172
173 =over 4
174
175 =item B<attach_to_class>
176
177 Sets the associated metaclass
178
179 =item B<detach_from_class>
180
181 Disassociates the method from the metaclass
182
183 =back
184
185 =head1 AUTHORS
186
187 Stevan Little E<lt>stevan@iinteractive.comE<gt>
188
189 =head1 COPYRIGHT AND LICENSE
190
191 Copyright 2006-2008 by Infinity Interactive, Inc.
192
193 L<http://www.iinteractive.com>
194
195 This library is free software; you can redistribute it and/or modify
196 it under the same terms as Perl itself.
197
198 =cut
199