actually, why not make the meta method renameable
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Meta.pm
CommitLineData
59b51046 1
2package Class::MOP::Method::Meta;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10our $VERSION = '1.08';
11$VERSION = eval $VERSION;
12our $AUTHORITY = 'cpan:STEVAN';
13
14use constant DEBUG_NO_META => $ENV{DEBUG_NO_META};
15
16use base 'Class::MOP::Method';
17
28a82dda 18sub _is_caller_mop_internal {
19 my $self = shift;
20 my ($caller) = @_;
21 return $caller =~ /^(?:Class::MOP|metaclass)(?:::|$)/;
22}
23
59b51046 24sub _generate_meta_method {
25 my $method_self = shift;
26 my $metaclass = shift;
27 sub {
28 # this will be compiled out if the env var wasn't set
29 if (DEBUG_NO_META) {
59b51046 30 confess "'meta' method called by MOP internals"
28a82dda 31 # it's okay to call meta methods on metaclasses, since we
32 # explicitly ask for them
33 if !$_[0]->isa('Class::MOP::Object')
23ab51e4 34 && !$_[0]->isa('Class::MOP::Mixin')
28a82dda 35 # it's okay if the test itself calls ->meta, we only care about
36 # if the mop internals call ->meta
37 && $method_self->_is_caller_mop_internal(scalar caller);
59b51046 38 }
39 # we must re-initialize so that it
40 # works as expected in subclasses,
41 # since metaclass instances are
42 # singletons, this is not really a
43 # big deal anyway.
44 $metaclass->initialize(blessed($_[0]) || $_[0])
45 };
46}
47
48sub wrap {
49 my ($class, @args) = @_;
50
51 unshift @args, 'body' if @args % 2 == 1;
52 my %params = @args;
53 confess "Overriding the body of meta methods is not allowed"
54 if $params{body};
55
56 my $metaclass_class = $params{associated_metaclass}->meta;
57 $params{body} = $class->_generate_meta_method($metaclass_class);
58 return $class->SUPER::wrap(%params);
59}
60
611;
62
63__END__
64
65=pod
66
67=head1 NAME
68
69Class::MOP::Method::Meta - Method Meta Object for C<meta> methods
70
71=head1 DESCRIPTION
72
73This is a L<Class::MOP::Method> subclass which represents C<meta>
74methods installed into classes by Class::MOP.
75
76=head1 METHODS
77
78=over 4
79
80=item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >>
81
82This is the constructor. It accepts a L<Class::MOP::Method> object and
83a hash of options. The options accepted are identical to the ones
84accepted by L<Class::MOP::Method>, except that C<body> cannot be passed
85(it will be generated automatically).
86
87=back
88
89=head1 AUTHORS
90
91Jesse Luehrs E<lt>doy at tozt dot netE<gt>
92
93=head1 COPYRIGHT AND LICENSE
94
95Copyright 2006-2010 by Infinity Interactive, Inc.
96
97L<http://www.iinteractive.com>
98
99This library is free software; you can redistribute it and/or modify
100it under the same terms as Perl itself.
101
102=cut
103