Changelogging
[gitmo/Mouse.git] / lib / Mouse / Meta / Role.pm
CommitLineData
a2227e71 1package Mouse::Meta::Role;
4cc4f8ed 2use Mouse::Util qw(:meta); # enables strict and warnings
74be9f76 3
6d28c5cf 4use Mouse::Meta::Module;
f3bb863f 5our @ISA = qw(Mouse::Meta::Module);
a2227e71 6
e058b279 7sub method_metaclass;
6cfa1e5e 8
8e64d0fa 9sub _construct_meta {
acf0f643 10 my $class = shift;
7a50b450 11
acf0f643 12 my %args = @_;
13
5132ec42 14 $args{methods} = {};
15 $args{attributes} = {};
16 $args{required_methods} = [];
17 $args{roles} = [];
274b6cce 18
9009aca1 19 my $self = bless \%args, ref($class) || $class;
20 if($class ne __PACKAGE__){
21 $self->meta->_initialize_object($self, \%args);
22 }
9009aca1 23 return $self;
7a50b450 24}
25
26sub create_anon_role{
27 my $self = shift;
28 return $self->create(undef, @_);
29}
30
43165725 31sub is_anon_role;
a2227e71 32
43165725 33sub get_roles;
afc73948 34
e7264861 35sub calculate_all_roles {
36 my $self = shift;
37 my %seen;
38 return grep { !$seen{ $_->name }++ }
39 ($self, map { $_->calculate_all_roles } @{ $self->get_roles });
40}
41
6cfa1e5e 42sub get_required_method_list{
43 return @{ $_[0]->{required_methods} };
44}
afc73948 45
59089ec3 46sub add_required_methods {
ea249879 47 my($self, @methods) = @_;
71e7b544 48 my %required = map{ $_ => 1 } @{$self->{required_methods}};
49 push @{$self->{required_methods}}, grep{ !$required{$_}++ && !$self->has_method($_) } @methods;
50 return;
59089ec3 51}
52
6cfa1e5e 53sub requires_method {
54 my($self, $name) = @_;
55 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
56}
57
274b6cce 58sub add_attribute {
59 my $self = shift;
60 my $name = shift;
6cfa1e5e 61
62 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
c9313657 63 return;
da0c885d 64}
65
3a63a2e7 66sub apply {
45f22b92 67 my $self = shift;
68 my $consumer = shift;
7a50b450 69
823419c5 70 require 'Mouse/Meta/Role/Application.pm';
71 return Mouse::Meta::Role::Application->new(@_)->apply($self, $consumer);
71e7b544 72}
73
71e7b544 74sub combine {
230dd14a 75 my($self, @role_specs) = @_;
21498b08 76
823419c5 77 require 'Mouse/Meta/Role/Composite.pm';
71e7b544 78 my $composite = Mouse::Meta::Role::Composite->create_anon_role();
79
80 foreach my $role_spec (@role_specs) {
80b463bb 81 my($role, $args) = @{$role_spec};
82 $role->apply($composite, %{$args});
21498b08 83 }
71e7b544 84 return $composite;
21498b08 85}
86
cb60d0b5 87sub add_before_method_modifier;
88sub add_around_method_modifier;
89sub add_after_method_modifier;
3a63a2e7 90
cb60d0b5 91sub get_before_method_modifiers;
92sub get_around_method_modifiers;
93sub get_after_method_modifiers;
47f36c05 94
6cfa1e5e 95sub add_override_method_modifier{
96 my($self, $method_name, $method) = @_;
97
60b5c3be 98 if($self->has_method($method_name)){
99 # This error happens in the override keyword or during role composition,
100 # so I added a message, "A local method of ...", only for compatibility (gfx)
8e64d0fa 101 $self->throw_error("Cannot add an override of method '$method_name' "
60b5c3be 102 . "because there is a local version of '$method_name'"
103 . "(A local method of the same name as been found)");
104 }
6cfa1e5e 105
106 $self->{override_method_modifiers}->{$method_name} = $method;
107}
108
8e64d0fa 109sub get_override_method_modifier {
110 my ($self, $method_name) = @_;
111 return $self->{override_method_modifiers}->{$method_name};
6cfa1e5e 112}
113
67199842 114sub does_role {
115 my ($self, $role_name) = @_;
116
117 (defined $role_name)
fce211ae 118 || $self->throw_error("You must supply a role name to look for");
67199842 119
f3e11122 120 $role_name = $role_name->name if ref $role_name;
121
67199842 122 # if we are it,.. then return true
123 return 1 if $role_name eq $self->name;
3a63a2e7 124 # otherwise.. check our children
125 for my $role (@{ $self->get_roles }) {
67199842 126 return 1 if $role->does_role($role_name);
127 }
128 return 0;
129}
130
a2227e71 1311;
1820fffe 132__END__
133
134=head1 NAME
135
136Mouse::Meta::Role - The Mouse Role metaclass
137
a25ca8d6 138=head1 VERSION
139
43c1bb1a 140This document describes Mouse version 0.71
a25ca8d6 141
503ed648 142=head1 DESCRIPTION
143
144This class is a meta object protocol for Mouse roles,
145which is a subset of Moose::Meta:::Role.
146
1820fffe 147=head1 SEE ALSO
148
149L<Moose::Meta::Role>
150
151=cut