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 _make_accessors_if_moose {
62 my ($self, $role, $target) = @_;
63 if ($INFO{$role}{inhaled_from_moose}) {
64 if (my @attrs = @{$INFO{$role}{attributes}||[]}) {
65 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
66 require Method::Generate::Accessor;
67 Method::Generate::Accessor->new
69 while (my ($name, $spec) = splice @attrs, 0, 2) {
70 $acc_gen->generate_method($target, $name, $spec);
76 sub apply_single_role_to_package {
77 my ($me, $to, $role) = @_;
78 $me->_inhale_if_moose($role);
79 $me->_make_accessors_if_moose($role, $to);
80 $me->SUPER::apply_single_role_to_package($to, $role);
81 $me->_handle_constructor($to, $INFO{$role}{attributes});
84 sub create_class_with_roles {
85 my ($me, $superclass, @roles) = @_;
88 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
91 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
93 $me->_inhale_if_moose($_) for @roles;
97 $me->SUPER::create_class_with_roles($superclass, @roles);
99 foreach my $role (@roles) {
100 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
103 $Moo::MAKERS{$new_name} = {};
105 $me->_handle_constructor(
106 $new_name, [ map @{$INFO{$_}{attributes}||{}}, @roles ], $superclass
112 sub _composable_package_for {
113 my ($self, $role) = @_;
114 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
115 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
116 $self->_make_accessors_if_moose($role, $composed_name);
117 $self->SUPER::_composable_package_for($role);
120 sub _install_single_modifier {
121 my ($me, @args) = @_;
122 _install_modifier(@args);
125 sub _handle_constructor {
126 my ($me, $to, $attr_info, $superclass) = @_;
127 return unless $attr_info && @$attr_info;
129 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
131 # only fiddle with the constructor if the target is a Moo class
133 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
134 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
143 Moo::Role - Minimal Object Orientation support for Roles
167 # bar gets imported, but not foo
176 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
177 documentation on how this works. The main addition here is extra bits to make
178 the roles more "Moosey;" which is to say, it adds L</has>.
180 =head1 IMPORTED SUBROUTINES
182 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
183 imported by this module.
191 Declares an attribute for the class to be composed into. See
192 L<Moo/has> for all options.
196 See L<Moo> for authors.
198 =head1 COPYRIGHT AND LICENSE
200 See L<Moo> for the copyright and license.