Commit | Line | Data |
d245e471 |
1 | package Moo::Role; |
2 | |
3 | use strictures 1; |
4 | use Moo::_Utils; |
5 | use base qw(Role::Tiny); |
6 | |
a047096b |
7 | require Moo::sification; |
8 | |
d245e471 |
9 | BEGIN { *INFO = \%Role::Tiny::INFO } |
10 | |
11 | our %INFO; |
12 | |
13 | sub 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}::"} }; |
575ba24c |
19 | _install_coderef "${target}::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 |
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 ]; |
57d402ef |
45 | $INFO{$role}{attributes} = [ |
a84066c7 |
46 | map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list |
57d402ef |
47 | ]; |
a84066c7 |
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 | |
a41e15c3 |
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 | |
a84066c7 |
73 | sub _make_accessors_if_moose { |
74 | my ($self, $role, $target) = @_; |
75 | if ($INFO{$role}{inhaled_from_moose}) { |
a41e15c3 |
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 | }); |
db10ae2d |
86 | my $con_gen = $Moo::MAKERS{$target}{constructor}; |
a41e15c3 |
87 | my @attrs = @{$INFO{$role}{attributes}||[]}; |
88 | while (my ($name, $spec) = splice @attrs, 0, 2) { |
db10ae2d |
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 | } |
a41e15c3 |
93 | $acc_gen->generate_method($target, $name, $spec); |
a84066c7 |
94 | } |
95 | } |
96 | |
6893ea30 |
97 | sub apply_single_role_to_package { |
369a4c50 |
98 | my ($me, $to, $role) = @_; |
a84066c7 |
99 | $me->_inhale_if_moose($role); |
d245e471 |
100 | $me->_handle_constructor($to, $INFO{$role}{attributes}); |
a41e15c3 |
101 | $me->_maybe_make_accessors($role, $to); |
102 | $me->SUPER::apply_single_role_to_package($to, $role); |
d245e471 |
103 | } |
104 | |
105 | sub create_class_with_roles { |
106 | my ($me, $superclass, @roles) = @_; |
107 | |
c69190f1 |
108 | my $new_name = join( |
109 | '__WITH__', $superclass, my $compose_name = join '__AND__', @roles |
110 | ); |
111 | |
d245e471 |
112 | return $new_name if $Role::Tiny::COMPOSED{class}{$new_name}; |
113 | |
a84066c7 |
114 | $me->_inhale_if_moose($_) for @roles; |
115 | |
64284a1b |
116 | my $m; |
117 | if ($m = Moo->_accessor_maker_for($superclass) |
118 | and ref($m) ne 'Method::Generate::Accessor') { |
119 | # old fashioned way time. |
120 | *{_getglob("${new_name}::ISA")} = [ $superclass ]; |
121 | $me->apply_roles_to_package($new_name, @roles); |
122 | return $new_name; |
123 | } |
124 | |
faa9ce11 |
125 | require Sub::Quote; |
d245e471 |
126 | |
127 | $me->SUPER::create_class_with_roles($superclass, @roles); |
128 | |
129 | foreach my $role (@roles) { |
130 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
131 | } |
132 | |
c69190f1 |
133 | $Moo::MAKERS{$new_name} = {}; |
134 | |
d245e471 |
135 | $me->_handle_constructor( |
873df570 |
136 | $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass |
d245e471 |
137 | ); |
138 | |
139 | return $new_name; |
140 | } |
141 | |
a84066c7 |
142 | sub _composable_package_for { |
143 | my ($self, $role) = @_; |
144 | my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role; |
145 | return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name}; |
146 | $self->_make_accessors_if_moose($role, $composed_name); |
147 | $self->SUPER::_composable_package_for($role); |
148 | } |
149 | |
dccea57d |
150 | sub _install_single_modifier { |
151 | my ($me, @args) = @_; |
152 | _install_modifier(@args); |
d245e471 |
153 | } |
154 | |
155 | sub _handle_constructor { |
c4570291 |
156 | my ($me, $to, $attr_info, $superclass) = @_; |
57d402ef |
157 | return unless $attr_info && @$attr_info; |
d245e471 |
158 | if ($INFO{$to}) { |
57d402ef |
159 | push @{$INFO{$to}{attributes}||=[]}, @$attr_info; |
d245e471 |
160 | } else { |
161 | # only fiddle with the constructor if the target is a Moo class |
162 | if ($INC{"Moo.pm"} |
c4570291 |
163 | and my $con = Moo->_constructor_maker_for($to, $superclass)) { |
a41e15c3 |
164 | # shallow copy of the specs since the constructor will assign an index |
57d402ef |
165 | $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info); |
d245e471 |
166 | } |
167 | } |
168 | } |
169 | |
170 | 1; |
bce933ec |
171 | |
0b6e5fff |
172 | =head1 NAME |
173 | |
174 | Moo::Role - Minimal Object Orientation support for Roles |
bce933ec |
175 | |
176 | =head1 SYNOPSIS |
177 | |
178 | package My::Role; |
179 | |
180 | use Moo::Role; |
181 | |
182 | sub foo { ... } |
183 | |
184 | sub bar { ... } |
185 | |
186 | has baz => ( |
187 | is => 'ro', |
188 | ); |
189 | |
190 | 1; |
191 | |
192 | else where |
193 | |
194 | package Some::Class; |
195 | |
196 | use Moo; |
197 | |
198 | # bar gets imported, but not foo |
199 | with('My::Role'); |
200 | |
201 | sub foo { ... } |
202 | |
203 | 1; |
204 | |
205 | =head1 DESCRIPTION |
206 | |
207 | C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the |
208 | documentation on how this works. The main addition here is extra bits to make |
209 | the roles more "Moosey;" which is to say, it adds L</has>. |
210 | |
211 | =head1 IMPORTED SUBROUTINES |
212 | |
213 | See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are |
214 | imported by this module. |
215 | |
216 | =head2 has |
217 | |
218 | has attr => ( |
219 | is => 'ro', |
220 | ); |
221 | |
222 | Declares an attribute for the class to be composed into. See |
223 | L<Moo/has> for all options. |
40f3e3aa |
224 | |
225 | =head1 AUTHORS |
226 | |
227 | See L<Moo> for authors. |
228 | |
229 | =head1 COPYRIGHT AND LICENSE |
230 | |
231 | See L<Moo> for the copyright and license. |
232 | |
233 | =cut |