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