2 package Moose::Meta::Role;
10 our $VERSION = '0.02';
12 __PACKAGE__->meta->add_attribute('role_meta' => (
16 __PACKAGE__->meta->add_attribute('attribute_map' => (
17 reader => 'get_attribute_map',
21 __PACKAGE__->meta->add_attribute('method_modifier_map' => (
22 reader => 'get_method_modifier_map',
36 $options{role_meta} = Class::MOP::Class->initialize(
38 ':method_metaclass' => 'Moose::Meta::Role::Method'
40 my $self = $class->meta->new_object(%options);
45 my ($self, $other) = @_;
47 foreach my $attribute_name ($self->get_attribute_list) {
48 # skip it if it has one already
49 next if $other->has_attribute($attribute_name);
50 # add it, although it could be overriden
51 $other->add_attribute(
53 %{$self->get_attribute($attribute_name)}
57 foreach my $method_name ($self->get_method_list) {
58 # skip it if it has one already
59 next if $other->has_method($method_name);
60 # add it, although it could be overriden
63 $self->get_method($method_name)
67 foreach my $method_name ($self->get_method_modifier_list('override')) {
68 # skip it if it has one already
69 next if $other->has_method($method_name);
70 # add it, although it could be overriden
71 $other->add_override_method_modifier(
73 $self->get_method_modifier('override' => $method_name),
78 foreach my $method_name ($self->get_method_modifier_list('before')) {
79 $other->add_before_method_modifier(
81 $self->get_method_modifier('before' => $method_name)
85 foreach my $method_name ($self->get_method_modifier_list('after')) {
86 $other->add_after_method_modifier(
88 $self->get_method_modifier('after' => $method_name)
92 foreach my $method_name ($self->get_method_modifier_list('around')) {
93 $other->add_around_method_modifier(
95 $self->get_method_modifier('around' => $method_name)
99 ## add the roles and set does()
101 $other->add_role($self);
104 # this will not replace a locally
105 # defined does() method, those
106 # should work as expected since
107 # they are working off the same
109 # It will override an inherited
110 # does() method though, since
111 # it needs to add this new metaclass
114 $other->add_method('does' => sub {
115 my (undef, $role_name) = @_;
117 || confess "You much supply a role name to does()";
118 foreach my $class ($other->class_precedence_list) {
120 if $other->initialize($class)->does_role($role_name);
123 }) unless $other->has_method('does');
127 # we delegate to some role_meta methods for convience here
128 # the Moose::Meta::Role is meant to be a read-only interface
129 # to the underlying role package, if you want to manipulate
130 # that, just use ->role_meta
132 sub name { (shift)->role_meta->name }
133 sub version { (shift)->role_meta->version }
135 sub get_method { (shift)->role_meta->get_method(@_) }
136 sub has_method { (shift)->role_meta->has_method(@_) }
137 sub get_method_list {
139 # meta is not applicable in this context,
140 # if you want to see it use the ->role_meta
141 grep { !/^meta$/ } $self->role_meta->get_method_list;
144 # ... however the items in statis (attributes & method modifiers)
145 # can be removed and added to through this API
150 my ($self, $name, %attr_desc) = @_;
151 $self->get_attribute_map->{$name} = \%attr_desc;
155 my ($self, $name) = @_;
156 exists $self->get_attribute_map->{$name} ? 1 : 0;
160 my ($self, $name) = @_;
161 $self->get_attribute_map->{$name}
164 sub remove_attribute {
165 my ($self, $name) = @_;
166 delete $self->get_attribute_map->{$name}
169 sub get_attribute_list {
171 keys %{$self->get_attribute_map};
176 sub add_method_modifier {
177 my ($self, $modifier_type, $method_name, $method) = @_;
178 $self->get_method_modifier_map->{$modifier_type}->{$method_name} = $method;
181 sub has_method_modifier {
182 my ($self, $modifier_type, $method_name) = @_;
183 exists $self->get_method_modifier_map->{$modifier_type}->{$method_name} ? 1 : 0
186 sub get_method_modifier {
187 my ($self, $modifier_type, $method_name) = @_;
188 $self->get_method_modifier_map->{$modifier_type}->{$method_name};
191 sub remove_method_modifier {
192 my ($self, $modifier_type, $method_name) = @_;
193 delete $self->get_method_modifier_map->{$modifier_type}->{$method_name};
196 sub get_method_modifier_list {
197 my ($self, $modifier_type) = @_;
198 keys %{$self->get_method_modifier_map->{$modifier_type}};
201 package Moose::Meta::Role::Method;
206 our $VERSION = '0.01';
208 use base 'Class::MOP::Method';
218 Moose::Meta::Role - The Moose Role metaclass
222 Moose's Roles are being actively developed, please see L<Moose::Role>
223 for more information.
253 =item B<get_method_list>
259 =item B<add_attribute>
261 =item B<has_attribute>
263 =item B<get_attribute>
265 =item B<get_attribute_list>
267 =item B<get_attribute_map>
269 =item B<remove_attribute>
275 =item B<add_method_modifier>
277 =item B<get_method_modifier>
279 =item B<has_method_modifier>
281 =item B<get_method_modifier_list>
283 =item B<get_method_modifier_map>
285 =item B<remove_method_modifier>
291 All complex software has bugs lurking in it, and this module is no
292 exception. If you find a bug please either email me, or add the bug
297 Stevan Little E<lt>stevan@iinteractive.comE<gt>
299 =head1 COPYRIGHT AND LICENSE
301 Copyright 2006 by Infinity Interactive, Inc.
303 L<http://www.iinteractive.com>
305 This library is free software; you can redistribute it and/or modify
306 it under the same terms as Perl itself.