Tweaks and documenting Mouse::Exporter
[gitmo/Mouse.git] / lib / Mouse / Exporter.pm
1 package Mouse::Exporter;
2 use strict;
3 use warnings;
4
5 use Carp qw(confess);
6
7 my %SPEC;
8
9 my $strict_bits = strict::bits(qw(subs refs vars));
10
11 # it must be "require", because Mouse::Util depends on Mouse::Exporter,
12 # which depends on Mouse::Util::import()
13 require Mouse::Util;
14
15 sub import{
16     $^H              |= $strict_bits;         # strict->import;
17     ${^WARNING_BITS}  = $warnings::Bits{all}; # warnings->import;
18     return;
19 }
20
21
22 sub setup_import_methods{
23     my($class, %args) = @_;
24
25     my $exporting_package = $args{exporting_package} ||= caller();
26
27     $SPEC{$exporting_package} = \%args;
28
29     # canonicalize args
30     my @export_from;
31     if($args{also}){
32         my %seen;
33         my @stack = ($exporting_package);
34
35         while(my $current = shift @stack){
36             push @export_from, $current;
37
38             my $also = $SPEC{$current}{also} or next;
39             push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
40         }
41     }
42     else{
43         @export_from = ($exporting_package);
44     }
45
46     {
47         my %exports;
48         my @removables;
49         my @all;
50
51         my @init_meta_methods;
52
53         foreach my $package(@export_from){
54             my $spec = $SPEC{$package} or next;
55
56             if(my $as_is = $spec->{as_is}){
57                 foreach my $thingy (@{$as_is}){
58                     my($code_package, $code_name, $code);
59
60                     if(ref($thingy)){
61                         $code = $thingy;
62                         ($code_package, $code_name) = Mouse::Util::get_code_info($code);
63                     }
64                     else{
65                         no strict 'refs';
66                         $code_package = $package;
67                         $code_name    = $thingy;
68                         $code         = \&{ $code_package . '::' . $code_name };
69                    }
70
71                     push @all, $code_name;
72                     $exports{$code_name} = $code;
73                     if($code_package eq $package){
74                         push @removables, $code_name;
75                     }
76                 }
77             }
78
79             if(my $init_meta = $package->can('init_meta')){
80                 if(!grep{ $_ == $init_meta } @init_meta_methods){
81                     unshift @init_meta_methods, $init_meta;
82                 }
83             }
84         }
85         $args{EXPORTS}    = \%exports;
86         $args{REMOVABLES} = \@removables;
87
88         $args{groups}{all}     ||= \@all;
89
90         if(my $default_list = $args{groups}{default}){
91             my %default;
92             foreach my $keyword(@{$default_list}){
93                 $default{$keyword} = $exports{$keyword}
94                     || confess(qq{The $exporting_package package does not export "$keyword"});
95             }
96             $args{DEFAULT} = \%default;
97         }
98         else{
99             $args{groups}{default} ||= \@all;
100             $args{DEFAULT}           = $args{EXPORTS};
101         }
102
103         if(@init_meta_methods){
104             $args{INIT_META} = \@init_meta_methods;
105         }
106     }
107
108     no strict 'refs';
109
110     *{$exporting_package . '::import'}    = \&do_import;
111     *{$exporting_package . '::unimport'}  = \&do_unimport;
112
113     return;
114 }
115
116
117 # the entity of general import()
118 sub do_import {
119     my($package, @args) = @_;
120
121     my $spec = $SPEC{$package}
122         || confess("The package $package package does not use Mouse::Exporter");
123
124     my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
125
126     my @exports;
127
128     foreach my $arg(@args){
129         if($arg =~ s/^-//){
130             Mouse::Util::not_supported("-$arg");
131         }
132         elsif($arg =~ s/^://){
133             my $group = $spec->{groups}{$arg}
134                 || confess(qq{The $package package does not export the group "$arg"});
135             push @exports, @{$group};
136         }
137         else{
138             push @exports, $arg;
139         }
140     }
141
142     $^H              |= $strict_bits;         # strict->import;
143     ${^WARNING_BITS}  = $warnings::Bits{all}; # warnings->import;
144
145     if($into eq 'main' && !$spec->{_export_to_main}){
146         warn qq{$package does not export its sugar to the 'main' package.\n};
147         return;
148     }
149
150     if($spec->{INIT_META}){
151         foreach my $init_meta(@{$spec->{INIT_META}}){
152             $into->$init_meta(for_class => $into);
153         }
154
155         # _apply_meta_traits($into); # TODO
156     }
157
158     if(@exports){
159         foreach my $keyword(@exports){
160             no strict 'refs';
161             *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
162                 || confess(qq{The $package package does not export "$keyword"});
163         }
164     }
165     else{
166         my $default = $spec->{DEFAULT};
167         while(my($keyword, $code) = each %{$default}){
168             no strict 'refs';
169             *{$into.'::'.$keyword} = $code;
170         }
171     }
172     return;
173 }
174
175 # the entity of general unimport()
176 sub do_unimport {
177     my($package, $arg) = @_;
178
179     my $spec = $SPEC{$package}
180         || confess("The package $package does not use Mouse::Exporter");
181
182     my $from = _get_caller_package($arg);
183
184     my $stash = do{
185         no strict 'refs';
186         \%{$from . '::'}
187     };
188
189     for my $keyword (@{ $spec->{REMOVABLES} }) {
190         my $gv = \$stash->{$keyword};
191         if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
192             delete $stash->{$keyword};
193         }
194     }
195     return;
196 }
197
198 # 1 extra level because it's called by import so there's a layer\r
199 # of indirection\r
200 sub _LEVEL(){ 1 }
201
202 sub _get_caller_package {
203     my($arg) = @_;
204
205     if(ref $arg){
206         return defined($arg->{into})       ? $arg->{into}
207              : defined($arg->{into_level}) ? scalar caller(_LEVEL + $arg->{into_level})
208              :                               scalar caller(_LEVEL);
209     }
210     else{
211         return scalar caller(_LEVEL);
212     }
213 }
214
215 1;
216
217 __END__
218
219 =head1 NAME
220
221 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
222
223 =head1 SYNOPSIS
224
225     package MyApp::Mouse;\r
226 \r
227     use Mouse ();\r
228     use Mouse::Exporter;\r
229 \r
230     Mouse::Exporter->setup_import_methods(\r
231       as_is     => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],\r
232       also      => 'Mouse',\r
233     );\r
234 \r
235     sub has_rw {
236         my $meta = caller->meta;\r
237         my ( $name, %options ) = @_;\r
238         $meta->add_attribute(\r
239           $name,\r
240           is => 'rw',\r
241           %options,\r
242         );\r
243     }\r
244 \r
245     # then later ...\r
246     package MyApp::User;\r
247
248     use MyApp::Mouse;\r
249 \r
250     has 'name';\r
251     has_rw 'size';\r
252     thing;\r
253 \r
254     no MyApp::Mouse;
255
256 =head1 DESCRIPTION
257
258 This module encapsulates the exporting of sugar functions in a\r
259 C<Mouse.pm>-like manner. It does this by building custom C<import>,\r
260 C<unimport> methods for your module, based on a spec you provide.\r
261
262 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
263 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
264
265 =head1 SEE ALSO
266
267 L<Moose::Exporter>
268
269 =cut