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