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