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 check_required_attributes {
59 sub apply_attributes {
62 my @all_attributes = map {
67 attr => $role->get_attribute($_),
69 } $role->get_attribute_list
73 foreach my $attr (@all_attributes) {
74 if (exists $seen{$attr->{name}}) {
75 confess "We have encountered an attribute conflict with '" . $attr->{name} . "'"
76 . "during composition. This is fatal error and cannot be disambiguated."
77 if $seen{$attr->{name}} != $attr->{attr};
79 $seen{$attr->{name}} = $attr->{attr};
82 foreach my $attr (@all_attributes) {
83 $c->add_attribute($attr->{name}, $attr->{attr});
90 my @all_methods = map {
95 method => $role->get_method($_),
97 } $role->get_method_list;
100 my (%seen, %method_map);
101 foreach my $method (@all_methods) {
102 if (exists $seen{$method->{name}}) {
103 if ($seen{$method->{name}}->body != $method->{method}->body) {
104 $c->add_required_methods($method->{name});
105 delete $method_map{$method->{name}};
109 $seen{$method->{name}} = $method->{method};
110 $method_map{$method->{name}} = $method->{method};
113 $c->alias_method($_ => $method_map{$_}) for keys %method_map;
116 sub apply_override_method_modifiers {
119 my @all_overrides = map {
124 method => $role->get_override_method_modifier($_),
126 } $role->get_method_modifier_list('override');
130 foreach my $override (@all_overrides) {
131 confess "Role '" . $c->name . "' has encountered an 'override' method conflict " .
132 "during composition (A local method of the same name as been found). This " .
134 if $c->has_method($override->{name});
135 if (exists $seen{$override->{name}}) {
136 confess "We have encountered an 'override' method conflict during " .
137 "composition (Two 'override' methods of the same name encountered). " .
138 "This is fatal error."
139 if $seen{$override->{name}} != $override->{method};
141 $seen{$override->{name}} = $override->{method};
144 $c->add_override_method_modifier(
145 $_->{name}, $_->{method}
146 ) for @all_overrides;
150 sub apply_method_modifiers {
151 my ($self, $modifier_type, $c) = @_;
152 my $add = "add_${modifier_type}_method_modifier";
153 my $get = "get_${modifier_type}_method_modifiers";
154 foreach my $role (@{$c->get_roles}) {
155 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
159 ) foreach $role->$get($method_name);
172 Moose::Meta::Role::Application::RoleSummation
176 Summation composes two traits, forming the union of non-conflicting
177 bindings and 'disabling' the conflicting bindings
189 =item B<check_role_exclusions>
191 =item B<check_required_methods>
193 =item B<check_required_attributes>
195 =item B<apply_attributes>
197 =item B<apply_methods>
199 =item B<apply_method_modifiers>
201 =item B<apply_override_method_modifiers>
207 All complex software has bugs lurking in it, and this module is no
208 exception. If you find a bug please either email me, or add the bug
213 Stevan Little E<lt>stevan@iinteractive.comE<gt>
215 =head1 COPYRIGHT AND LICENSE
217 Copyright 2006-2008 by Infinity Interactive, Inc.
219 L<http://www.iinteractive.com>
221 This library is free software; you can redistribute it and/or modify
222 it under the same terms as Perl itself.