no more XS mah!
[gitmo/Moose.git] / lib / Moose / Meta / Method / Augmented.pm
1 package Moose::Meta::Method::Augmented;
2
3 use strict;
4 use warnings;
5
6 use Carp 'confess';
7
8 our $VERSION   = '0.02';
9 our $AUTHORITY = 'cpan:STEVAN';
10
11 use base 'Moose::Meta::Method';
12
13 sub new {
14     my ( $class, %args ) = @_;
15
16     # the package can be overridden by roles
17     # it is really more like body's compilation stash
18     # this is where we need to override the definition of super() so that the
19     # body of the code can call the right overridden version
20     my $name = $args{name};
21     my $meta = $args{class};
22
23     my $super = $meta->find_next_method_by_name($name);
24
25     (defined $super)
26         || confess "You cannot augment '$name' because it has no super method";
27
28     my $_super_package = $super->package_name;
29     # BUT!,... if this is an overriden method ....
30     if ($super->isa('Moose::Meta::Method::Overriden')) {
31         # we need to be sure that we actually
32         # find the next method, which is not
33         # an 'override' method, the reason is
34         # that an 'override' method will not
35         # be the one calling inner()
36         my $real_super = $meta->_find_next_method_by_name_which_is_not_overridden($name);
37         $_super_package = $real_super->package_name;
38     }
39
40     my $super_body = $super->body;
41
42     my $method = $args{method};
43
44     my $body = sub {
45         local $Moose::INNER_ARGS{$_super_package} = [ @_ ];
46         local $Moose::INNER_BODY{$_super_package} = $method;
47         $super_body->(@_);
48     };
49
50     # FIXME store additional attrs
51     $class->wrap(
52         $body,
53         package_name => $meta->name,
54         name         => $name
55     );
56 }
57
58 1;
59
60 __END__
61
62 =pod
63
64 =head1 NAME
65
66 Moose::Meta::Method::Augmented - A Moose Method metaclass for augmented methods
67
68 =head1 DESCRIPTION
69
70 This class implements method augmenting logic for the L<Moose> C<augment> keyword.
71
72 This involves setting up C<inner> for the superclass body, and dispatching to
73 the superclass from the normal body.
74
75 The subclass definition (the augmentation itself) will be invoked explicitly
76 using the C<inner> keyword from the parent class's method definition.
77
78 =head1 METHODS
79
80 =over 4
81
82 =item B<new>
83
84 =back
85
86 =head1 BUGS
87
88 All complex software has bugs lurking in it, and this module is no 
89 exception. If you find a bug please either email me, or add the bug
90 to cpan-RT.
91
92 =head1 AUTHOR
93
94 Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
95
96 =head1 COPYRIGHT AND LICENSE
97
98 Copyright 2006-2008 by Infinity Interactive, Inc.
99
100 L<http://www.iinteractive.com>
101
102 This library is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself.
104
105 =cut