5 use base qw(Role::Tiny);
7 BEGIN { *INFO = \%Role::Tiny::INFO }
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;
25 if ($INC{'Moo/HandleMoose.pm'}) {
26 Moo::HandleMoose::inject_fake_metaclass_for($target);
28 goto &Role::Tiny::import;
31 sub _inhale_if_moose {
32 my ($self, $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
39 $Role::Tiny::APPLIED_TO{$role} = {
40 map +($_->name => 1), $meta->calculate_all_roles
42 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
43 $INFO{$role}{attributes} = [
44 map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
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 ];
55 require Class::Method::Modifiers if @$mods;
56 $INFO{$role}{inhaled_from_moose} = 1;
61 sub _maybe_make_accessors {
62 my ($self, $role, $target) = @_;
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);
71 sub _make_accessors_if_moose {
72 my ($self, $role, $target) = @_;
73 if ($INFO{$role}{inhaled_from_moose}) {
74 $self->_make_accessors($role, $target);
79 my ($self, $role, $target) = @_;
80 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
81 require Method::Generate::Accessor;
82 Method::Generate::Accessor->new
84 my @attrs = @{$INFO{$role}{attributes}||[]};
85 while (my ($name, $spec) = splice @attrs, 0, 2) {
86 $acc_gen->generate_method($target, $name, $spec);
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);
98 sub create_class_with_roles {
99 my ($me, $superclass, @roles) = @_;
102 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
105 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
107 $me->_inhale_if_moose($_) for @roles;
111 $me->SUPER::create_class_with_roles($superclass, @roles);
113 foreach my $role (@roles) {
114 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
117 $Moo::MAKERS{$new_name} = {};
119 $me->_handle_constructor(
120 $new_name, [ map @{$INFO{$_}{attributes}||{}}, @roles ], $superclass
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);
134 sub _install_single_modifier {
135 my ($me, @args) = @_;
136 _install_modifier(@args);
139 sub _handle_constructor {
140 my ($me, $to, $attr_info, $superclass) = @_;
141 return unless $attr_info && @$attr_info;
143 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
145 # only fiddle with the constructor if the target is a Moo class
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);
158 Moo::Role - Minimal Object Orientation support for Roles
182 # bar gets imported, but not foo
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>.
195 =head1 IMPORTED SUBROUTINES
197 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
198 imported by this module.
206 Declares an attribute for the class to be composed into. See
207 L<Moo/has> for all options.
211 See L<Moo> for authors.
213 =head1 COPYRIGHT AND LICENSE
215 See L<Moo> for the copyright and license.