File Coverage

blib/lib/Catalyst/Component/WithDelayedSetup.pm
Criterion Covered Total %
statement 4 4 100.0
branch n/a
condition 2 3 66.6
subroutine 2 2 100.0
pod 0 1 0.0
total 8 10 80.0


line stmt bran cond sub pod time code
1             package Catalyst::Component::WithDelayedSetup;
2              
3 1     1   10865 use Moose::Role;
  1         2  
  1         6  
4             our $VERSION = '0.001';
5              
6             around 'COMPONENT', sub {
7             my ($orig, $class, @args) = @_;
8             return bless sub { $class->$orig(@args) }, $class;
9             };
10              
11             my $ONCE;
12              
13 5   66 5 0 81936 sub ACCEPT_CONTEXT { $ONCE ||= shift->() }
14              
15             1;
16              
17             =head1 NAME
18              
19             Catalyst::Component::WithDelayedSetup - Moose Role for components which setup late
20              
21             =head1 SYNOPSIS
22              
23             package MyApp::Model::Foo;
24              
25             use Moose;
26             extends 'Catalyst::Model';
27             with 'Catalyst::Component::DelayedSetup';
28              
29             # Proceed as normal
30              
31             =head1 DESCRIPTION
32              
33             Sometimes you want an application scoped component that nevertheless needs other
34             application components as part of its setup. In the past this was not reliable
35             since Application scoped components are setup in linear order. You could not
36             call $app->model('A') in a COMPONENT method and expect $app->model('B') to be there
37             This role defers creating the application scoped instance until after your application is
38             fully setup. This means you can now assume your other application scoped components
39             (components that do COMPONENT but not ACCEPT_CONTEXT) are available as dependencies.
40              
41             Please note this means that your instance is not created until the first time its
42             called in a request. As a result any errors with configuration will not show up
43             until later in runtime. So there is a larger burden on your testing to make sure
44             your application startup and runtime is accurate. Also note that even though your
45             instance creation is deferred to request time, the request context is NOT given,
46             but the application is (this means that you cannot depend on components that do
47             ACCEPT_CONTEXT, since you don't have one...).
48              
49             Please note it makes no sense to use this component role and then do the ACCEPT_CONTEXT
50             method...
51              
52             =head1 SEE ALSO
53              
54             L<Catalyst::Component>, L<Catalyst>.
55              
56             =head1 AUTHOR
57            
58             John Napiorkowski L<email:jjnapiork@cpan.org>
59            
60             =head1 COPYRIGHT & LICENSE
61            
62             Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
63            
64             This library is free software; you can redistribute it and/or modify it under
65             the same terms as Perl itself.
66              
67             =cut