first cut at extension tests
[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   require Sub::Quote;
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
117   $Moo::MAKERS{$new_name} = {};
118
119   $me->_handle_constructor(
120     $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
121   );
122
123   return $new_name;
124 }
125
126 sub _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
134 sub _install_single_modifier {
135   my ($me, @args) = @_;
136   _install_modifier(@args);
137 }
138
139 sub _handle_constructor {
140   my ($me, $to, $attr_info, $superclass) = @_;
141   return unless $attr_info && @$attr_info;
142   if ($INFO{$to}) {
143     push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
144   } else {
145     # only fiddle with the constructor if the target is a Moo class
146     if ($INC{"Moo.pm"}
147         and my $con = Moo->_constructor_maker_for($to, $superclass)) {
148       # shallow copy of the specs since the constructor will assign an index
149       $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
150     }
151   }
152 }
153
154 1;
155
156 =head1 NAME
157
158 Moo::Role - Minimal Object Orientation support for Roles
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
176 else 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
191 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
192 documentation on how this works.  The main addition here is extra bits to make
193 the roles more "Moosey;" which is to say, it adds L</has>.
194
195 =head1 IMPORTED SUBROUTINES
196
197 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
198 imported by this module.
199
200 =head2 has
201
202  has attr => (
203    is => 'ro',
204  );
205
206 Declares an attribute for the class to be composed into.  See
207 L<Moo/has> for all options.
208
209 =head1 AUTHORS
210
211 See L<Moo> for authors.
212
213 =head1 COPYRIGHT AND LICENSE
214
215 See L<Moo> for the copyright and license.
216
217 =cut