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