my $method = $self->get_method($method_name);
# if we dont have local ...
unless ($method) {
- # make sure this method even exists ...
- ($self->find_next_method_by_name($method_name))
+ # try to find the next method
+ $method = $self->find_next_method_by_name($method_name);
+ # die if it does not exist
+ (defined $method)
|| confess "The method '$method_name' is not found in the inherience hierarchy for this class";
- # if so, then create a local which just
- # calls the next applicable method ...
- $self->add_method($method_name => sub {
- $self->find_next_method_by_name($method_name)->(@_);
- });
- $method = $self->get_method($method_name);
- }
-
- # now make sure we wrap it properly
- # (if it isnt already)
- unless ($method->isa('Class::MOP::Method::Wrapped')) {
+ # and now make sure to wrap it
+ # even if it is already wrapped
+ # because we need a new sub ref
$method = Class::MOP::Method::Wrapped->wrap($method);
- $self->add_method($method_name => $method);
- }
+ }
+ else {
+ # now make sure we wrap it properly
+ $method = Class::MOP::Method::Wrapped->wrap($method)
+ unless $method->isa('Class::MOP::Method::Wrapped');
+ }
+ $self->add_method($method_name => $method);
return $method;
};
$method;
}
+sub get_original_method {
+ my $code = shift;
+ $MODIFIERS{$code}->{orig}
+ if exists $MODIFIERS{$code};
+}
+
sub add_before_modifier {
my $code = shift;
my $modifier = shift;
This simply blesses the C<&code> reference passed to it.
+=item B<get_original_method>
+
=back
=head2 Modifiers
use strict;
use warnings;
-use Test::More tests => 17;
+use Test::More tests => 20;
use Test::Exception;
BEGIN {
});
::ok(CheckingAccount->meta->has_method('withdraw'), '... checking account now has a withdraw method');
+ ::isa_ok(CheckingAccount->meta->get_method('withdraw'), 'Class::MOP::Method::Wrapped');
+ ::isa_ok(BankAccount->meta->get_method('withdraw'), 'Class::MOP::Method');
}
$savings_account->withdraw(50);
} '... withdrew from savings successfully';
is($savings_account->balance, 200, '... got the right savings balance after withdrawl');
+dies_ok {
+ $savings_account->withdraw(250);
+} '... could not withdraw from savings successfully';
+
$savings_account->deposit(150);
is($savings_account->balance, 350, '... got the right savings balance after deposit');