MapComponentDependencies/Utils.pm

filter: Statementstmt 51.2% 42 / 82 Branchbran 60.0% 12 / 20 Conditioncond 42.8% 6 / 14 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 50.0% 21 / 42 Podpod 47.6% 10 / 21 totaltotal 50.0% 93 / 186 CC 18 SCAR 17.6
CC
18
Cov
52%
_expand_config
21.9
__ANON__
6.0
__ANON__
6.0
CRAP 5.8
↑ Summary MapComponentDependencies.pm »
1 package Catalyst::Plugin::MapComponentDependencies::Utils;
2  
3 4 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our @EXPORT_OK = (qw/FromModel FromView FromController
8 FromComponent FromCode ConfigLoaderSubstitutions
9 FromContext FromRequest FromResponse FromLog
10 FromApplication/);
11  
12 4 our %EXPORT_TAGS = (All => \@EXPORT_OK, ALL => \@EXPORT_OK);
13  
14 14 ▶sub model_ns { __PACKAGE__ .'::MODEL' }
Subroutine model_ns: called CC=1 SCAR=0.0
Pod undocumented
15 12 ▶sub view_ns { __PACKAGE__ .'::VIEW' }
Subroutine view_ns: called CC=1 SCAR=0.0
Pod undocumented
16 12 ▶sub controller_ns { __PACKAGE__ .'::CONTROLLER' }
Subroutine controller_ns: called CC=1 SCAR=0.0
Pod undocumented
17 12 ▶sub component_ns { __PACKAGE__ .'::COMPONENT' }
Subroutine component_ns: called CC=1 SCAR=0.0
Pod undocumented
18 15 ▶sub code_ns { __PACKAGE__ .'::CODE' }
Subroutine code_ns: called CC=1 SCAR=0.0
Pod undocumented
19  
20 61 ▶sub _is {
Subroutine _is: called CC=4 SCAR=13.9
21 61 my ($possible, $target_ns) = @_;
22 61 ▶ return (defined($possible) and
Branch: 1 branch
BranchTrueFalseTotal
defined $possible && builtin::blessed($possible) && $possible->isa($target_ns) ? : 5 56 61
Condition83%5/6
defined $possible and builtin::blessed($possible) and $possible->isa($target_ns)
!ll&&!rl&&r
40165
defined $possible and builtin::blessed($possible)
!ll&&!rl&&r
04021
MC/DC66%2/3
defined $possible and builtin::blessed($possible) and $possible->isa($target_ns)
defined $possible builtin::blessed($possible) $possible->isa($target_ns)
Truth table75%3/4
A and B and C
A= defined $possibleB= builtin::blessed($possible)C= $possible->isa($target_ns)
ABCresult
0XX0
10X0
1100
1111
23 blessed($possible) and
24 $possible->isa($target_ns)) ?
25 $$possible : 0;
26 }
27  
28 13 ▶sub is_model($) { return _is(shift, model_ns) }
Subroutine is_model: called CC=1 SCAR=0.0
Pod undocumented
29 12 ▶sub is_view($) { return _is(shift, view_ns) }
Subroutine is_view: called CC=1 SCAR=0.0
Pod undocumented
30 12 ▶sub is_controller($) { return _is(shift, controller_ns) }
Subroutine is_controller: called CC=1 SCAR=0.0
Pod undocumented
31 12 ▶sub is_component($) { return _is(shift, component_ns) }
Subroutine is_component: called CC=1 SCAR=0.0
Pod undocumented
32 12 ▶sub is_code($) { return _is(shift, code_ns) }
Subroutine is_code: called CC=1 SCAR=0.0
Pod undocumented
33  
34 1 ▶sub FromModel($) { my $v = shift; return bless \$v, model_ns }
Subroutine FromModel: called CC=1 SCAR=0.0
Pod documented
35 0 ▶sub FromView($) { my $v = shift; return bless \$v, view_ns }
Subroutine FromView: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub FromController($) { my $v = shift; return bless \$v, controller_ns }
Subroutine FromController: not called CC=1 SCAR=6.9
Pod documented
37 0 ▶sub FromComponent($) { my $v = shift; return bless \$v, component_ns }
Subroutine FromComponent: not called CC=1 SCAR=6.9
Pod undocumented
38 3 ▶sub FromCode(&) { my $v = shift; return bless \$v, code_ns }
Subroutine FromCode: called CC=1 SCAR=0.0
Pod documented
39  
40 1 ▶sub FromContext {
Subroutine FromContext: called CC=1 SCAR=0.0
Pod documented
41 2 ▶ return FromCode {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 2 my ($c, $name, $config) = @_;
43 2 ▶ return blessed $c ? $c : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($c) ? : 1 1 2
44 1 };
45 }
46  
47 0 ▶sub FromRequest {
Subroutine FromRequest: not called CC=1 SCAR=6.9
Pod documented
48 0 ▶ return FromCode {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my ($c, $name, $config) = @_;
50 0 ▶ return blessed $c ? $c->request : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($c) ? : 0 0 0
51 0 };
52 }
53  
54 0 ▶sub FromResponse {
Subroutine FromResponse: not called CC=1 SCAR=6.9
Pod documented
55 0 ▶ return FromCode {
Subroutine __ANON__: not called CC=2 SCAR=17.9
56 0 my ($c, $name, $config) = @_;
57 0 ▶ return blessed $c ? $c->response : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($c) ? : 0 0 0
58 0 };
59 }
60  
61 0 ▶sub FromLog {
Subroutine FromLog: not called CC=1 SCAR=6.9
Pod documented
62 0 ▶ return FromCode {
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 my ($c_or_app, $name, $config) = @_;
64 0 return $c->log; # log is application and context
65 0 };
66 }
67  
68 1 ▶sub FromApplication {
Subroutine FromApplication: called CC=1 SCAR=0.1
Pod documented
69 1 ▶ return FromCode {
Subroutine __ANON__: called CC=2 SCAR=7.2
70 1 my ($c_or_app, $name, $config) = @_;
71 1 ▶ return blessed $c_or_app ? ref($c_or_app): $c_or_app;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($c_or_app) ? : 0 1 1
72 1 };
73 }
74  
75 14 ▶sub _expand_config {
Subroutine _expand_config: called CC=11 SCAR=30.9
76 14 my ($app_or_ctx, $component_name, $config) = @_;
77  
78 14 my $mapped_config = +{}; # shallow clone... might need something better than all this later
79 14 foreach my $key (keys %$config) {
80 13 my $value = $config->{$key};
81 13 ▶ if(my $m = is_model $value) {
Branch: 5 branches
BranchTrueFalseTotal
if (my $m = &is_model($value)) { } 1 12 13
elsif (my $v = &is_view($value)) { } 0 12 12
elsif (my $c = &is_controller($value)) { } 0 12 12
elsif (my $c = &is_component($value)) { } 0 12 12
elsif (my $cb = &is_code($value)) { } 4 8 12
82 1 ▶ $mapped_config->{$key} = $app_or_ctx->model($m) || die "$m is not a Model";
Condition50%1/2
$app_or_ctx->model($m) || die("$m is not a Model")
l!l
10
MC/DC0%0/1
$app_or_ctx->model($m) || die("$m is not a Model")
$app_or_ctx->model($m)
Truth table50%1/2
A
A= $app_or_ctx->model($m)
Aresult
11
00
83 } elsif(my $v = is_view $value) {
84 0 ▶ $mapped_config->{$key} = $app_or_ctx->view($v) || die "$v is not a View";
Condition0%0/2
$app_or_ctx->view($v) || die("$v is not a View")
l!l
00
MC/DC0%0/1
$app_or_ctx->view($v) || die("$v is not a View")
$app_or_ctx->view($v)
Truth table0%0/2
A
A= $app_or_ctx->view($v)
Aresult
11
00
85 } elsif(my $c = is_controller $value) {
86 0 ▶ $mapped_config->{$key} = $app_or_ctx->controller($c) || die "$c is not a Controller";
Condition0%0/2
$app_or_ctx->controller($c) || die("$c is not a Controller")
l!l
00
MC/DC0%0/1
$app_or_ctx->controller($c) || die("$c is not a Controller")
$app_or_ctx->controller($c)
Truth table0%0/2
A
A= $app_or_ctx->controller($c)
Aresult
11
00
87 } elsif(my $c = is_component $value) {
88 0 ▶ $mapped_config->{$key} = $app_or_ctx->component($c) || die "$c is not a Component";
Condition0%0/2
$app_or_ctx->component($c) || die("$c is not a Component")
l!l
00
MC/DC0%0/1
$app_or_ctx->component($c) || die("$c is not a Component")
$app_or_ctx->component($c)
Truth table0%0/2
A
A= $app_or_ctx->component($c)
Aresult
11
00
89 } elsif(my $cb = is_code $value) {
90 4 $mapped_config->{$key} = $cb->($app_or_ctx, $component_name, $config);
91 }
92 }
93  
94 14 return my $merged = Catalyst::Utils::merge_hashes($config, $mapped_config);
95 }
96  
97 0 ▶sub ConfigLoaderSubstitutions {
Subroutine ConfigLoaderSubstitutions: not called CC=1 SCAR=6.9
Pod documented
98 return (
99 0 ▶ FromContext => sub { my $c = shift; FromContext },
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 ▶ FromRequest => sub { my $c = shift; FromRequest },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 ▶ FromResponse => sub { my $c = shift; FromResponse },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 ▶ FromLog => sub { my $c = shift; FromLog },
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 ▶ FromApplication => sub { my $c = shift; FromApplication },
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 ▶ FromModel => sub { my $c = shift; FromModel(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 ▶ FromView => sub { my $c = shift; FromView(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 ▶ FromController => sub { my $c = shift; FromController(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 ▶ FromComponent => sub { my $c = shift; FromComponent(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 ▶ FromCode => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 my $c = shift;
110 0 ▶ FromCode { eval shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 },
112 0 );
113 }
114  
115 4 1;
116  
117 =head1 NAME
118  
119 Catalyst::Plugin::MapComponentDependencies::Utils - Utilities to integrate dependencies
120  
121 =head1 SYNOPSIS
122  
123 package MyApp;
124  
125 use Moose;
126 use Catalyst 'MapComponentDependencies;
127 use Catalyst::Plugin::MapComponentDependencies::Utils ':All';
128  
129 MyApp->config(
130 'Model::Bar' => { key => 'value' },
131 'Model::Foo' => {
132 bar => FromModel 'Bar',
133 baz => FromCode {
134 my ($app_or_ctx, $component_name) = @_;
135 return ...;
136 },
137 another_param => 'value',
138 },
139 );
140  
141 MyApp->setup;
142  
143 =head1 DESCRIPTION
144  
145 Utility functions to streamline integration of dynamic dependencies into your
146 global L<Catalyst> configuration.
147  
148 L<Catalyst::Plugin::MapComponentDependencies> offers a simple way to specify
149 configuration values for you components to be the value of other components
150 and to do so in a way that respects if your component does ACCEPT_CONTEXT.
151 We do this by providing a new namespace key in your configuration. However
152 you may prefer a 'flatter' configuration. These utility methods allow you to
153 'tag' a value in your configuration. This leads to a more simple configuration
154 setup, but it has the downside in that you must either use a Perl configuration
155 (as in the SYNOPSIS example) or if you are using L<Catalyst::Plugin::ConfigLoader>
156 you can install additional configuration substitutions like so:
157  
158 use Catalyst::Plugin::MapComponentDependencies::Utils ':All';
159  
160 __PACKAGE__->config->{ 'Plugin::ConfigLoader' }
161 ->{ substitutions } = { ConfigLoaderSubstitutions };
162  
163 See L<Catalyst::Plugin::MapComponentDependencies> for other options to declare
164 your component dependencies if this approach does not appeal.
165  
166 =head1 EXPORTS
167  
168 This package exports the following functions
169  
170 =head2 FromModel
171  
172 Creates a dependency to the named model.
173  
174 =head2 FromView
175  
176 Creates a dependency to the named model.
177  
178 =head2 FromController
179  
180 Creates a dependency to the named controller.
181  
182 =head2 FromCode
183  
184 An anonymouse coderef that must return the expected dependency.
185  
186 =head2 FromContext
187  
188 The current context, or undefined if the model does not ACCEPT_CONTEXT.
189  
190 B<NOTE>: Its really easy to create a circular reference when using the
191 context as a dependency. I recommend making sure the object which is
192 consuming it stores a weak reference. For example:
193  
194 package MyApp::Object;
195  
196 use Moose;
197  
198 has ctx => (is=>'ro', required=>1, weak_ref=>1);
199  
200 # rest of code...
201  
202 =head2 FromRequest
203  
204 The current L<Catalyst::Request> instance, or undefined if the model does not
205 ACCEPT_CONTEXT.
206  
207 =head2 FromResponse
208  
209 The current L<Catalyst::Response> instance, or undefined if the model does not
210 ACCEPT_CONTEXT.
211  
212 =head2 FromLog
213  
214 The current Log object.
215  
216 =head2 FromApplication
217  
218 You application class.
219  
220 =head2 ConfigLoaderSubstitutions
221  
222 Returns a Hash suitable for use as additional substitutions in
223 L<Catalyst::Plugin::ConfigLoader>.
224  
225 =head1 SEE ALSO
226  
227 L<Catalyst>, L<Catalyst::Plugin::MapComponentDependencies>,
228 L<Catalyst::Plugin::ConfigLoader>.
229  
230 =head1 AUTHOR
231
232 John Napiorkowski L<email:jjnapiork@cpan.org>
233
234 =head1 COPYRIGHT & LICENSE
235
236 Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
237
238 This library is free software; you can redistribute it and/or modify it under
239 the same terms as Perl itself.
240  
241 =cut
↑ Summary MapComponentDependencies.pm »