Version 1.12
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Meta.pm
1
2 package Class::MOP::Method::Meta;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed';
9
10 our $VERSION   = '1.12';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 use constant DEBUG_NO_META => $ENV{DEBUG_NO_META} ? 1 : 0;
15
16 use base 'Class::MOP::Method';
17
18 sub _is_caller_mop_internal {
19     my $self = shift;
20     my ($caller) = @_;
21     return $caller =~ /^(?:Class::MOP|metaclass)(?:::|$)/;
22 }
23
24 sub _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) {
30             confess "'meta' method called by MOP internals"
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                 && !$_[0]->isa('Class::MOP::Mixin')
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);
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
48 sub 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
61 sub _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
80 1;
81
82 __END__
83
84 =pod
85
86 =head1 NAME
87
88 Class::MOP::Method::Meta - Method Meta Object for C<meta> methods
89
90 =head1 DESCRIPTION
91
92 This is a L<Class::MOP::Method> subclass which represents C<meta>
93 methods 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
101 This is the constructor. It accepts a L<Class::MOP::Method> object and
102 a hash of options. The options accepted are identical to the ones
103 accepted 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
110 Jesse Luehrs E<lt>doy at tozt dot netE<gt>
111
112 =head1 COPYRIGHT AND LICENSE
113
114 Copyright 2006-2010 by Infinity Interactive, Inc.
115
116 L<http://www.iinteractive.com>
117
118 This library is free software; you can redistribute it and/or modify
119 it under the same terms as Perl itself.
120
121 =cut
122