Commit | Line | Data |
---|---|---|
f9e68395 | 1 | package Mouse::Role; |
bc69ee88 | 2 | use Mouse::Util qw(not_supported); # enables strict and warnings |
f3bb863f | 3 | |
43523060 | 4 | use Carp qw(confess); |
5 | use Scalar::Util qw(blessed); | |
f9e68395 | 6 | |
6d28c5cf | 7 | use Mouse (); |
fea36fd2 | 8 | use Mouse::Exporter; |
9 | ||
10 | Mouse::Exporter->setup_import_methods( | |
11 | as_is => [qw( | |
12 | extends with | |
13 | has | |
14 | before after around | |
15 | override super | |
16 | augment inner | |
17 | ||
18 | requires excludes | |
19 | ), | |
20 | \&Scalar::Util::blessed, | |
21 | \&Carp::confess, | |
22 | ], | |
2cb8b713 | 23 | ); |
24 | ||
43523060 | 25 | # XXX: for backward compatibility |
26 | our @EXPORT = qw( | |
27 | extends with | |
28 | has | |
29 | before after around | |
30 | override super | |
31 | augment inner | |
32 | ||
33 | requires excludes | |
34 | ||
35 | blessed confess | |
36 | ); | |
b32e8fb9 | 37 | |
38 | sub before { | |
8bc2760b | 39 | my $meta = Mouse::Meta::Role->initialize(scalar caller); |
b32e8fb9 | 40 | |
41 | my $code = pop; | |
42 | for (@_) { | |
43 | $meta->add_before_method_modifier($_ => $code); | |
44 | } | |
45 | } | |
46 | ||
47 | sub after { | |
8bc2760b | 48 | my $meta = Mouse::Meta::Role->initialize(scalar caller); |
b32e8fb9 | 49 | |
50 | my $code = pop; | |
51 | for (@_) { | |
52 | $meta->add_after_method_modifier($_ => $code); | |
f9e68395 | 53 | } |
b32e8fb9 | 54 | } |
55 | ||
56 | sub around { | |
8bc2760b | 57 | my $meta = Mouse::Meta::Role->initialize(scalar caller); |
b32e8fb9 | 58 | |
59 | my $code = pop; | |
60 | for (@_) { | |
61 | $meta->add_around_method_modifier($_ => $code); | |
62 | } | |
63 | } | |
64 | ||
67199842 | 65 | |
66 | sub super { | |
85bd3f44 | 67 | return if !defined $Mouse::SUPER_BODY; |
67199842 | 68 | $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS); |
69 | } | |
70 | ||
71 | sub override { | |
85bd3f44 | 72 | # my($name, $code) = @_; |
73 | Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_); | |
67199842 | 74 | } |
75 | ||
76 | # We keep the same errors messages as Moose::Role emits, here. | |
77 | sub inner { | |
6d28c5cf | 78 | Carp::croak "Roles cannot support 'inner'"; |
67199842 | 79 | } |
80 | ||
81 | sub augment { | |
6d28c5cf | 82 | Carp::croak "Roles cannot support 'augment'"; |
67199842 | 83 | } |
84 | ||
b32e8fb9 | 85 | sub has { |
8bc2760b | 86 | my $meta = Mouse::Meta::Role->initialize(scalar caller); |
b32e8fb9 | 87 | my $name = shift; |
b32e8fb9 | 88 | |
1b9e472d | 89 | $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name; |
b32e8fb9 | 90 | } |
91 | ||
6d28c5cf | 92 | sub extends { |
93 | Carp::croak "Roles do not support 'extends'" | |
94 | } | |
b32e8fb9 | 95 | |
b1b81553 | 96 | sub with { |
8bc2760b | 97 | my $meta = Mouse::Meta::Role->initialize(scalar caller); |
ff687069 | 98 | Mouse::Util::apply_all_roles($meta->name, @_); |
b1b81553 | 99 | } |
b32e8fb9 | 100 | |
59089ec3 | 101 | sub requires { |
8bc2760b | 102 | my $meta = Mouse::Meta::Role->initialize(scalar caller); |
6d28c5cf | 103 | $meta->throw_error("Must specify at least one method") unless @_; |
59089ec3 | 104 | $meta->add_required_methods(@_); |
105 | } | |
b32e8fb9 | 106 | |
6d28c5cf | 107 | sub excludes { |
108 | not_supported; | |
109 | } | |
b32e8fb9 | 110 | |
fea36fd2 | 111 | sub 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 | |
133 | 1; | |
134 | ||
cadd5b5e | 135 | __END__ |
136 | ||
137 | =head1 NAME | |
138 | ||
1820fffe | 139 | Mouse::Role - The Mouse Role |
140 | ||
141 | =head1 SYNOPSIS | |
142 | ||
143 | package MyRole; | |
144 | use Mouse::Role; | |
cadd5b5e | 145 | |
146 | =head1 KEYWORDS | |
147 | ||
1820fffe | 148 | =head2 C<< meta -> Mouse::Meta::Role >> |
cadd5b5e | 149 | |
150 | Returns this role's metaclass instance. | |
151 | ||
1820fffe | 152 | =head2 C<< before (method|methods) -> CodeRef >> |
cadd5b5e | 153 | |
1820fffe | 154 | Sets up a B<before> method modifier. See L<Moose/before> or |
cadd5b5e | 155 | L<Class::Method::Modifiers/before>. |
156 | ||
1820fffe | 157 | =head2 C<< after (method|methods) => CodeRef >> |
cadd5b5e | 158 | |
1820fffe | 159 | Sets up an B<after> method modifier. See L<Moose/after> or |
cadd5b5e | 160 | L<Class::Method::Modifiers/after>. |
161 | ||
1820fffe | 162 | =head2 C<< around (method|methods) => CodeRef >> |
cadd5b5e | 163 | |
1820fffe | 164 | Sets up an B<around> method modifier. See L<Moose/around> or |
cadd5b5e | 165 | L<Class::Method::Modifiers/around>. |
166 | ||
1820fffe | 167 | =head2 C<super> |
67199842 | 168 | |
1820fffe | 169 | Sets up the B<super> keyword. See L<Moose/super>. |
67199842 | 170 | |
1820fffe | 171 | =head2 C<< override method => CodeRef >> |
67199842 | 172 | |
1820fffe | 173 | Sets up an B<override> method modifier. See L<Moose/Role/override>. |
67199842 | 174 | |
1820fffe | 175 | =head2 C<inner> |
67199842 | 176 | |
1820fffe | 177 | This 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 | 181 | This 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 | |
185 | Sets up an attribute (or if passed an arrayref of names, multiple attributes) to | |
186 | this role. See L<Mouse/has>. | |
187 | ||
1820fffe | 188 | =head2 C<< confess(error) -> BOOM >> |
cadd5b5e | 189 | |
190 | L<Carp/confess> for your convenience. | |
191 | ||
1820fffe | 192 | =head2 C<< blessed(value) -> ClassName | undef >> |
cadd5b5e | 193 | |
194 | L<Scalar::Util/blessed> for your convenience. | |
195 | ||
196 | =head1 MISC | |
197 | ||
198 | =head2 import | |
199 | ||
200 | Importing Mouse::Role will give you sugar. | |
201 | ||
202 | =head2 unimport | |
203 | ||
1820fffe | 204 | Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the |
cadd5b5e | 205 | keywords (such as L</has>) it will break loudly instead breaking subtly. |
206 | ||
1820fffe | 207 | =head1 SEE ALSO |
208 | ||
209 | L<Moose::Role> | |
210 | ||
cadd5b5e | 211 | =cut |
212 |