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