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