Beginning of dzilization
[gitmo/Moose.git] / lib / Moose / Meta / Method / Augmented.pm
1 package Moose::Meta::Method::Augmented;
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 $name = $args{name};
18     my $meta = $args{class};
19
20     my $super = $meta->find_next_method_by_name($name);
21
22     (defined $super)
23         || $meta->throw_error("You cannot augment '$name' because it has no super method", data => $name);
24
25     my $_super_package = $super->package_name;
26     # BUT!,... if this is an overridden method ....
27     if ($super->isa('Moose::Meta::Method::Overridden')) {
28         # we need to be sure that we actually
29         # find the next method, which is not
30         # an 'override' method, the reason is
31         # that an 'override' method will not
32         # be the one calling inner()
33         my $real_super = $meta->_find_next_method_by_name_which_is_not_overridden($name);
34         $_super_package = $real_super->package_name;
35     }
36
37     my $super_body = $super->body;
38
39     my $method = $args{method};
40
41     my $body = sub {
42         local $Moose::INNER_ARGS{$_super_package} = [ @_ ];
43         local $Moose::INNER_BODY{$_super_package} = $method;
44         $super_body->(@_);
45     };
46
47     # FIXME store additional attrs
48     $class->wrap(
49         $body,
50         package_name => $meta->name,
51         name         => $name
52     );
53 }
54
55 1;
56
57 # ABSTRACT: A Moose Method metaclass for augmented methods
58
59 __END__
60
61 =pod
62
63 =head1 DESCRIPTION
64
65 This class implements method augmentation logic for the L<Moose>
66 C<augment> keyword.
67
68 The augmentation subroutine reference will be invoked explicitly using
69 the C<inner> keyword from the parent class's method definition.
70
71 =head1 INHERITANCE
72
73 C<Moose::Meta::Method::Augmented> is a subclass of L<Moose::Meta::Method>.
74
75 =head1 METHODS
76
77 =over 4
78
79 =item B<< Moose::Meta::Method::Augmented->new(%options) >>
80
81 This constructs a new object. It accepts the following options:
82
83 =over 8
84
85 =item * class
86
87 The metaclass object for the class in which the augmentation is being
88 declared. This option is required.
89
90 =item * name
91
92 The name of the method which we are augmenting. This method must exist
93 in one of the class's superclasses. This option is required.
94
95 =item * method
96
97 The subroutine reference which implements the augmentation. This
98 option is required.
99
100 =back
101
102 =back
103
104 =head1 BUGS
105
106 See L<Moose/BUGS> for details on reporting bugs.
107
108 =cut