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