Mouse internal cache must be started with '_mouse_cache'
[gitmo/Mouse.git] / lib / Mouse / Role.pm
CommitLineData
f9e68395 1package Mouse::Role;
8bdd9cfc 2use Mouse::Exporter; # enables strict and warnings
f3bb863f 3
b0d52f03 4our $VERSION = '0.73';
8bdd9cfc 5
6use Carp qw(confess);
43523060 7use Scalar::Util qw(blessed);
f9e68395 8
8bdd9cfc 9use Mouse::Util qw(not_supported);
43408273 10use Mouse::Meta::Role;
6d28c5cf 11use Mouse ();
fea36fd2 12
13Mouse::Exporter->setup_import_methods(
14 as_is => [qw(
15 extends with
16 has
17 before after around
18 override super
19 augment inner
20
21 requires excludes
22 ),
23 \&Scalar::Util::blessed,
24 \&Carp::confess,
25 ],
2cb8b713 26);
27
43523060 28
9ee0e57c 29sub extends {
30 Carp::croak "Roles do not support 'extends'";
31}
43523060 32
4cc4f8ed 33sub with {
34 Mouse::Util::apply_all_roles(scalar(caller), @_);
9ee0e57c 35 return;
36}
37
38sub has {
39 my $meta = Mouse::Meta::Role->initialize(scalar caller);
40 my $name = shift;
41
42 $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )})
43 if @_ % 2; # odd number of arguments
44
13f78bbc 45 for my $n(ref($name) ? @{$name} : $name){
46 $meta->add_attribute($n => @_);
9ee0e57c 47 }
48 return;
49}
b32e8fb9 50
51sub before {
8bc2760b 52 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 53 my $code = pop;
013ee5f0 54 for my $name($meta->_collect_methods(@_)) {
55 $meta->add_before_method_modifier($name => $code);
b32e8fb9 56 }
9ee0e57c 57 return;
b32e8fb9 58}
59
60sub after {
8bc2760b 61 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 62 my $code = pop;
013ee5f0 63 for my $name($meta->_collect_methods(@_)) {
64 $meta->add_after_method_modifier($name => $code);
f9e68395 65 }
9ee0e57c 66 return;
b32e8fb9 67}
68
69sub around {
8bc2760b 70 my $meta = Mouse::Meta::Role->initialize(scalar caller);
b32e8fb9 71 my $code = pop;
013ee5f0 72 for my $name($meta->_collect_methods(@_)) {
73 $meta->add_around_method_modifier($name => $code);
b32e8fb9 74 }
9ee0e57c 75 return;
b32e8fb9 76}
77
67199842 78
79sub super {
85bd3f44 80 return if !defined $Mouse::SUPER_BODY;
67199842 81 $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
82}
83
84sub override {
85bd3f44 85 # my($name, $code) = @_;
86 Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
9ee0e57c 87 return;
67199842 88}
89
90# We keep the same errors messages as Moose::Role emits, here.
91sub inner {
6d28c5cf 92 Carp::croak "Roles cannot support 'inner'";
67199842 93}
94
95sub augment {
6d28c5cf 96 Carp::croak "Roles cannot support 'augment'";
67199842 97}
98
59089ec3 99sub requires {
8bc2760b 100 my $meta = Mouse::Meta::Role->initialize(scalar caller);
6d28c5cf 101 $meta->throw_error("Must specify at least one method") unless @_;
59089ec3 102 $meta->add_required_methods(@_);
9ee0e57c 103 return;
59089ec3 104}
b32e8fb9 105
6d28c5cf 106sub excludes {
107 not_supported;
108}
b32e8fb9 109
fea36fd2 110sub init_meta{
9704fe9d 111 shift;
112 my %args = @_;
7daedfff 113
9704fe9d 114 my $class = $args{for_class}
fea36fd2 115 or Carp::confess("Cannot call init_meta without specifying a for_class");
b32e8fb9 116
fea36fd2 117 my $metaclass = $args{metaclass} || 'Mouse::Meta::Role';
7daedfff 118
9704fe9d 119 my $meta = $metaclass->initialize($class);
7daedfff 120
fea36fd2 121 $meta->add_method(meta => sub{
122 $metaclass->initialize(ref($_[0]) || $_[0]);
3a63a2e7 123 });
b32e8fb9 124
9704fe9d 125 # make a role type for each Mouse role
126 Mouse::Util::TypeConstraints::role_type($class)
127 unless Mouse::Util::TypeConstraints::find_type_constraint($class);
128
fea36fd2 129 return $meta;
b32e8fb9 130}
f9e68395 131
1321;
133
cadd5b5e 134__END__
135
136=head1 NAME
137
1820fffe 138Mouse::Role - The Mouse Role
139
a25ca8d6 140=head1 VERSION
141
b0d52f03 142This document describes Mouse version 0.73
a25ca8d6 143
1820fffe 144=head1 SYNOPSIS
145
146 package MyRole;
147 use Mouse::Role;
cadd5b5e 148
149=head1 KEYWORDS
150
1820fffe 151=head2 C<< meta -> Mouse::Meta::Role >>
cadd5b5e 152
153Returns this role's metaclass instance.
154
b0b9f25a 155=head2 C<< before (method|methods|regexp) -> CodeRef >>
cadd5b5e 156
8b13ad67 157Sets up a B<before> method modifier. See L<Moose/before>.
cadd5b5e 158
b0b9f25a 159=head2 C<< after (method|methods|regexp) => CodeRef >>
cadd5b5e 160
8b13ad67 161Sets up an B<after> method modifier. See L<Moose/after>.
cadd5b5e 162
b0b9f25a 163=head2 C<< around (method|methods|regexp) => CodeRef >>
cadd5b5e 164
8b13ad67 165Sets up an B<around> method modifier. See L<Moose/around>.
cadd5b5e 166
1820fffe 167=head2 C<super>
67199842 168
1820fffe 169Sets up the B<super> keyword. See L<Moose/super>.
67199842 170
1820fffe 171=head2 C<< override method => CodeRef >>
67199842 172
1820fffe 173Sets up an B<override> method modifier. See L<Moose/Role/override>.
67199842 174
1820fffe 175=head2 C<inner>
67199842 176
1820fffe 177This is not supported in roles and emits an error. See L<Moose/Role>.
67199842 178
1820fffe 179=head2 C<< augment method => CodeRef >>
67199842 180
1820fffe 181This is not supported in roles and emits an error. See L<Moose/Role>.
67199842 182
1820fffe 183=head2 C<< has (name|names) => parameters >>
cadd5b5e 184
185Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
186this role. See L<Mouse/has>.
187
1820fffe 188=head2 C<< confess(error) -> BOOM >>
cadd5b5e 189
190L<Carp/confess> for your convenience.
191
1820fffe 192=head2 C<< blessed(value) -> ClassName | undef >>
cadd5b5e 193
194L<Scalar::Util/blessed> for your convenience.
195
196=head1 MISC
197
198=head2 import
199
200Importing Mouse::Role will give you sugar.
201
202=head2 unimport
203
1820fffe 204Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
cadd5b5e 205keywords (such as L</has>) it will break loudly instead breaking subtly.
206
1820fffe 207=head1 SEE ALSO
208
209L<Moose::Role>
210
cadd5b5e 211=cut
212