add an option to explicitly prohibit method shadowing
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application.pm
1 package Moose::Meta::Role::Application;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 __PACKAGE__->meta->add_attribute('method_exclusions' => (
8     init_arg => '-excludes',
9     reader   => 'get_method_exclusions',
10     default  => sub { [] },
11     Class::MOP::_definition_context(),
12 ));
13
14 __PACKAGE__->meta->add_attribute('method_aliases' => (
15     init_arg => '-alias',
16     reader   => 'get_method_aliases',
17     default  => sub { {} },
18     Class::MOP::_definition_context(),
19 ));
20
21 __PACKAGE__->meta->add_attribute('shadowing_prohibited' => (
22     init_arg => '-prohibit_shadowing',
23     reader => 'is_shadowing_prohibited',
24     default => sub { 0 },
25     Class::MOP::_definition_context(),
26 ));
27
28 sub new {
29     my ($class, %params) = @_;
30     $class->_new(\%params);
31 }
32
33 sub is_method_excluded {
34     my ($self, $method_name) = @_;
35     foreach (@{$self->get_method_exclusions}) {
36         return 1 if $_ eq $method_name;
37     }
38     return 0;
39 }
40
41 sub is_method_aliased {
42     my ($self, $method_name) = @_;
43     exists $self->get_method_aliases->{$method_name} ? 1 : 0
44 }
45
46 sub is_aliased_method {
47     my ($self, $method_name) = @_;
48     my %aliased_names = reverse %{$self->get_method_aliases};
49     exists $aliased_names{$method_name} ? 1 : 0;
50 }
51
52 sub apply {
53     my $self = shift;
54
55     $self->check_role_exclusions(@_);
56     $self->check_required_methods(@_);
57     $self->check_required_attributes(@_);
58
59     $self->apply_attributes(@_);
60     $self->apply_methods(@_);
61
62     $self->apply_override_method_modifiers(@_);
63
64     $self->apply_before_method_modifiers(@_);
65     $self->apply_around_method_modifiers(@_);
66     $self->apply_after_method_modifiers(@_);
67 }
68
69 sub check_role_exclusions           { Carp::croak "Abstract Method" }
70 sub check_required_methods          { Carp::croak "Abstract Method" }
71 sub check_required_attributes       { Carp::croak "Abstract Method" }
72
73 sub apply_attributes                { Carp::croak "Abstract Method" }
74 sub apply_methods                   { Carp::croak "Abstract Method" }
75 sub apply_override_method_modifiers { Carp::croak "Abstract Method" }
76 sub apply_method_modifiers          { Carp::croak "Abstract Method" }
77
78 sub apply_before_method_modifiers   { (shift)->apply_method_modifiers('before' => @_) }
79 sub apply_around_method_modifiers   { (shift)->apply_method_modifiers('around' => @_) }
80 sub apply_after_method_modifiers    { (shift)->apply_method_modifiers('after'  => @_) }
81
82 1;
83
84 # ABSTRACT: A base class for role application
85
86 __END__
87
88 =pod
89
90 =head1 DESCRIPTION
91
92 This is the abstract base class for role applications.
93
94 The API for this class and its subclasses still needs some
95 consideration, and is intentionally not yet documented.
96
97 =head2 METHODS
98
99 =over 4
100
101 =item B<new>
102
103 =item B<meta>
104
105 =item B<get_method_exclusions>
106
107 =item B<is_method_excluded>
108
109 =item B<get_method_aliases>
110
111 =item B<is_aliased_method>
112
113 =item B<is_method_aliased>
114
115 =item B<apply>
116
117 =item B<check_role_exclusions>
118
119 =item B<check_required_methods>
120
121 =item B<check_required_attributes>
122
123 =item B<apply_attributes>
124
125 =item B<apply_methods>
126
127 =item B<apply_method_modifiers>
128
129 =item B<apply_before_method_modifiers>
130
131 =item B<apply_after_method_modifiers>
132
133 =item B<apply_around_method_modifiers>
134
135 =item B<apply_override_method_modifiers>
136
137 =back
138
139 =head1 BUGS
140
141 See L<Moose/BUGS> for details on reporting bugs.
142
143 =cut
144