give CMOP::Object a real meta method, and simplify some things
[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')
34 # it's okay if the test itself calls ->meta, we only care about
35 # if the mop internals call ->meta
36 && $method_self->_is_caller_mop_internal(scalar caller);
59b51046 37 }
38 # we must re-initialize so that it
39 # works as expected in subclasses,
40 # since metaclass instances are
41 # singletons, this is not really a
42 # big deal anyway.
43 $metaclass->initialize(blessed($_[0]) || $_[0])
44 };
45}
46
47sub wrap {
48 my ($class, @args) = @_;
49
50 unshift @args, 'body' if @args % 2 == 1;
51 my %params = @args;
52 confess "Overriding the body of meta methods is not allowed"
53 if $params{body};
54
55 my $metaclass_class = $params{associated_metaclass}->meta;
56 $params{body} = $class->_generate_meta_method($metaclass_class);
57 return $class->SUPER::wrap(%params);
58}
59
601;
61
62__END__
63
64=pod
65
66=head1 NAME
67
68Class::MOP::Method::Meta - Method Meta Object for C<meta> methods
69
70=head1 DESCRIPTION
71
72This is a L<Class::MOP::Method> subclass which represents C<meta>
73methods installed into classes by Class::MOP.
74
75=head1 METHODS
76
77=over 4
78
79=item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >>
80
81This is the constructor. It accepts a L<Class::MOP::Method> object and
82a hash of options. The options accepted are identical to the ones
83accepted by L<Class::MOP::Method>, except that C<body> cannot be passed
84(it will be generated automatically).
85
86=back
87
88=head1 AUTHORS
89
90Jesse Luehrs E<lt>doy at tozt dot netE<gt>
91
92=head1 COPYRIGHT AND LICENSE
93
94Copyright 2006-2010 by Infinity Interactive, Inc.
95
96L<http://www.iinteractive.com>
97
98This library is free software; you can redistribute it and/or modify
99it under the same terms as Perl itself.
100
101=cut
102