bump version to 0.91
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Wrapped.pm
CommitLineData
ba38bf08 1
2package Class::MOP::Method::Wrapped;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
9b522fc4 8use Scalar::Util 'blessed';
ba38bf08 9
4b18c1b3 10our $VERSION = '0.91';
d519662a 11$VERSION = eval $VERSION;
ba38bf08 12our $AUTHORITY = 'cpan:STEVAN';
13
b7bdffc3 14use base 'Class::MOP::Method';
ba38bf08 15
16# NOTE:
b7bdffc3 17# this ugly beast is the result of trying
ba38bf08 18# to micro optimize this as much as possible
19# while not completely loosing maintainability.
20# At this point it's "fast enough", after all
21# you can't get something for nothing :)
22my $_build_wrapped_method = sub {
69e3ab0a 23 my $modifier_table = shift;
24 my ($before, $after, $around) = (
25 $modifier_table->{before},
26 $modifier_table->{after},
27 $modifier_table->{around},
28 );
29 if (@$before && @$after) {
30 $modifier_table->{cache} = sub {
2dfbb99a 31 for my $c (@$before) { $c->(@_) };
69e3ab0a 32 my @rval;
33 ((defined wantarray) ?
34 ((wantarray) ?
35 (@rval = $around->{cache}->(@_))
36 :
37 ($rval[0] = $around->{cache}->(@_)))
38 :
39 $around->{cache}->(@_));
2dfbb99a 40 for my $c (@$after) { $c->(@_) };
69e3ab0a 41 return unless defined wantarray;
42 return wantarray ? @rval : $rval[0];
43 }
44 }
45 elsif (@$before && !@$after) {
46 $modifier_table->{cache} = sub {
2dfbb99a 47 for my $c (@$before) { $c->(@_) };
69e3ab0a 48 return $around->{cache}->(@_);
49 }
50 }
51 elsif (@$after && !@$before) {
52 $modifier_table->{cache} = sub {
53 my @rval;
54 ((defined wantarray) ?
55 ((wantarray) ?
56 (@rval = $around->{cache}->(@_))
57 :
58 ($rval[0] = $around->{cache}->(@_)))
59 :
60 $around->{cache}->(@_));
2dfbb99a 61 for my $c (@$after) { $c->(@_) };
69e3ab0a 62 return unless defined wantarray;
63 return wantarray ? @rval : $rval[0];
64 }
65 }
66 else {
67 $modifier_table->{cache} = $around->{cache};
68 }
ba38bf08 69};
70
71sub wrap {
4c105333 72 my ( $class, $code, %params ) = @_;
2dfbb99a 73
69e3ab0a 74 (blessed($code) && $code->isa('Class::MOP::Method'))
75 || confess "Can only wrap blessed CODE";
2dfbb99a 76
69e3ab0a 77 my $modifier_table = {
78 cache => undef,
79 orig => $code,
80 before => [],
81 after => [],
82 around => {
83 cache => $code->body,
84 methods => [],
85 },
86 };
87 $_build_wrapped_method->($modifier_table);
7fe2c0b6 88 return $class->SUPER::wrap(
4c105333 89 sub { $modifier_table->{cache}->(@_) },
2dfbb99a 90 # get these from the original
4c105333 91 # unless explicitly overriden
7fe2c0b6 92 package_name => $params{package_name} || $code->package_name,
93 name => $params{name} || $code->name,
94
95 modifier_table => $modifier_table,
4c105333 96 );
7fe2c0b6 97}
98
99sub _new {
100 my $class = shift;
101 return Class::MOP::Class->initialize($class)->new_object(@_)
102 if $class ne __PACKAGE__;
103
104 my $params = @_ == 1 ? $_[0] : {@_};
105
106 return bless {
107 # inherited from Class::MOP::Method
108 'body' => $params->{body},
109 'associated_metaclass' => $params->{associated_metaclass},
110 'package_name' => $params->{package_name},
111 'name' => $params->{name},
112 'original_method' => $params->{original_method},
113
114 # defined in this class
115 'modifier_table' => $params->{modifier_table}
116 } => $class;
ba38bf08 117}
118
119sub get_original_method {
69e3ab0a 120 my $code = shift;
8683db0e 121 $code->{'modifier_table'}->{orig};
ba38bf08 122}
123
124sub add_before_modifier {
69e3ab0a 125 my $code = shift;
126 my $modifier = shift;
8683db0e 127 unshift @{$code->{'modifier_table'}->{before}} => $modifier;
128 $_build_wrapped_method->($code->{'modifier_table'});
ba38bf08 129}
130
b88aa2e8 131sub before_modifiers {
132 my $code = shift;
133 return @{$code->{'modifier_table'}->{before}};
134}
135
ba38bf08 136sub add_after_modifier {
69e3ab0a 137 my $code = shift;
138 my $modifier = shift;
8683db0e 139 push @{$code->{'modifier_table'}->{after}} => $modifier;
140 $_build_wrapped_method->($code->{'modifier_table'});
ba38bf08 141}
142
b88aa2e8 143sub after_modifiers {
144 my $code = shift;
145 return @{$code->{'modifier_table'}->{after}};
146}
147
ba38bf08 148{
69e3ab0a 149 # NOTE:
150 # this is another possible candidate for
151 # optimization as well. There is an overhead
152 # associated with the currying that, if
153 # eliminated might make around modifiers
154 # more manageable.
155 my $compile_around_method = sub {{
156 my $f1 = pop;
157 return $f1 unless @_;
158 my $f2 = pop;
159 push @_, sub { $f2->( $f1, @_ ) };
160 redo;
161 }};
162
163 sub add_around_modifier {
164 my $code = shift;
165 my $modifier = shift;
8683db0e 166 unshift @{$code->{'modifier_table'}->{around}->{methods}} => $modifier;
167 $code->{'modifier_table'}->{around}->{cache} = $compile_around_method->(
168 @{$code->{'modifier_table'}->{around}->{methods}},
169 $code->{'modifier_table'}->{orig}->body
69e3ab0a 170 );
8683db0e 171 $_build_wrapped_method->($code->{'modifier_table'});
69e3ab0a 172 }
ba38bf08 173}
174
b88aa2e8 175sub around_modifiers {
176 my $code = shift;
177 return @{$code->{'modifier_table'}->{around}->{methods}};
178}
179
ba38bf08 1801;
181
182__END__
183
184=pod
185
b7bdffc3 186=head1 NAME
ba38bf08 187
07ba54f8 188Class::MOP::Method::Wrapped - Method Meta Object for methods with before/after/around modifiers
ba38bf08 189
ba38bf08 190=head1 DESCRIPTION
191
07ba54f8 192This is a L<Class::MOP::Method> subclass which implements before,
193after, and around method modifiers.
127d39a7 194
ba38bf08 195=head1 METHODS
196
197=head2 Construction
198
199=over 4
200
07ba54f8 201=item B<< Class::MOP::Method::Wrapped->wrap($metamethod, %options) >>
127d39a7 202
07ba54f8 203This is the constructor. It accepts a L<Class::MOP::Method> object and
204a hash of options.
ba38bf08 205
07ba54f8 206The options are:
ba38bf08 207
07ba54f8 208=over 8
ba38bf08 209
07ba54f8 210=item * name
ba38bf08 211
07ba54f8 212The method name (without a package name). This will be taken from the
213provided L<Class::MOP::Method> object if it is not provided.
127d39a7 214
07ba54f8 215=item * package_name
ba38bf08 216
07ba54f8 217The package name for the method. This will be taken from the provided
218L<Class::MOP::Method> object if it is not provided.
ba38bf08 219
07ba54f8 220=item * associated_metaclass
ba38bf08 221
07ba54f8 222An optional L<Class::MOP::Class> object. This is the metaclass for the
223method's class.
ba38bf08 224
225=back
226
07ba54f8 227=item B<< $metamethod->get_original_method >>
b88aa2e8 228
07ba54f8 229This returns the L<Class::MOP::Method> object that was passed to the
230constructor.
231
232=item B<< $metamethod->add_before_modifier($code) >>
233
234=item B<< $metamethod->add_after_modifier($code) >>
235
236=item B<< $metamethod->add_around_modifier($code) >>
237
238These methods all take a subroutine reference and apply it as a
239modifier to the original method.
240
241=item B<< $metamethod->before_modifiers >>
b88aa2e8 242
07ba54f8 243=item B<< $metamethod->after_modifiers >>
b88aa2e8 244
07ba54f8 245=item B<< $metamethod->around_modifiers >>
b88aa2e8 246
07ba54f8 247These methods all return a list of subroutine references which are
248acting as the specified type of modifier.
b88aa2e8 249
250=back
251
ba38bf08 252=head1 AUTHORS
253
254Stevan Little E<lt>stevan@iinteractive.comE<gt>
255
ba38bf08 256=head1 COPYRIGHT AND LICENSE
257
070bb6c9 258Copyright 2006-2009 by Infinity Interactive, Inc.
ba38bf08 259
260L<http://www.iinteractive.com>
261
262This library is free software; you can redistribute it and/or modify
b7bdffc3 263it under the same terms as Perl itself.
ba38bf08 264
265=cut
266