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