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