Commit | Line | Data |
2de7e794 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
7ff56534 |
6 | use Test::More tests => 7; |
2de7e794 |
7 | use Test::Exception; |
8 | use Test::Moose; |
9 | |
7ff56534 |
10 | |
2de7e794 |
11 | |
12 | { |
13 | package My::Meta::Attribute::DefaultReadOnly; |
14 | use Moose; |
d03bd989 |
15 | |
2de7e794 |
16 | extends 'Moose::Meta::Attribute'; |
d03bd989 |
17 | |
2de7e794 |
18 | around 'new' => sub { |
19 | my $next = shift; |
20 | my ($self, $name, %options) = @_; |
d03bd989 |
21 | $options{is} = 'ro' |
2de7e794 |
22 | unless exists $options{is}; |
23 | $next->($self, $name, %options); |
d03bd989 |
24 | }; |
2de7e794 |
25 | } |
26 | |
27 | { |
28 | package My::Attribute::Trait; |
29 | use Moose::Role; |
d03bd989 |
30 | |
2de7e794 |
31 | has 'alias_to' => (is => 'ro', isa => 'Str'); |
d03bd989 |
32 | |
2de7e794 |
33 | after 'install_accessors' => sub { |
34 | my $self = shift; |
35 | $self->associated_class->add_method( |
d03bd989 |
36 | $self->alias_to, |
2de7e794 |
37 | $self->get_read_method_ref |
38 | ); |
39 | }; |
40 | } |
41 | |
42 | { |
43 | package My::Class; |
44 | use Moose; |
d03bd989 |
45 | |
2de7e794 |
46 | has 'bar' => ( |
47 | metaclass => 'My::Meta::Attribute::DefaultReadOnly', |
48 | traits => [qw/My::Attribute::Trait/], |
49 | isa => 'Int', |
50 | alias_to => 'baz', |
51 | ); |
52 | } |
53 | |
54 | my $c = My::Class->new(bar => 100); |
55 | isa_ok($c, 'My::Class'); |
56 | |
57 | is($c->bar, 100, '... got the right value for bar'); |
58 | |
59 | can_ok($c, 'baz'); |
60 | is($c->baz, 100, '... got the right value for baz'); |
61 | |
62 | isa_ok($c->meta->get_attribute('bar'), 'My::Meta::Attribute::DefaultReadOnly'); |
63 | does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait'); |
64 | is($c->meta->get_attribute('bar')->_is_metadata, 'ro', '... got the right metaclass customization'); |
65 | |
66 | |
67 | |
68 | |