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