Commit | Line | Data |
d245e471 |
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; |
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 |
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 ]; |
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 |
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 | |
a84066c7 |
71 | sub _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 | |
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); |
a84066c7 |
87 | } |
88 | } |
89 | |
6893ea30 |
90 | sub 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 | |
98 | sub 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 | |
64284a1b |
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 | |
faa9ce11 |
118 | require Sub::Quote; |
d245e471 |
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 | |
c69190f1 |
126 | $Moo::MAKERS{$new_name} = {}; |
127 | |
d245e471 |
128 | $me->_handle_constructor( |
873df570 |
129 | $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass |
d245e471 |
130 | ); |
131 | |
132 | return $new_name; |
133 | } |
134 | |
a84066c7 |
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 | |
dccea57d |
143 | sub _install_single_modifier { |
144 | my ($me, @args) = @_; |
145 | _install_modifier(@args); |
d245e471 |
146 | } |
147 | |
148 | sub _handle_constructor { |
c4570291 |
149 | my ($me, $to, $attr_info, $superclass) = @_; |
57d402ef |
150 | return unless $attr_info && @$attr_info; |
d245e471 |
151 | if ($INFO{$to}) { |
57d402ef |
152 | push @{$INFO{$to}{attributes}||=[]}, @$attr_info; |
d245e471 |
153 | } else { |
154 | # only fiddle with the constructor if the target is a Moo class |
155 | if ($INC{"Moo.pm"} |
c4570291 |
156 | and my $con = Moo->_constructor_maker_for($to, $superclass)) { |
a41e15c3 |
157 | # shallow copy of the specs since the constructor will assign an index |
57d402ef |
158 | $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info); |
d245e471 |
159 | } |
160 | } |
161 | } |
162 | |
163 | 1; |
bce933ec |
164 | |
0b6e5fff |
165 | =head1 NAME |
166 | |
167 | Moo::Role - Minimal Object Orientation support for Roles |
bce933ec |
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. |
40f3e3aa |
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 |