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