6 # attribute metaclass for specifying with 'has'
7 package Foo::Meta::Attribute;
9 extends 'Moose::Meta::Attribute';
11 around _process_options => sub {
13 my ($class, $name, $opts) = @_;
14 my $default = delete $opts->{default};
15 $opts->{default} = sub { $default->() . "1" };
16 $class->$orig($name, $opts);
19 # trait for specifying with has
20 package Foo::Meta::Trait;
23 around _process_options => sub {
25 my ($class, $name, $opts) = @_;
26 my $default = delete $opts->{default};
27 $opts->{default} = sub { $default->() . "2" };
28 $class->$orig($name, $opts);
31 # attribute metaclass role for specifying with MetaRole
32 package Foo::Meta::Role::Attribute;
35 around _process_options => sub {
37 my ($class, $name, $opts) = @_;
38 my $default = delete $opts->{default};
39 $opts->{default} = sub { "3" . $default->() };
40 $class->$orig($name, $opts);
46 use Moose::Util::MetaRole;
48 Moose::Exporter->setup_import_methods(also => ['Moose']);
53 Moose->init_meta(%options);
54 Moose::Util::MetaRole::apply_metaclass_roles(
55 for_class => $options{for_class},
56 attribute_metaclass_roles => ['Foo::Meta::Role::Attribute'],
58 return $options{for_class}->meta;
62 # class that uses both
65 BEGIN { Foose->import }
71 default => sub { 'BAR' },
75 metaclass => 'Foo::Meta::Attribute',
79 default => sub { 'BAZ' },
83 traits => ['Foo::Meta::Trait'],
87 default => sub { 'QUUX' },
91 use Test::More tests => 8;
93 is($foo->bar, '3BAR', 'Attribute meta-role applied by exporter');
94 ok($foo->meta->get_attribute('bar')->meta->does_role('Foo::Meta::Role::Attribute'), 'normal attribute does the meta-role');
97 local $TODO = 'metaclass on attribute currently overrides attr metarole';
98 is($foo->baz, '3BAZ1', 'Attribute meta role from exporter + metaclass on attribute');
99 ok($foo->meta->get_attribute('baz')->meta->does_role('Foo::Meta::Role::Attribute'), 'attribute using metaclass does the meta-role');
101 ok($foo->meta->get_attribute('baz')->isa('Foo::Meta::Attribute'), 'attribute using a metaclass isa the metaclass');
103 is($foo->quux, '3QUUX2', 'Attribute meta-role + trait');
104 ok($foo->meta->get_attribute('quux')->meta->does_role('Foo::Meta::Role::Attribute'), 'attribute using a trait does the meta-role');
105 ok($foo->meta->get_attribute('quux')->meta->does_role('Foo::Meta::Trait'), 'attribute using a trait does the trait');