remember to register roles with newly created metaclass
[gitmo/Moo.git] / lib / Moo / Role.pm
CommitLineData
d245e471 1package Moo::Role;
2
3use strictures 1;
4use Moo::_Utils;
5use base qw(Role::Tiny);
6
7BEGIN { *INFO = \%Role::Tiny::INFO }
8
9our %INFO;
10
11sub 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}::"} };
17 *{_getglob "${target}::has"} = sub {
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);
23 $INFO{$target}{attributes}{$name} = \%spec;
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
31sub apply_role_to_package {
369a4c50 32 my ($me, $to, $role) = @_;
33 $me->SUPER::apply_role_to_package($to, $role);
d245e471 34 $me->_handle_constructor($to, $INFO{$role}{attributes});
35}
36
37sub create_class_with_roles {
38 my ($me, $superclass, @roles) = @_;
39
c69190f1 40 my $new_name = join(
41 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
42 );
43
d245e471 44 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
45
faa9ce11 46 require Sub::Quote;
d245e471 47
48 $me->SUPER::create_class_with_roles($superclass, @roles);
49
50 foreach my $role (@roles) {
51 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
52 }
53
c69190f1 54 $Moo::MAKERS{$new_name} = {};
55
d245e471 56 $me->_handle_constructor(
c4570291 57 $new_name, { map %{$INFO{$_}{attributes}||{}}, @roles }, $superclass
d245e471 58 );
59
60 return $new_name;
61}
62
dccea57d 63sub _install_single_modifier {
64 my ($me, @args) = @_;
65 _install_modifier(@args);
d245e471 66}
67
68sub _handle_constructor {
c4570291 69 my ($me, $to, $attr_info, $superclass) = @_;
d245e471 70 return unless $attr_info && keys %$attr_info;
71 if ($INFO{$to}) {
72 @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info;
73 } else {
74 # only fiddle with the constructor if the target is a Moo class
75 if ($INC{"Moo.pm"}
c4570291 76 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
d245e471 77 $con->register_attribute_specs(%$attr_info);
78 }
79 }
80}
81
821;
bce933ec 83
0b6e5fff 84=head1 NAME
85
86Moo::Role - Minimal Object Orientation support for Roles
bce933ec 87
88=head1 SYNOPSIS
89
90 package My::Role;
91
92 use Moo::Role;
93
94 sub foo { ... }
95
96 sub bar { ... }
97
98 has baz => (
99 is => 'ro',
100 );
101
102 1;
103
104else where
105
106 package Some::Class;
107
108 use Moo;
109
110 # bar gets imported, but not foo
111 with('My::Role');
112
113 sub foo { ... }
114
115 1;
116
117=head1 DESCRIPTION
118
119C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
120documentation on how this works. The main addition here is extra bits to make
121the roles more "Moosey;" which is to say, it adds L</has>.
122
123=head1 IMPORTED SUBROUTINES
124
125See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
126imported by this module.
127
128=head2 has
129
130 has attr => (
131 is => 'ro',
132 );
133
134Declares an attribute for the class to be composed into. See
135L<Moo/has> for all options.
40f3e3aa 136
137=head1 AUTHORS
138
139See L<Moo> for authors.
140
141=head1 COPYRIGHT AND LICENSE
142
143See L<Moo> for the copyright and license.
144
145=cut