Another broken case where Moo::Role, then namespace::autoclean get loaded
[gitmo/Moo.git] / lib / Moo / Role.pm
CommitLineData
d245e471 1package Moo::Role;
2
3use strictures 1;
4use Moo::_Utils;
5use base qw(Role::Tiny);
6
a047096b 7require Moo::sification;
8
d245e471 9BEGIN { *INFO = \%Role::Tiny::INFO }
10
11our %INFO;
12
13sub import {
14 my $target = caller;
15 strictures->import;
1ba11455 16 return if $INFO{$target}; # already exported into this package
d245e471 17 # get symbol table reference
18 my $stash = do { no strict 'refs'; \%{"${target}::"} };
167455a0 19 _install_coderef "${target}::has" => "Moo::Role::has" => sub {
d245e471 20 my ($name, %spec) = @_;
21 ($INFO{$target}{accessor_maker} ||= do {
faa9ce11 22 require Method::Generate::Accessor;
d245e471 23 Method::Generate::Accessor->new
24 })->generate_method($target, $name, \%spec);
57d402ef 25 push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
d245e471 26 };
7f9775b1 27 if ($INC{'Moo/HandleMoose.pm'}) {
28 Moo::HandleMoose::inject_fake_metaclass_for($target);
29 }
d245e471 30 goto &Role::Tiny::import;
31}
32
a84066c7 33sub _inhale_if_moose {
34 my ($self, $role) = @_;
35 _load_module($role);
36 if (!$INFO{$role} and $INC{"Moose.pm"}) {
37 if (my $meta = Class::MOP::class_of($role)) {
38 $INFO{$role}{methods} = {
a3411285 39 map +($_ => $role->can($_)),
40 grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'),
41 $meta->get_method_list
a84066c7 42 };
43 $Role::Tiny::APPLIED_TO{$role} = {
44 map +($_->name => 1), $meta->calculate_all_roles
45 };
46 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
57d402ef 47 $INFO{$role}{attributes} = [
a84066c7 48 map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
57d402ef 49 ];
a84066c7 50 my $mods = $INFO{$role}{modifiers} = [];
51 foreach my $type (qw(before after around)) {
52 my $map = $meta->${\"get_${type}_method_modifiers_map"};
53 foreach my $method (keys %$map) {
54 foreach my $mod (@{$map->{$method}}) {
55 push @$mods, [ $type => $method => $mod ];
56 }
57 }
58 }
59 require Class::Method::Modifiers if @$mods;
60 $INFO{$role}{inhaled_from_moose} = 1;
61 }
62 }
63}
64
a41e15c3 65sub _maybe_make_accessors {
66 my ($self, $role, $target) = @_;
67 my $m;
68 if ($INFO{$role}{inhaled_from_moose}
69 or $m = Moo->_accessor_maker_for($target)
70 and ref($m) ne 'Method::Generate::Accessor') {
71 $self->_make_accessors($role, $target);
72 }
73}
74
a84066c7 75sub _make_accessors_if_moose {
76 my ($self, $role, $target) = @_;
77 if ($INFO{$role}{inhaled_from_moose}) {
a41e15c3 78 $self->_make_accessors($role, $target);
79 }
80}
81
82sub _make_accessors {
83 my ($self, $role, $target) = @_;
84 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
85 require Method::Generate::Accessor;
86 Method::Generate::Accessor->new
87 });
db10ae2d 88 my $con_gen = $Moo::MAKERS{$target}{constructor};
a41e15c3 89 my @attrs = @{$INFO{$role}{attributes}||[]};
90 while (my ($name, $spec) = splice @attrs, 0, 2) {
db10ae2d 91 # needed to ensure we got an index for an arrayref based generator
92 if ($con_gen) {
93 $spec = $con_gen->all_attribute_specs->{$name};
94 }
a41e15c3 95 $acc_gen->generate_method($target, $name, $spec);
a84066c7 96 }
97}
98
4a79464d 99sub apply_roles_to_package {
100 my ($me, $to, @roles) = @_;
101 $me->_inhale_if_moose($_) for @roles;
102 $me->SUPER::apply_roles_to_package($to, @roles);
103}
104
6893ea30 105sub apply_single_role_to_package {
369a4c50 106 my ($me, $to, $role) = @_;
a84066c7 107 $me->_inhale_if_moose($role);
d245e471 108 $me->_handle_constructor($to, $INFO{$role}{attributes});
a41e15c3 109 $me->_maybe_make_accessors($role, $to);
110 $me->SUPER::apply_single_role_to_package($to, $role);
d245e471 111}
112
113sub create_class_with_roles {
114 my ($me, $superclass, @roles) = @_;
115
c69190f1 116 my $new_name = join(
117 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
118 );
119
d245e471 120 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
121
a84066c7 122 $me->_inhale_if_moose($_) for @roles;
123
64284a1b 124 my $m;
125 if ($m = Moo->_accessor_maker_for($superclass)
126 and ref($m) ne 'Method::Generate::Accessor') {
127 # old fashioned way time.
128 *{_getglob("${new_name}::ISA")} = [ $superclass ];
129 $me->apply_roles_to_package($new_name, @roles);
130 return $new_name;
131 }
132
faa9ce11 133 require Sub::Quote;
d245e471 134
135 $me->SUPER::create_class_with_roles($superclass, @roles);
136
137 foreach my $role (@roles) {
138 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
139 }
140
c69190f1 141 $Moo::MAKERS{$new_name} = {};
142
d245e471 143 $me->_handle_constructor(
873df570 144 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
d245e471 145 );
146
147 return $new_name;
148}
149
a84066c7 150sub _composable_package_for {
151 my ($self, $role) = @_;
152 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
153 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
154 $self->_make_accessors_if_moose($role, $composed_name);
155 $self->SUPER::_composable_package_for($role);
156}
157
dccea57d 158sub _install_single_modifier {
159 my ($me, @args) = @_;
160 _install_modifier(@args);
d245e471 161}
162
163sub _handle_constructor {
c4570291 164 my ($me, $to, $attr_info, $superclass) = @_;
57d402ef 165 return unless $attr_info && @$attr_info;
d245e471 166 if ($INFO{$to}) {
57d402ef 167 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
d245e471 168 } else {
169 # only fiddle with the constructor if the target is a Moo class
170 if ($INC{"Moo.pm"}
c4570291 171 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
a41e15c3 172 # shallow copy of the specs since the constructor will assign an index
57d402ef 173 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
d245e471 174 }
175 }
176}
177
1781;
bce933ec 179
0b6e5fff 180=head1 NAME
181
182Moo::Role - Minimal Object Orientation support for Roles
bce933ec 183
184=head1 SYNOPSIS
185
186 package My::Role;
187
188 use Moo::Role;
189
190 sub foo { ... }
191
192 sub bar { ... }
193
194 has baz => (
195 is => 'ro',
196 );
197
198 1;
199
200else where
201
202 package Some::Class;
203
204 use Moo;
205
206 # bar gets imported, but not foo
207 with('My::Role');
208
209 sub foo { ... }
210
211 1;
212
213=head1 DESCRIPTION
214
215C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
216documentation on how this works. The main addition here is extra bits to make
217the roles more "Moosey;" which is to say, it adds L</has>.
218
219=head1 IMPORTED SUBROUTINES
220
221See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
222imported by this module.
223
224=head2 has
225
226 has attr => (
227 is => 'ro',
228 );
229
230Declares an attribute for the class to be composed into. See
231L<Moo/has> for all options.
40f3e3aa 232
233=head1 AUTHORS
234
235See L<Moo> for authors.
236
237=head1 COPYRIGHT AND LICENSE
238
239See L<Moo> for the copyright and license.
240
241=cut