Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/Mouse/Meta/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,56 @@ sub add_attribute {
# install accessors first
$attr->install_accessors();

foreach my $isa ($self->linearized_isa)
{
my $meta = Mouse::Util::get_metaclass_by_name($isa);
my $modifiers = $meta->{modifiers};

my @targets;

# install accessors modifiers
foreach my $type (qw(accessor reader writer predicate clearer)) {
next unless (exists $attr->{$type}
&& exists $modifiers->{$attr->{$type}});

foreach my $mtype (qw(before around after)) {
foreach my $m ( @{ $modifiers->{$attr->{$type}}->{$mtype} } ) {
push @targets, [$mtype, $attr->{$type}, $m];
}
}
}

# install handles modifiers
if (exists $attr->{handles})
{
my @handles;

if (ref $attr->{handles} eq 'HASH')
{
@handles = keys %{ $attr->{handles} }
}
elsif (ref $attr->{handles} eq 'ARRAY')
{
@handles = @{ $attr->{handles} };
}

foreach my $handle ( @handles ) {
next unless (exists $modifiers->{$handle});

foreach my $mtype (qw(before around after)) {
foreach my $m ( @{ $modifiers->{$handle}->{$mtype} } ) {
push @targets, [$mtype, $handle, $m];
}
}
}
}

foreach my $mod (@targets) {
my ($mtype, $name, $subroutine) = @{ $mod };
$self->_install_modifier($mtype, $name, $subroutine);
}
}

# then register the attribute to the metaclass
$attr->{insertion_order} = keys %{ $self->{attributes} };
$self->{attributes}{$name} = $attr;
Expand Down
219 changes: 219 additions & 0 deletions t/020_attributes/037_overrided_accessor_modifier.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Exception;

{
package Foo;

use Mouse;

has 'foo' => (
is => 'ro',
writer => 'set_foo',
predicate => 'has_foo',
);

has 'set_foo_arounded' => (
is => 'rw',
isa => 'Int',
default => 0,
);

has 'has_foo_arounded' => (
is => 'rw',
isa => 'Int',
default => 0,
);

around 'has_foo' => sub {
my $orig = shift;
my $self = shift;

$self->has_foo_arounded($self->has_foo_arounded + 1);

$self->$orig(@_);
};

around 'set_foo' => sub {
my $orig = shift;
my $self = shift;

$self->set_foo_arounded($self->set_foo_arounded + 1);

$self->$orig(@_);
};
}

{
package MyFoo;

use Mouse;

sub push { return; };
}

{
package Bar;

use Mouse;

extends 'Foo';

has '+foo' => (
lazy => 0,
);

has 'bar' => (
is => 'ro',
isa => 'MyFoo',
reader => 'get_bar',
default => sub { MyFoo->new(); },
handles => [qw/push/],
);

has 'get_bar_arounded' => (
is => 'rw',
isa => 'Int',
default => 0,
);

has 'bar_handle_arounded' => (
is => 'rw',
isa => 'Int',
default => 0,
);

around 'has_foo' => sub {
my $orig = shift;
my $self = shift;

$self->has_foo_arounded($self->has_foo_arounded + 1);

$self->$orig(@_);
};

around 'set_foo' => sub
{
my $orig = shift;
my $self = shift;

$self->set_foo_arounded($self->set_foo_arounded + 1);

$self->$orig(@_);
};

around 'get_bar' => sub
{
my $orig = shift;
my $self = shift;

$self->get_bar_arounded($self->get_bar_arounded + 1);

$self->$orig(@_);
};

around 'push' => sub
{
my $orig = shift;
my $self = shift;

$self->bar_handle_arounded($self->bar_handle_arounded + 1);

$self->$orig(@_);
};
}

{
package Baz;

use Mouse;

extends 'Bar';

has '+bar' => (
lazy => 0,
);

around 'has_foo' => sub {
my $orig = shift;
my $self = shift;

$self->has_foo_arounded($self->has_foo_arounded + 1);

$self->$orig(@_);
};

around 'get_bar' => sub
{
my $orig = shift;
my $self = shift;

$self->get_bar_arounded($self->get_bar_arounded + 1);

$self->$orig(@_);
};

around 'push' => sub
{
my $orig = shift;
my $self = shift;

$self->bar_handle_arounded($self->bar_handle_arounded + 1);

$self->$orig(@_);
};
}

{
my $foo = Foo->new;

isa_ok($foo, 'Foo');

$foo->has_foo();
$foo->set_foo(1);

is($foo->has_foo_arounded, 1, '... got hte correct value');
is($foo->set_foo_arounded, 1, '... got hte correct value');

my $bar = Bar->new;

isa_ok($bar, 'Bar');

$bar->has_foo();
is($bar->has_foo_arounded, 2, '... got hte correct value');

$bar->set_foo(1);
is($bar->set_foo_arounded, 2, '... got hte correct value');

$bar->get_bar();
is($bar->get_bar_arounded, 1, '... got hte correct value');

$bar->push(1);
# method delegation calls reader internally
# Mouse/Meta/Method/Delegation.pm:26-51
is($bar->get_bar_arounded, 2, '... got hte correct value');
is($bar->bar_handle_arounded, 1, '... got hte correct value');

my $baz = Baz->new;

isa_ok($baz, 'Baz');

$baz->has_foo();
is($baz->has_foo_arounded, 3, '... got hte correct value');

$baz->set_foo(1);
is($baz->set_foo_arounded, 2, '... got hte correct value');

$baz->get_bar();
is($baz->get_bar_arounded, 2, '... got hte correct value');

$baz->push(1);
is($baz->get_bar_arounded, 4, '... got hte correct value');
is($baz->bar_handle_arounded, 2, '... got hte correct value');
}

done_testing;