1 package Moose::Meta::Role::Application::RoleSummation;
8 use Scalar::Util 'blessed';
11 use Moose::Meta::Role::Composite;
13 our $VERSION = '0.01';
14 our $AUTHORITY = 'cpan:STEVAN';
16 use base 'Moose::Meta::Role::Application';
18 # stolen from List::MoreUtils ...
19 my $uniq = sub { my %h; map { $h{$_}++ == 0 ? $_ : () } @_ };
21 sub check_role_exclusions {
24 my @all_excluded_roles = $uniq->(map {
25 $_->get_excluded_roles_list
28 foreach my $role (@{$c->get_roles}) {
29 foreach my $excluded (@all_excluded_roles) {
30 confess "Conflict detected: " . $role->name . " excludes role '" . $excluded . "'"
31 if $role->does_role($excluded);
35 $c->add_excluded_roles(@all_excluded_roles);
38 sub check_required_methods {
41 my %all_required_methods = map { $_ => undef } $uniq->(map {
42 $_->get_required_method_list
45 foreach my $role (@{$c->get_roles}) {
46 foreach my $required (keys %all_required_methods) {
47 delete $all_required_methods{$required}
48 if $role->has_method($required);
52 $c->add_required_methods(keys %all_required_methods);
55 sub apply_attributes {
58 my @all_attributes = map {
63 attr => $role->get_attribute($_),
65 } $role->get_attribute_list
69 foreach my $attr (@all_attributes) {
70 if (exists $seen{$attr->{name}}) {
71 confess "We have encountered an attribute conflict with '" . $attr->{name} . "'"
72 . "during composition. This is fatal error and cannot be disambiguated."
73 if $seen{$attr->{name}} != $attr->{attr};
75 $seen{$attr->{name}} = $attr->{attr};
78 foreach my $attr (@all_attributes) {
79 $c->add_attribute($attr->{name}, $attr->{attr});
86 my @all_methods = map {
91 method => $role->get_method($_),
93 } $role->get_method_list;
96 my (%seen, %method_map);
97 foreach my $method (@all_methods) {
98 if (exists $seen{$method->{name}}) {
99 if ($seen{$method->{name}}->body != $method->{method}->body) {
100 $c->add_required_methods($method->{name});
101 delete $method_map{$method->{name}};
105 $seen{$method->{name}} = $method->{method};
106 $method_map{$method->{name}} = $method->{method};
109 $c->alias_method($_ => $method_map{$_}) for keys %method_map;
112 sub apply_override_method_modifiers {
115 my @all_overrides = map {
120 method => $role->get_override_method_modifier($_),
122 } $role->get_method_modifier_list('override');
126 foreach my $override (@all_overrides) {
127 confess "Role '" . $c->name . "' has encountered an 'override' method conflict " .
128 "during composition (A local method of the same name as been found). This " .
130 if $c->has_method($override->{name});
131 if (exists $seen{$override->{name}}) {
132 confess "We have encountered an 'override' method conflict during " .
133 "composition (Two 'override' methods of the same name encountered). " .
134 "This is fatal error."
135 if $seen{$override->{name}} != $override->{method};
137 $seen{$override->{name}} = $override->{method};
140 $c->add_override_method_modifier(
141 $_->{name}, $_->{method}
142 ) for @all_overrides;
146 sub apply_method_modifiers {
147 my ($self, $modifier_type, $c) = @_;
148 my $add = "add_${modifier_type}_method_modifier";
149 my $get = "get_${modifier_type}_method_modifiers";
150 foreach my $role (@{$c->get_roles}) {
151 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
155 ) foreach $role->$get($method_name);
168 Moose::Meta::Role::Application::RoleSummation
172 Summation composes two traits, forming the union of non-conflicting
173 bindings and 'disabling' the conflicting bindings
185 =item B<check_required_methods>
187 =item B<check_role_exclusions>
189 =item B<apply_attributes>
191 =item B<apply_methods>
193 =item B<apply_method_modifiers>
195 =item B<apply_override_method_modifiers>
201 All complex software has bugs lurking in it, and this module is no
202 exception. If you find a bug please either email me, or add the bug
207 Stevan Little E<lt>stevan@iinteractive.comE<gt>
209 =head1 COPYRIGHT AND LICENSE
211 Copyright 2006-2008 by Infinity Interactive, Inc.
213 L<http://www.iinteractive.com>
215 This library is free software; you can redistribute it and/or modify
216 it under the same terms as Perl itself.