2 package Mouse::Meta::Role;
10 # because Mouse doesn't introspect existing classes, we're forced to
11 # only pay attention to other Mouse classes
12 sub _metaclass_cache {
15 return $METACLASS_CACHE{$name};
21 $METACLASS_CACHE{$name} = $class->new(name => $name)
22 if !exists($METACLASS_CACHE{$name});
23 return $METACLASS_CACHE{$name};
31 $args{attributes} ||= {};
32 $args{required_methods} ||= [];
37 sub name { $_[0]->{name} }
39 sub add_required_methods {
42 push @{$self->{required_methods}}, @methods;
49 $self->{attributes}->{$name} = $spec;
52 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
53 sub get_attribute_list { keys %{ $_[0]->{attributes} } }
54 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
60 for my $name (@{$self->{required_methods}}) {
61 unless ($class->name->can($name)) {
62 confess "'@{[ $self->name ]}' requires the method '$name' to be implemented by '@{[ $class->name ]}'";
66 for my $name ($self->get_attribute_list) {
67 next if $class->has_attribute($name);
68 my $spec = $self->get_attribute($name);
69 Mouse::Meta::Attribute->create($class, $name, %$spec);
72 for my $modifier_type (qw/before after around/) {
73 my $add_method = "add_${modifier_type}_method_modifier";
74 my $modified = $self->{"${modifier_type}_method_modifiers"};
76 for my $method_name (keys %$modified) {
77 for my $code (@{ $modified->{$method_name} }) {
78 $class->$add_method($method_name => $code);
84 for my $modifier_type (qw/before after around/) {
86 *{ __PACKAGE__ . '::' . "add_${modifier_type}_method_modifier" } = sub {
87 my ($self, $method_name, $method) = @_;
89 push @{ $self->{"${modifier_type}_method_modifiers"}->{$method_name} },
93 *{ __PACKAGE__ . '::' . "get_${modifier_type}_method_modifiers" } = sub {
94 my ($self, $method_name, $method) = @_;
95 @{ $self->{"${modifier_type}_method_modifiers"}->{$method_name} || [] }