Version 1.12
[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
bd2550f8 10our $VERSION = '1.12';
59b51046 11$VERSION = eval $VERSION;
12our $AUTHORITY = 'cpan:STEVAN';
13
684b69cf 14use constant DEBUG_NO_META => $ENV{DEBUG_NO_META} ? 1 : 0;
59b51046 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
5281e1f9 61sub _make_compatible_with {
62 my $self = shift;
63 my ($other) = @_;
64
65 # XXX: this is pretty gross. the issue here is that CMOP::Method::Meta
66 # objects are subclasses of CMOP::Method, but when we get to moose, they'll
67 # need to be compatible with Moose::Meta::Method, which isn't possible. the
68 # right solution here is to make ::Meta into a role that gets applied to
69 # whatever the method_metaclass happens to be and get rid of
70 # _meta_method_metaclass entirely, but that's not going to happen until
71 # we ditch cmop and get roles into the bootstrapping, so. i'm not
72 # maintaining the previous behavior of turning them into instances of the
73 # new method_metaclass because that's equally broken, and at least this way
74 # any issues will at least be detectable and potentially fixable. -doy
75 return $self unless $other->_is_compatible_with($self->_real_ref_name);
76
77 return $self->SUPER::_make_compatible_with(@_);
78}
79
59b51046 801;
81
82__END__
83
84=pod
85
86=head1 NAME
87
88Class::MOP::Method::Meta - Method Meta Object for C<meta> methods
89
90=head1 DESCRIPTION
91
92This is a L<Class::MOP::Method> subclass which represents C<meta>
93methods installed into classes by Class::MOP.
94
95=head1 METHODS
96
97=over 4
98
99=item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >>
100
101This is the constructor. It accepts a L<Class::MOP::Method> object and
102a hash of options. The options accepted are identical to the ones
103accepted by L<Class::MOP::Method>, except that C<body> cannot be passed
104(it will be generated automatically).
105
106=back
107
108=head1 AUTHORS
109
110Jesse Luehrs E<lt>doy at tozt dot netE<gt>
111
112=head1 COPYRIGHT AND LICENSE
113
114Copyright 2006-2010 by Infinity Interactive, Inc.
115
116L<http://www.iinteractive.com>
117
118This library is free software; you can redistribute it and/or modify
119it under the same terms as Perl itself.
120
121=cut
122