Imports/Context/Default.pm

filter: Statementstmt 87.2% 48 / 55 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 82.3% 14 / 17 Podpod n/a totaltotal 82.5% 66 / 80 CC 5 SCAR 8.3
CC
5
Cov
83%
__ANON__
6.0
__ANON__
6.0
__ANON__
3.0
CRAP 2.3
« Imports.pm ↑ Summary Imports/Context/Config.pm »
1 package CatalystX::Imports::Context::Default;
2  
3 =head1 NAME
4  
5 CatalystX::Imports::Context::Default - Default Context Library
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::Context>
15  
16 =cut
17  
18 4 ▶use base 'CatalystX::Imports::Context';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 SYNOPSIS
21  
22 package MyApp::Controller::Foo;
23 use base 'Catalyst::Controller';
24 use CatalystX::Imports Context => ':all';
25  
26 sub foo: Local {
27 stash( rs => model('Foo')->find(param('foo')) );
28 }
29  
30 1;
31  
32 =head1 DESCRIPTION
33  
34 This package represents the default library of
35 L<Context|CatalystX::Imports::Context> exports.
36  
37 =head2 Tags
38  
39 There are some tags you can use to import groups of functions:
40  
41 =over
42  
43 =item C<:all>
44  
45 Imports all registered exports in their real names. Aliases will not
46 be included in the export. You still have to specify them explicitly.
47  
48 =item C<:intro>
49  
50 All exports in the L</INTROSPECTION EXPORTS> section.
51  
52 =item C<:mvc>
53  
54 The C<L</model>>, C<L</view>> and C<L</controller>> exports.
55  
56 =item C<:req>
57  
58 Exports all functions in the L</REQUEST EXPORTS> section.
59  
60 =item C<:param>
61  
62 Contains the C<L</has_param>> and C<L</param>> exports.
63  
64 =item C<:log>
65  
66 Everything defined in the L</LOGGING AND DEBUGGING EXPORTS> section.
67  
68 =item C<:debug>
69  
70 The L</debug> and L</log_debug> exports.
71  
72 =back
73  
74 For more information on the import syntax, please consult
75 L<CatalystX::Imports::Context>.
76  
77 =cut
78  
79 # just for convenience, argument positions
80 4 my %Pos = (LIB => 0, SELF => 1, CTX => 2, A_ARGS => 3, ARGS => 4);
81  
82 =head1 INTROSPECTION EXPORTS
83  
84 =cut
85  
86 =head2 action
87  
88 The C<action> function is a shortcut to lookup action(chain) objects.
89 When called without arguments:
90  
91 my $current_action_object = action;
92  
93 it will return the current action as stored in C<$c-E<gt>action>. You
94 can also specify the action of which you'd like to have the object. It
95 accepts simple action names (not starting with a slash) to return an
96 action relative to the current controller:
97  
98 my $action = action('list');
99  
100 But it also allows you to pass an absolute action path:
101  
102 my $action = action('/foo/bar/edit');
103  
104 =cut
105  
106 __PACKAGE__->register_export(
107 name => 'action',
108 5 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
109 5 my ($self, $c, $name) = @_[ @Pos{qw( SELF CTX ARGS )} ];
110 5 ▶ return $c->action
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 4 5
111 unless $name;
112 4 ▶ return $c->dispatcher->get_action_by_path($name)
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[^/] 1 3 4
113 if $name =~ m{^/};
114 3 return $self->action_for($name);
115 },
116 4 tags => [qw( intro )],
117 );
118  
119 =head2 model
120  
121 =head2 view
122  
123 =head2 controller
124  
125 These three functions are shortcuts to the corresponding method on the
126 context object. Therefore, the expression
127  
128 my $person = model('DBIC::Person')->find(23);
129  
130 will usually do the same as
131  
132 my $person = $c->model('DBIC::Person')->find(23);
133  
134 Note, however, that these three exports are aware of the
135 L<component_prefix|CatalystX::Imports/component_prefix> configuration
136 setting.
137  
138 =cut
139  
140 4 for my $type (qw( model view controller )) {
141 __PACKAGE__->register_export(
142 name => $type,
143 3 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 my ($library, $self, $c, $a_args, $name, @args)
145 3 = @_[ @Pos{qw( LIB SELF CTX A_ARGS )}, $Pos{ARGS} .. $#_ ];
146 # my ($library, $self, $c, $a_args, $name) = @_;
147 3 return $library->resolve_component($self, $c, $type, $name, \@args);
148 },
149 12 tags => [qw( intro mvc )],
150 );
151 }
152  
153 =head2 uri_for
154  
155 See also L<Catalyst/$c-E<gt>uri_for>. Here is an example in combination
156 with L</action>:
157  
158 my $edit_uri = uri_for(action('edit'), 23);
159  
160 =cut
161  
162 __PACKAGE__->register_export(
163 name => 'uri_for',
164 1 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 1 my ($c, @args) = @_[ $Pos{CTX}, $Pos{ARGS} .. $#_ ];
166 1 return $c->uri_for(@args);
167 },
168 4 tags => [qw( intro )],
169 );
170  
171 =head2 path_to
172  
173 See also L<Catalyst/$c-E<gt>path_to>. This utility function builds a
174 path by your specification, starting at the application root:
175  
176 my $pdf_dir = path_to(qw( root data pdfs ));
177  
178 =cut
179  
180 __PACKAGE__->register_export(
181 name => 'path_to',
182 1 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 1 my ($c, @args) = @_[ $Pos{CTX}, $Pos{ARGS} .. $#_ ];
184 1 return $c->path_to(@args);
185 },
186 4 tags => [qw( intro )],
187 );
188  
189 =head1 REQUEST EXPORTS
190  
191 =cut
192  
193 =head2 stash
194  
195 See L<Catalyst/$c-E<gt>stash>, for which this function is a shortcut:
196  
197 stash(rs => $result_set); # stores the key 'rs' in the stash
198 ...
199 my $rs = stash->{rs}; # retrieves it again
200  
201 =cut
202  
203 __PACKAGE__->register_export(
204 name => 'stash',
205 2 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 2 my ($c, @args) = @_[ $Pos{CTX}, $Pos{ARGS} .. $#_ ];
207 2 return $c->stash(@args);
208 },
209 4 tags => [qw( req )],
210 );
211  
212 =head2 arguments
213  
214 Returns the last called action's passed arguments.
215  
216 =cut
217  
218 __PACKAGE__->register_export(
219 name => 'arguments',
220 5 ▶ code => sub { return $_[ $Pos{A_ARGS} ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
221 4 tags => [qw( req )],
222 );
223  
224 =head2 request
225  
226 Returns the current L<Catalyst::Request> object. You can also import
227 its L<alias|CatalystX::Imports::Context/ALIASES> C<req>.
228  
229 if (request->method eq 'POST') {
230 ...
231 }
232  
233 =cut
234  
235 __PACKAGE__->register_export(
236 name => 'request',
237 1 ▶ code => sub { $_[ $Pos{CTX} ]->request },
Subroutine __ANON__: called CC=1 SCAR=0.0
238 4 alias => [qw( req )],
239 tags => [qw( req )],
240 );
241  
242 =head2 response
243  
244 Returns the current L<Catalyst::Response> object. You can also import
245 its L<alias|CatalystX::Imports::Context/ALIASES> C<res>.
246  
247 response->status(404);
248 response->body('I misplaced that resource.');
249  
250 =cut
251  
252 __PACKAGE__->register_export(
253 name => 'response',
254 10 ▶ code => sub { $_[ $Pos{CTX} ]->response },
Subroutine __ANON__: called CC=1 SCAR=0.0
255 4 alias => [qw( res )],
256 tags => [qw( req )],
257 );
258  
259 =head2 captures
260  
261 Returns the current requests captures.
262  
263 =cut
264  
265 __PACKAGE__->register_export(
266 name => 'captures',
267 2 ▶ code => sub { @{ $_[ $Pos{CTX} ]->request->captures } },
Subroutine __ANON__: called CC=1 SCAR=0.0
268 4 alias => [qw( cap )],
269 tags => [qw( req )],
270 );
271  
272 =head2 has_param
273  
274 Boolean test if a query parameter was submitted with the request.
275  
276 sub search: Local {
277 if (has_param('q')) {
278 my $q = param('q');
279 stash( result => model('Foo')->search({ bar => $q }) );
280 }
281 }
282  
283 =cut
284  
285 __PACKAGE__->register_export(
286 name => 'has_param',
287 2 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
288 2 my ($c, $name) = @_[ @Pos{qw( CTX ARGS )} ];
289 2 return exists $c->request->params->{ $name };
290 },
291 4 tags => [qw( req param )],
292 );
293  
294 =head2 param
295  
296 The same as a call to the C<param> method on the current
297 L<Catalyst::Request> object.
298  
299 =cut
300  
301 __PACKAGE__->register_export(
302 name => 'param',
303 2 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
304 2 my ($c, $name) = @_[ @Pos{qw( CTX ARGS )} ];
305 2 return $c->request->param( $name );
306 },
307 4 tags => [qw( req param )],
308 );
309  
310 =head1 LOGGING AND DEBUGGING EXPORTS
311  
312 =cut
313  
314 =head2 debug
315  
316 This function allows you to nest debugging code directly in your actions
317 and only execute it when debugging is turned on.
318  
319 debug { # runs only in debug mode
320 my $foo = 'something';
321 my @bar = (1 .. 10_000);
322 warn "Doing $foo on $_" for @bar;
323 log_debug('Done.');
324 };
325  
326 =cut
327  
328 __PACKAGE__->register_export(
329 name => 'debug',
330 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
331 0 my ($c, $code) = @_[ @Pos{qw( CTX ARGS )} ];
332 0 ▶ return $code->() if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
333 0 return;
334 },
335 4 tags => [qw( log debug )],
336 prototype => '&',
337 );
338  
339 =head2 log_debug
340  
341 Outputs content to the logging channel, B<but only> if the application is
342 in debug mode.
343  
344 =cut
345  
346 __PACKAGE__->register_export(
347 name => 'log_debug',
348 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
349 0 my ($c, @args) = @_[ $Pos{CTX}, $Pos{ARGS} .. $#_ ];
350 0 ▶ return $c->log->debug(@args) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
351 },
352 4 tags => [qw( log debug )],
353 );
354  
355 =head2 log_info
356  
357 =head2 log_warn
358  
359 =head2 log_error
360  
361 These functions log to the C<info>, C<warn> and C<error> channels.
362  
363 =cut
364  
365 4 for my $type (qw( info warn error )) {
366 __PACKAGE__->register_export(
367 name => "log_$type",
368 0 ▶ code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
369 0 my ($c, @args) = @_[ $Pos{CTX}, $Pos{ARGS} .. $#_ ];
370 0 return $c->log->$type(@args);
371 },
372 12 tags => [qw( log )],
373 );
374 }
375  
376 =head1 SEE ALSO
377  
378 L<Catalyst>,
379 L<CatalystX::Imports::Context>,
380 L<CatalystX::Imports>
381  
382 =head1 AUTHOR AND COPYRIGHT
383  
384 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>
385  
386 =head1 LICENSE
387  
388 This program is free software; you can redistribute it and/or modify
389 it under the same terms as perl itself.
390  
391 =cut
392  
393 4 1;
« Imports.pm ↑ Summary Imports/Context/Config.pm »