Make roles smarter
[gitmo/Mouse.git] / lib / Mouse / Exporter.pm
CommitLineData
db9fc237 1package Mouse::Exporter;
2use strict;
3use warnings;
4
5use Carp qw(confess);
6
db9fc237 7my %SPEC;
8
99934a1b 9use constant _strict_bits => strict::bits(qw(subs refs vars));
1ff34b4c 10
0eb86915 11# it must be "require", because Mouse::Util depends on Mouse::Exporter,
12# which depends on Mouse::Util::import()
13require Mouse::Util;
14
1bd3c83e 15sub import{
99934a1b 16 $^H |= _strict_bits; # strict->import;
4b55a023 17 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
1bd3c83e 18 return;
19}
20
0eb86915 21
db9fc237 22sub setup_import_methods{
23 my($class, %args) = @_;
24
25 my $exporting_package = $args{exporting_package} ||= caller();
26
e6dc493d 27 my($import, $unimport) = $class->build_import_methods(%args);
28
29 no strict 'refs';
30
31 *{$exporting_package . '::import'} = $import;
32 *{$exporting_package . '::unimport'} = $unimport;
33
34 # for backward compatibility
35 *{$exporting_package . '::export_to_level'} = sub{
36 my($package, $level, undef, @args) = @_; # the third argument is redundant
37 $package->import({ into_level => $level + 1 }, @args);
38 };
39 *{$exporting_package . '::export'} = sub{
40 my($package, $into, @args) = @_;
41 $package->import({ into => $into }, @args);
42 };
43 return;
44}
45
46sub build_import_methods{
47 my($class, %args) = @_;
48
49 my $exporting_package = $args{exporting_package} ||= caller();
50
db9fc237 51 $SPEC{$exporting_package} = \%args;
52
53 # canonicalize args
54 my @export_from;
55 if($args{also}){
56 my %seen;
57 my @stack = ($exporting_package);
58
59 while(my $current = shift @stack){
60 push @export_from, $current;
61
62 my $also = $SPEC{$current}{also} or next;
0eb86915 63 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
db9fc237 64 }
65 }
66 else{
67 @export_from = ($exporting_package);
68 }
69
72dc2c9d 70 my %exports;
71 my @removables;
72 my @all;
73
74 my @init_meta_methods;
75
76 foreach my $package(@export_from){
77 my $spec = $SPEC{$package} or next;
1ff34b4c 78
72dc2c9d 79 if(my $as_is = $spec->{as_is}){
80 foreach my $thingy (@{$as_is}){
81 my($code_package, $code_name, $code);
82
83 if(ref($thingy)){
84 $code = $thingy;
85 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
86 }
87 else{
88 no strict 'refs';
89 $code_package = $package;
90 $code_name = $thingy;
91 $code = \&{ $code_package . '::' . $code_name };
92 }
93
94 push @all, $code_name;
95 $exports{$code_name} = $code;
96 if($code_package eq $package){
97 push @removables, $code_name;
1ff34b4c 98 }
99 }
db9fc237 100 }
1ff34b4c 101
72dc2c9d 102 if(my $init_meta = $package->can('init_meta')){
103 if(!grep{ $_ == $init_meta } @init_meta_methods){
104 push @init_meta_methods, $init_meta;
1ff34b4c 105 }
1ff34b4c 106 }
72dc2c9d 107 }
108 $args{EXPORTS} = \%exports;
109 $args{REMOVABLES} = \@removables;
1ff34b4c 110
72dc2c9d 111 $args{groups}{all} ||= \@all;
112
113 if(my $default_list = $args{groups}{default}){
114 my %default;
115 foreach my $keyword(@{$default_list}){
116 $default{$keyword} = $exports{$keyword}
117 || confess(qq{The $exporting_package package does not export "$keyword"});
1ff34b4c 118 }
72dc2c9d 119 $args{DEFAULT} = \%default;
120 }
121 else{
122 $args{groups}{default} ||= \@all;
123 $args{DEFAULT} = $args{EXPORTS};
124 }
125
126 if(@init_meta_methods){
127 $args{INIT_META} = \@init_meta_methods;
db9fc237 128 }
129
e6dc493d 130 return (\&do_import, \&do_unimport);
db9fc237 131}
132
db9fc237 133
134# the entity of general import()
135sub do_import {
1ff34b4c 136 my($package, @args) = @_;
db9fc237 137
1ff34b4c 138 my $spec = $SPEC{$package}
139 || confess("The package $package package does not use Mouse::Exporter");
db9fc237 140
141 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
142
143 my @exports;
45bbec05 144 my @traits;
8cbcbb47 145
45bbec05 146 while(@args){
147 my $arg = shift @args;
1ff34b4c 148 if($arg =~ s/^-//){
45bbec05 149 if($arg eq 'traits'){
f12892e5 150 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
45bbec05 151 }
152 else {
153 Mouse::Util::not_supported("-$arg");
154 }
1ff34b4c 155 }
156 elsif($arg =~ s/^://){
1bd3c83e 157 my $group = $spec->{groups}{$arg}
1ff34b4c 158 || confess(qq{The $package package does not export the group "$arg"});
db9fc237 159 push @exports, @{$group};
160 }
161 else{
162 push @exports, $arg;
163 }
164 }
165
99934a1b 166 $^H |= _strict_bits; # strict->import;
4b55a023 167 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
db9fc237 168
1ff34b4c 169 if($spec->{INIT_META}){
45bbec05 170 my $meta;
1ff34b4c 171 foreach my $init_meta(@{$spec->{INIT_META}}){
134f7bcb 172 $meta = $package->$init_meta(for_class => $into);
1ff34b4c 173 }
db9fc237 174
45bbec05 175 if(@traits){
176 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
177 @traits =
f12892e5 178 map{
179 ref($_) ? $_
180 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
181 } @traits;
45bbec05 182
45bbec05 183 require Mouse::Util::MetaRole;
3d7c6ec9 184 Mouse::Util::MetaRole::apply_metaroles(
185 for => $into,
186 Mouse::Util::is_a_metarole($into->meta)
187 ? (role_metaroles => { role => \@traits })
188 : (class_metaroles => { class => \@traits }),
45bbec05 189 );
190 }
db9fc237 191 }
f12892e5 192 elsif(@traits){
193 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
194 }
db9fc237 195
1ff34b4c 196 if(@exports){
197 foreach my $keyword(@exports){
198 no strict 'refs';
199 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
200 || confess(qq{The $package package does not export "$keyword"});
201 }
202 }
203 else{
204 my $default = $spec->{DEFAULT};
205 while(my($keyword, $code) = each %{$default}){
206 no strict 'refs';
207 *{$into.'::'.$keyword} = $code;
208 }
db9fc237 209 }
210 return;
211}
212
213# the entity of general unimport()
214sub do_unimport {
1ff34b4c 215 my($package, $arg) = @_;
db9fc237 216
1ff34b4c 217 my $spec = $SPEC{$package}
218 || confess("The package $package does not use Mouse::Exporter");
db9fc237 219
220 my $from = _get_caller_package($arg);
221
222 my $stash = do{
223 no strict 'refs';
224 \%{$from . '::'}
225 };
226
227 for my $keyword (@{ $spec->{REMOVABLES} }) {
21df8e42 228 next if !exists $stash->{$keyword};
4ee3190e 229 my $gv = \$stash->{$keyword};
230 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
231 delete $stash->{$keyword};
232 }
db9fc237 233 }
234 return;
235}
236
237sub _get_caller_package {
238 my($arg) = @_;
239
7ad9df77 240 # We need one extra level because it's called by import so there's a layer
241 # of indirection
db9fc237 242 if(ref $arg){
243 return defined($arg->{into}) ? $arg->{into}
7ad9df77 244 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
245 : scalar caller(1);
db9fc237 246 }
247 else{
7ad9df77 248 return scalar caller(1);
db9fc237 249 }
250}
251
99934a1b 252#sub _spec{ %SPEC }
253
db9fc237 2541;
db9fc237 255__END__
256
257=head1 NAME
258
8cbcbb47 259Mouse::Exporter - make an import() and unimport() just like Mouse.pm
db9fc237 260
a25ca8d6 261=head1 VERSION
262
4bc73e47 263This document describes Mouse version 0.50_03
a25ca8d6 264
db9fc237 265=head1 SYNOPSIS
266
7ad9df77 267 package MyApp::Mouse;
268
269 use Mouse ();
270 use Mouse::Exporter;
271
272 Mouse::Exporter->setup_import_methods(
273 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
274 also => 'Mouse',
275 );
276
8cbcbb47 277 sub has_rw {
7ad9df77 278 my $meta = caller->meta;
279 my ( $name, %options ) = @_;
280 $meta->add_attribute(
281 $name,
282 is => 'rw',
283 %options,
284 );
285 }
286
287 # then later ...
288 package MyApp::User;
289
290 use MyApp::Mouse;
291
292 has 'name';
293 has_rw 'size';
294 thing;
295
8cbcbb47 296 no MyApp::Mouse;
db9fc237 297
298=head1 DESCRIPTION
299
7ad9df77 300This module encapsulates the exporting of sugar functions in a
301C<Mouse.pm>-like manner. It does this by building custom C<import>,
302C<unimport> methods for your module, based on a spec you provide.
8cbcbb47 303
304Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
305but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
db9fc237 306
e6dc493d 307=head1 METHODS
308
309=head2 C<< setup_import_methods( ARGS ) >>
310
311=head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>
312
db9fc237 313=head1 SEE ALSO
314
315L<Moose::Exporter>
316
5ea1528f 317=cut
318