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