Skip to content

Commit 621b274

Browse files
committed
support method modifiers for overrided accessor
Signed-off-by: Ji-Hyeon Gim <potatogim@gluesys.com>
1 parent e1ca94c commit 621b274

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

lib/Mouse/Meta/Class.pm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ sub add_attribute {
250250
# install accessors first
251251
$attr->install_accessors();
252252

253+
foreach my $super ($self->superclasses)
254+
{
255+
my $meta = Mouse::Util::get_metaclass_by_name($super);
256+
my $modifiers = $meta->{modifiers};
257+
258+
foreach my $type (qw(accessor reader writer predicate clearer))
259+
{
260+
next unless (exists $attr->{$type}
261+
&& exists $modifiers->{$attr->{$type}});
262+
263+
foreach my $mtype (qw(before around after))
264+
{
265+
foreach my $m ( @{ $modifiers->{$attr->{$type}}->{$mtype} } )
266+
{
267+
$self->_install_modifier($mtype, $attr->{$type}, $m);
268+
}
269+
}
270+
}
271+
}
272+
253273
# then register the attribute to the metaclass
254274
$attr->{insertion_order} = keys %{ $self->{attributes} };
255275
$self->{attributes}{$name} = $attr;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More;
7+
use Test::Exception;
8+
9+
{
10+
package Foo;
11+
use Mouse;
12+
13+
has 'foo' => (
14+
is => 'ro',
15+
writer => 'set_foo',
16+
);
17+
18+
has 'foo_arounded' => (
19+
is => 'rw',
20+
isa => 'Int',
21+
default => 0,
22+
);
23+
24+
around 'set_foo' => sub {
25+
my $orig = shift;
26+
my $self = shift;
27+
28+
$self->foo_arounded($self->foo_arounded + 1);
29+
30+
$self->$orig(@_);
31+
};
32+
}
33+
34+
{
35+
package Bar;
36+
37+
use Mouse;
38+
39+
extends 'Foo';
40+
41+
has '+foo' => (
42+
lazy => 0,
43+
);
44+
45+
has 'bar' => (
46+
is => 'ro',
47+
writer => 'set_bar',
48+
reader => 'get_bar',
49+
default => 'bar',
50+
);
51+
52+
has 'bar_arounded' => (
53+
is => 'rw',
54+
isa => 'Int',
55+
default => 0,
56+
);
57+
58+
around 'set_foo' => sub
59+
{
60+
my $orig = shift;
61+
my $self = shift;
62+
63+
$self->foo_arounded($self->foo_arounded + 1);
64+
65+
$self->$orig(@_);
66+
};
67+
68+
around 'get_bar' => sub
69+
{
70+
my $orig = shift;
71+
my $self = shift;
72+
73+
$self->bar_arounded($self->bar_arounded + 1);
74+
75+
$self->$orig(@_);
76+
};
77+
}
78+
79+
{
80+
package Baz;
81+
82+
use Mouse;
83+
84+
extends 'Bar';
85+
86+
has '+bar' => (
87+
is => 'ro',
88+
);
89+
90+
around 'get_bar' => sub
91+
{
92+
my $orig = shift;
93+
my $self = shift;
94+
95+
$self->bar_arounded($self->bar_arounded + 1);
96+
97+
$self->$orig(@_);
98+
};
99+
}
100+
101+
{
102+
my $baz = Baz->new;
103+
104+
isa_ok($baz, 'Baz');
105+
106+
$baz->set_foo(1);
107+
$baz->get_bar();
108+
109+
is($baz->foo_arounded, 2, '... got hte correct value');
110+
is($baz->bar_arounded, 2, '... got hte correct value');
111+
}
112+
113+
done_testing;

0 commit comments

Comments
 (0)