Imports/Context.pm

filter: Statementstmt 95.6% 132 / 138 Branchbran 70.0% 21 / 30 Conditioncond 54.5% 18 / 33 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 92.3% 24 / 26 Podpod 100% 6 / 6 totaltotal 82.4% 206 / 250 CC 38 SCAR 22.1
CC
38
Cov
85%
export_into
19.4
register_export
12.3
context_export_into
11.2
CRAP 9.1
↑ Summary Imports/Vars.pm »
1 package CatalystX::Imports::Context;
2  
3 =head1 NAME
4  
5 CatalystX::Imports::Context - Exports Context Helper Functions
6  
7 =cut
8  
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 BASE CLASSES
13  
14 L<CatalystX::Imports>
15  
16 =cut
17  
18 4 ▶use base 'CatalystX::Imports';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use vars qw( $EXPORT_MAP_NAME $DEFAULT_LIBRARY );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use List::MoreUtils qw( part apply uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Scalar::Util qw( set_prototype );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Carp::Clan qw{ ^CatalystX::Imports(?:::|$) };
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Filter::EOF;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 $EXPORT_MAP_NAME = 'CATALYSTX_IMPORTS_EXPORT_MAP';
29 4 $DEFAULT_LIBRARY = __PACKAGE__ . '::Default';
30  
31 =head1 SYNOPSIS
32  
33 package MyApp::Controller::Foo;
34 use base 'Catalyst::Controller';
35  
36 # Export everything minus the 'captures' function. Also load
37 # the additional 'Foo' library and a config value
38 use CatalystX::Imports
39 Context => {
40 Default => [qw( :all -captures +Foo )],
41 Config => [qw( model_name )],
42 };
43  
44 sub list: Local {
45 stash( rs => model(model_name)->search_rs );
46 }
47  
48 sub edit: Local {
49 stash(
50 foo => model(model_name)->find(args[0]),
51 list_uri => uri_for(action('list')),
52 );
53 }
54  
55 1;
56  
57 =head1 DESCRIPTION
58  
59 This package represents the base class and export manager for all
60 libraries. The default library can be found under the package name
61 L<CatalystX::Imports::Context::Default>.
62  
63 The exports will be removed after compiletime. By then, the calls
64 to them in your controller will already be bound to the right code
65 slots by perl. This keeps these functions from being available as
66 methods on your controller object.
67  
68 =head1 IMPORT SYNTAX
69  
70 You can specify what library parts you want to import into your
71 controller on the C<use> line to L<CatalystX::Imports>:
72  
73 use CatalystX::Imports Context => [qw(:all -captures +Foo)];
74  
75 This would import all functions from the default library
76 L<CatalystX::Imports::Context::Default>, except the C<captures> function.
77 See L<CatalystX::Imports::Context::Default/Tags> for all available tags
78 in the default library.
79  
80 Additionally, it will search and load the C<Foo> library, which would be
81 C<CatalystX::Imports::Context::Foo>. This notation doesn't accept any
82 arguments, so the library specific default symbols will be exported.
83  
84 If you just want some specific functions imported, you can also specify
85 them explicitly:
86  
87 use CatalystX::Imports
88 Context => [qw(action uri_for model config stash)];
89  
90 At last, to be specific about more than one library, you can pass a
91 hash reference:
92  
93 use CatalystX::Imports
94 Context => { Default => ':all', Config => [qw(model_name)] };
95  
96 See the libraries documentation for further syntax information.
97  
98 =head1 ALIASES
99  
100 If documented, you can also import a function with one of it's aliases.
101 If you import a function via a tag, it will only be exported under its
102 real name, not its aliased names. Therefor, to use an aliase you have
103 to specify aliases explicitly at any time to use them:
104  
105 # load aliases for short forms of 'request' and 'response'
106 use CatalystX::Imports Context => [qw( req res )];
107  
108 =head1 INCLUDED LIBRARIES
109  
110 =over
111  
112 =item L<CatalystX::Imports::Context::Default>
113  
114 Contains default shortcuts and inline accessors.
115  
116 =item L<CatalystX::Imports::Context::Config>
117  
118 Allows you to import local controller (instance) configuration accessors
119 as inline functions into your namespace.
120  
121 =back
122  
123 =cut
124  
125 =head1 METHODS
126  
127 =cut
128  
129 =head2 register_export
130  
131 This method registers a new export in the library it's called upon. You
132 will mostly only need this function for creating your own libraries:
133  
134 package CatalystX::Imports::Context::MyOwnLibrary;
135 use base 'CatalystX::Imports::Context';
136  
137 __PACKAGE__->register_export(
138 name => 'double',
139 alias => 'times_two',
140 prototype => '$',
141 tags => [qw( math )],
142 code => sub {
143 my ($library, $ctrl, $ctx, $action_args, @args) = @_;
144 return $args[0] * 2;
145 },
146 );
147  
148 The C<code> and C<name> parameters are mandatory. If you specify an
149 alias, it can be imported explicitly, but will not be included in the
150 C<:all> tag.
151  
152 The prototype is the usual prototpe you could stuff on perl subroutines.
153 If you specify tags as an array reference, the export will be included
154 in those tag sets by it's name and aliases. It will be included in the
155 C<:all> tag in any case, but only under it's name, not it's aliases.
156  
157 The specified code reference will get the library class name, the
158 controller and context objects (like a L<Catalyst> action), and an array
159 reference of the arguments passed to the last action and then it's
160 own actual arguments passed in. You could call the above with
161  
162 double(23); # 46
163  
164 =cut
165  
166 73 ▶sub register_export {
Subroutine register_export: called CC=12 SCAR=25.1
Pod documented
167 73 my ($class, @args) = @_;
168  
169 # we expect pairs of option keys and values as arguments
170 73 ▶ croak 'register_export expects key/value pairs as arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 73 73
171 if @args % 2;
172 73 my %options = @args;
173  
174 # check if every required option is there
175 73 for my $required (qw( code name )) {
176 croak "register_export: Missing required parameter: '$required'"
177 unless exists $options{ $required }
178 146 ▶ and defined $options{ $required };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$required} and defined $options{$required} 0 146 146
Condition33%1/3
exists $options{$required} and defined $options{$required}
!ll&&!rl&&r
00146
MC/DC0%0/2
exists $options{$required} and defined $options{$required}
exists $options{$required} defined $options{$required}
Truth table33%1/3
A and B
A= exists $options{$required}B= defined $options{$required}
ABresult
0X0
100
111
179 }
180  
181 # optionals
182 73 ▶ my @tags = @{ $options{tags} || [] };
Condition100%2/2
$options{'tags'} || []
l!l
721
MC/DC100%1/1
$options{'tags'} || []
$options{'tags'}
Truth table100%2/2
A
A= $options{'tags'}
Aresult
11
00
183 73 ▶ my @aliases = @{ $options{alias} || [] };
Condition100%2/2
$options{'alias'} || []
l!l
1261
MC/DC100%1/1
$options{'alias'} || []
$options{'alias'}
Truth table100%2/2
A
A= $options{'alias'}
Aresult
11
00
184  
185 # get the export map, we'll need it
186 73 my $export_map = $class->_export_map;
187  
188 # walk the names we want to register this under
189 73 for my $name ($options{name}, @aliases) {
190  
191 # register in tags, only name goes into :all by default
192 85 ▶ for my $t (uniq @tags, ($options{name} eq $name ? 'all' : ())) {
Branch: 1 branch
BranchTrueFalseTotal
$options{'name'} eq $name ? : 73 12 85
193 185 ▶ push @{ $export_map->{tag}{ $t } ||= [] }, $name;
Condition100%2/2
$$export_map{"tag"}{$t} ||= []
l!l
15728
MC/DC100%1/1
$$export_map{"tag"}{$t} ||= []
$$export_map{"tag"}{$t}
Truth table100%2/2
A
A= $$export_map{"tag"}{$t}
Aresult
11
00
194 }
195  
196 # save export information
197 $export_map->{export}{ $name } = {
198 name => $name,
199 code => $options{code},
200 ( exists $options{prototype}
201 ? ( prototype => $options{prototype} )
202 85 ▶ : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'prototype'} ? : 5 80 85
203 };
204 }
205  
206 73 return 1;
207 }
208  
209 =head2 _export_map
210  
211 Returns the libraries export map as a hash reference. This will be stored
212 in your library class (if you build your own, otherwise you don't have to
213 care) in the C<%CATALYSTX_IMPORTS_EXPORT_MAP> package variable.
214  
215 =cut
216  
217 199 ▶sub _export_map {
Subroutine _export_map: called CC=1 SCAR=0.0
218 199 my ($class) = @_;
219 199 my $map_name = "${class}::${EXPORT_MAP_NAME}";
220 4 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 4 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
222 199 return \%{ $map_name };
223 }
224 }
225  
226 =head2 get_export
227  
228 Expects the name of an export in the library and will return the
229 information it stored with it. An export will be stored under its actual
230 name as well as its aliases.
231  
232 =cut
233  
234 113 ▶sub get_export { $_[0]->_export_map->{export}{ $_[1] } }
Subroutine get_export: called CC=1 SCAR=0.0
Pod documented
235  
236 =head2 export_into
237  
238 Called by L<CatalystX::Imports>' C<import> method. Takes a target and a
239 set of commands specified in L</IMPORT SYNTAX>. This will forward the
240 commands to the actual libraries and the L</context_export_into> method
241 in them.
242  
243 =cut
244  
245 9 ▶sub export_into {
Subroutine export_into: called CC=15 SCAR=29.6
Pod documented
246 9 my ($class, $target, @args) = @_;
247 9 my %args;
248  
249 # we accept lists and array refs for default, and explicit
250 # hash refs for more control
251 9 ▶ if (@args == 1 and ref $args[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'ARRAY') { } 3 6 9
elsif (@args == 1 and ref $args[0] eq 'HASH') { } 3 3 6
Condition66%2/3
@args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
063
MC/DC50%1/2
@args == 1 and ref $args[0] eq "ARRAY"
@args == 1 ref $args[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
252 3 %args = (Default => $args[0]);
253 }
254 ▶ elsif (@args == 1 and ref $args[0] eq 'HASH') {
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
033
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
255 3 %args = %{ $args[0] };
256 ref($args{ $_ }) eq 'ARRAY' or $args{ $_ } = [ $args{ $_ } ]
257 3 ▶ for keys %args;
Branch: 1 branch
BranchTrueFalseTotal
ref $args{$_} eq "ARRAY" or $args{$_} = [$args{$_}] 3 3 6
258 }
259 else {
260 3 %args = (Default => \@args);
261 }
262  
263 # filter out additional libraries in Default arguments
264 9 ▶ my @default_args = @{ delete($args{Default}) || [] };
Condition50%1/2
delete $args{'Default'} || []
l!l
90
MC/DC0%0/1
delete $args{'Default'} || []
delete $args{'Default'}
Truth table50%1/2
A
A= delete $args{'Default'}
Aresult
11
00
265 9 my %load_default;
266 9 for my $arg (@default_args) {
267 9 ▶ if ($arg =~ /^[+](.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^[+](.+)$/) 0 9 9
268 0 ▶ next unless exists $args{ $1 };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$1} 0 0 0
269 0 ▶ $args{ $1 } ||= [];
Condition0%0/2
$args{$1} ||= []
l!l
00
MC/DC0%0/1
$args{$1} ||= []
$args{$1}
Truth table0%0/2
A
A= $args{$1}
Aresult
11
00
270 0 $load_default{ $1 } = 1;
271 0 next;
272 }
273 9 ▶ push @{ $args{Default} ||= [] }, $arg;
Condition50%1/2
$args{"Default"} ||= []
l!l
09
MC/DC0%0/1
$args{"Default"} ||= []
$args{"Default"}
Truth table50%1/2
A
A= $args{"Default"}
Aresult
11
00
274 }
275  
276 # load libraries and export symbols
277 9 for my $lib (keys %args) {
278 12 my $lib_class = __PACKAGE__ . '::' . $lib;
279 12 Class::MOP::load_class($lib_class);
280 12 my @symbols = @{ $args{ $lib } };
281 push @symbols, $lib_class->default_exports
282 12 ▶ if $load_default{ $lib };
Branch: 1 branch
BranchTrueFalseTotal
if $load_default{$lib} 0 12 12
283 12 $lib_class->context_export_into($target, @{ $args{ $lib } });
284 }
285  
286 9 return 1;
287 }
288  
289 =head2 context_export_into
290  
291 Takes a target and an actual command set for a library (no C<+Foo> stuff)
292 and cleans that (flattens out tags, removes C<-substractions>). It will
293 utilise L</context_install_export_into> to actually export the final set
294 of functions.
295  
296 =cut
297  
298 12 ▶sub context_export_into {
Subroutine context_export_into: called CC=10 SCAR=24.1
Pod documented
299 12 my ($class, $target, @exports) = @_;
300  
301 # part and clean different type of import arguments
302 12 my ($export_list, $tags, $substract) = map { [] } 1..3;
303 12 for my $export (@exports) {
304 18 ▶ push @$substract, $export and next
Branch: 1 branch
BranchTrueFalseTotal
if $export =~ s/^-// 0 18 18
Condition20%1/5
$export =~ s/^-// && (push(@$substract, $export) && (next))
!ll&&!rl&&r
1800
push @$substract, $export and next
!ll
00
MC/DC0%0/2
$export =~ s/^-// && (push(@$substract, $export) && (next))
$export =~ s/^-// push @$substract, $export
Truth table33%1/3
A && B
A= $export =~ s/^-//B= push @$substract, $export
ABresult
0X0
100
111
305 if $export =~ s/^-//;
306 18 ▶ push @$tags, $export and next
Branch: 1 branch
BranchTrueFalseTotal
if $export =~ s/^:// 6 12 18
Condition60%3/5
$export =~ s/^:// && (push(@$tags, $export) && (next))
!ll&&!rl&&r
1260
push @$tags, $export and next
!ll
06
MC/DC0%0/2
$export =~ s/^:// && (push(@$tags, $export) && (next))
$export =~ s/^:// push @$tags, $export
Truth table33%1/3
A && B
A= $export =~ s/^://B= push @$tags, $export
ABresult
0X0
100
111
307 if $export =~ s/^://;
308 12 push @$export_list, $export;
309 }
310  
311 # fetch the export map, we're going to use it a bit
312 12 my $export_map = $class->_export_map;
313  
314 # resolve tags
315 12 for my $tag (@$tags) {
316 6 ▶ my $tag_exports = $export_map->{tag}{ $tag }
Branch: 1 branch
BranchTrueFalseTotal
my $tag_exports = $$export_map{"tag"}{$tag} or croak("Unknown Context tag: ':${tag}'") 6 0 6
317 or croak "Unknown Context tag: ':$tag'";
318 6 push @$export_list, @$tag_exports;
319 }
320  
321 # remove doubles and substractions
322 0 ▶ my %substract_map = map { ($_ => 1) } @{ $substract || [] };
Condition50%1/2
$substract || []
l!l
120
MC/DC0%0/1
$substract || []
$substract
Truth table50%1/2
A
A= $substract
Aresult
11
00
323 @$export_list
324 12 = grep { not exists $substract_map{ $_ } }
325 uniq @$export_list;
326  
327 # install the exports
328 12 for my $export (@$export_list) {
329 122 $class->context_install_export_into($target, $export);
330 }
331  
332 # register the exports to be removed after compile time
333 12 ▶ Filter::EOF->on_eof_call(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
334 12 for my $export (@$export_list) {
335 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
336 122 delete ${ $target . '::' }{ $export };
337 }
338 12 });
339  
340 12 return 1;
341 }
342  
343 =head2 context_install_export_into
344  
345 Takes a target class and the name of an export to install the function
346 in the specified class.
347  
348 =cut
349  
350 122 ▶sub context_install_export_into {
Subroutine context_install_export_into: called CC=3 SCAR=11.0
Pod documented
351 122 my ($class, $target, $export) = @_;
352  
353 # find the export information
354 122 ▶ my $export_info = $class->get_export($export)
Branch: 1 branch
BranchTrueFalseTotal
my $export_info = $class->get_export($export) or croak("Unknown Context export: '${export}'") 122 0 122
355 or croak "Unknown Context export: '$export'";
356 122 my ($code, $prototype) = @{ $export_info }{qw( code prototype )};
357  
358 # the wrapper fetches the current objects
359 37 ▶ my $export_code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
360 37 my ($controller, $context, $arguments) = do {
361 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
362 37 map { ${ "${target}::" . ${ "CatalystX::Imports::$_" } } }
363 qw( STORE_CONTROLLER STORE_CONTEXT STORE_ARGUMENTS );
364 };
365 37 return $class->$code($controller, $context, $arguments, @_);
366 122 };
367  
368 # install the export, include prototype if specified
369 4 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
370 122 my $name = join('::',$target, $export_info->{name});
371 *$name = subname $name, (
372 defined $prototype
373 0 ▶ ? set_prototype sub { $export_code->(@_) }, $prototype
Subroutine __ANON__: not called CC=1 SCAR=0.1
Subroutine config_test_alias1: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
defined $prototype ? : 17 105 122
374 : $export_code
375 );
376 }
377  
378 122 return 1;
379 }
380  
381 =head2 default_exports
382  
383 Should be overridden by subclasses if they want to export something
384 by default. This will be used if the library is specified without any
385 arguments at all. E.g. this:
386  
387 use CatalystX::Imports Context => [qw( +Foo )];
388  
389 will export C<Foo>'s defaults, but
390  
391 use CatalystX::Imports Context => { Foo => [] };
392  
393 will not. Without an overriding method, the default is set to export
394 nothing at all.
395  
396 =cut
397  
398 0 ▶sub default_exports { }
Subroutine default_exports: not called CC=1 SCAR=0.0
Pod documented
399  
400 =head1 DIAGNOSTICS
401  
402 =head2 register_export expects key/value pairs as arguments
403  
404 You passed an odd number of values into the C<register_export> method
405 call, but it expects key and value pairs of named options. See
406 L>/register_export> for available options and calling syntax.
407  
408 =head2 register_export: Missing required parameter: 'foo'
409  
410 The L</register_export> method expects a few parameters that can't be
411 omitted, including C<foo>. Pass in the parameter as specified in the
412 section about the L</register_export> method.
413  
414 =head2 Unknown Context tag: ':foo'
415  
416 You specified to import the functions in the tag C<:foo> on your C<use>
417 line, but no tag with the name C<:foo> was registered in the library.
418  
419 =head2 Unknown Context export: 'foo'
420  
421 You asked for export of the function C<foo>, but no function under this
422 name was registered in the library. Please consult your library
423 documentation for a list of available exports. The default library can
424 be found under L<CatalystX::Imports::Context::Default>.
425  
426 =head1 SEE ALSO
427  
428 L<Catalyst>,
429 L<Filter::EOF>,
430 L<CatalystX::Imports::Context::Default>,
431 L<CatalystX::Imports::Context::Config>,
432 L<CatalystX::Imports::Vars>,
433 L<CatalystX::Imports>
434  
435 =head1 AUTHOR AND COPYRIGHT
436  
437 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>
438  
439 =head1 LICENSE
440  
441 This program is free software; you can redistribute it and/or modify
442 it under the same terms as perl itself.
443  
444 =cut
445  
446 4 1;
↑ Summary Imports/Vars.pm »