pass spec down into generation code
[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
7BEGIN { *INFO = \%Role::Tiny::INFO }
8
9our %INFO;
10
11sub import {
12 my $target = caller;
13 strictures->import;
1ba11455 14 return if $INFO{$target}; # already exported into this package
d245e471 15 # get symbol table reference
16 my $stash = do { no strict 'refs'; \%{"${target}::"} };
575ba24c 17 _install_coderef "${target}::has" => sub {
d245e471 18 my ($name, %spec) = @_;
19 ($INFO{$target}{accessor_maker} ||= do {
faa9ce11 20 require Method::Generate::Accessor;
d245e471 21 Method::Generate::Accessor->new
22 })->generate_method($target, $name, \%spec);
57d402ef 23 push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
d245e471 24 };
7f9775b1 25 if ($INC{'Moo/HandleMoose.pm'}) {
26 Moo::HandleMoose::inject_fake_metaclass_for($target);
27 }
d245e471 28 goto &Role::Tiny::import;
29}
30
a84066c7 31sub _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 ];
57d402ef 43 $INFO{$role}{attributes} = [
a84066c7 44 map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
57d402ef 45 ];
a84066c7 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
a41e15c3 61sub _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
a84066c7 71sub _make_accessors_if_moose {
72 my ($self, $role, $target) = @_;
73 if ($INFO{$role}{inhaled_from_moose}) {
a41e15c3 74 $self->_make_accessors($role, $target);
75 }
76}
77
78sub _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);
a84066c7 87 }
88}
89
6893ea30 90sub apply_single_role_to_package {
369a4c50 91 my ($me, $to, $role) = @_;
a84066c7 92 $me->_inhale_if_moose($role);
d245e471 93 $me->_handle_constructor($to, $INFO{$role}{attributes});
a41e15c3 94 $me->_maybe_make_accessors($role, $to);
95 $me->SUPER::apply_single_role_to_package($to, $role);
d245e471 96}
97
98sub create_class_with_roles {
99 my ($me, $superclass, @roles) = @_;
100
c69190f1 101 my $new_name = join(
102 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
103 );
104
d245e471 105 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
106
a84066c7 107 $me->_inhale_if_moose($_) for @roles;
108
faa9ce11 109 require Sub::Quote;
d245e471 110
111 $me->SUPER::create_class_with_roles($superclass, @roles);
112
113 foreach my $role (@roles) {
114 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
115 }
116
c69190f1 117 $Moo::MAKERS{$new_name} = {};
118
d245e471 119 $me->_handle_constructor(
57d402ef 120 $new_name, [ map @{$INFO{$_}{attributes}||{}}, @roles ], $superclass
d245e471 121 );
122
123 return $new_name;
124}
125
a84066c7 126sub _composable_package_for {
127 my ($self, $role) = @_;
128 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
129 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
130 $self->_make_accessors_if_moose($role, $composed_name);
131 $self->SUPER::_composable_package_for($role);
132}
133
dccea57d 134sub _install_single_modifier {
135 my ($me, @args) = @_;
136 _install_modifier(@args);
d245e471 137}
138
139sub _handle_constructor {
c4570291 140 my ($me, $to, $attr_info, $superclass) = @_;
57d402ef 141 return unless $attr_info && @$attr_info;
d245e471 142 if ($INFO{$to}) {
57d402ef 143 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
d245e471 144 } else {
145 # only fiddle with the constructor if the target is a Moo class
146 if ($INC{"Moo.pm"}
c4570291 147 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
a41e15c3 148 # shallow copy of the specs since the constructor will assign an index
57d402ef 149 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
d245e471 150 }
151 }
152}
153
1541;
bce933ec 155
0b6e5fff 156=head1 NAME
157
158Moo::Role - Minimal Object Orientation support for Roles
bce933ec 159
160=head1 SYNOPSIS
161
162 package My::Role;
163
164 use Moo::Role;
165
166 sub foo { ... }
167
168 sub bar { ... }
169
170 has baz => (
171 is => 'ro',
172 );
173
174 1;
175
176else where
177
178 package Some::Class;
179
180 use Moo;
181
182 # bar gets imported, but not foo
183 with('My::Role');
184
185 sub foo { ... }
186
187 1;
188
189=head1 DESCRIPTION
190
191C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
192documentation on how this works. The main addition here is extra bits to make
193the roles more "Moosey;" which is to say, it adds L</has>.
194
195=head1 IMPORTED SUBROUTINES
196
197See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
198imported by this module.
199
200=head2 has
201
202 has attr => (
203 is => 'ro',
204 );
205
206Declares an attribute for the class to be composed into. See
207L<Moo/has> for all options.
40f3e3aa 208
209=head1 AUTHORS
210
211See L<Moo> for authors.
212
213=head1 COPYRIGHT AND LICENSE
214
215See L<Moo> for the copyright and license.
216
217=cut