02580b42da5490dec244970b406cdee8dcc803fe
[gitmo/Moose.git] / lib / Moose / Meta / Method / Overridden.pm
1 package Moose::Meta::Method::Overridden;
2
3 use strict;
4 use warnings;
5
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use base 'Moose::Meta::Method';
9
10 sub new {
11     my ( $class, %args ) = @_;
12
13     # the package can be overridden by roles
14     # it is really more like body's compilation stash
15     # this is where we need to override the definition of super() so that the
16     # body of the code can call the right overridden version
17     my $super_package = $args{package} || $args{class}->name;
18
19     my $name = $args{name};
20
21     my $super = $args{class}->find_next_method_by_name($name);
22
23     (defined $super)
24         || $class->throw_error("You cannot override '$name' because it has no super method", data => $name);
25
26     my $super_body = $super->body;
27
28     my $method = $args{method};
29
30     my $body = sub {
31         local $Moose::SUPER_PACKAGE = $super_package;
32         local @Moose::SUPER_ARGS = @_;
33         local $Moose::SUPER_BODY = $super_body;
34         return $method->(@_);
35     };
36
37     # FIXME do we need this make sure this works for next::method?
38     # subname "${super_package}::${name}", $method;
39
40     # FIXME store additional attrs
41     $class->wrap(
42         $body,
43         package_name => $args{class}->name,
44         name         => $name
45     );
46 }
47
48 1;
49
50 # ABSTRACT: A Moose Method metaclass for overridden methods
51
52 __END__
53
54 =pod
55
56 =head1 DESCRIPTION
57
58 This class implements method overriding logic for the L<Moose>
59 C<override> keyword.
60
61 The overriding subroutine's parent will be invoked explicitly using
62 the C<super> keyword from the parent class's method definition.
63
64 =head1 METHODS
65
66 =over 4
67
68 =item B<< Moose::Meta::Method::Overridden->new(%options) >>
69
70 This constructs a new object. It accepts the following options:
71
72 =over 8
73
74 =item * class
75
76 The metaclass object for the class in which the override is being
77 declared. This option is required.
78
79 =item * name
80
81 The name of the method which we are overriding. This method must exist
82 in one of the class's superclasses. This option is required.
83
84 =item * method
85
86 The subroutine reference which implements the overriding. This option
87 is required.
88
89 =back
90
91 =back
92
93 =head1 BUGS
94
95 See L<Moose/BUGS> for details on reporting bugs.
96
97 =cut