MapComponentDependencies.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 90.0% 9 / 10 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 92.1% 59 / 64 CC 10 SCAR 12.6
CC
10
Cov
94%
__ANON__
6.0
__ANON__
3.0
map_dependency
2.0
CRAP 3.5
« MapComponentDependencies/Utils.pm ↑ Summary
1 package Catalyst::Plugin::MapComponentDependencies;
2  
3 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Catalyst::Plugin::MapComponentDependencies::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 requires 'config_for', 'component';
8  
9 4 our $VERSION = '0.009';
10  
11 # Allow a shorthand version of config, should make it a bit
12 # more tidy and I doubt we will ever need configuration for
13 # the plugin itself.
14  
15 17 ▶my $plugin_config = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
16 17 ▶ my $config_namespace = shift->config->{'Plugin::MapComponentDependencies'} ||= +{};
Condition100%2/2
+(shift())->config->{"Plugin::MapComponentDependencies"} ||= {}
l!l
152
MC/DC100%1/1
+(shift())->config->{"Plugin::MapComponentDependencies"} ||= {}
+(shift())->config->{"Plugin::MapComponentDependencies"}
Truth table100%2/2
A
A= +(shift())->config->{"Plugin::MapComponentDependencies"}
Aresult
11
00
17 return exists $config_namespace->{map_dependencies} ?
18 $config_namespace->{map_dependencies} :
19 17 ▶ $config_namespace;
Branch: 1 branch
BranchTrueFalseTotal
exists $config_namespace->{'map_dependencies'} ? : 3 14 17
20 4 };
21  
22 1 ▶sub map_dependency {
Subroutine map_dependency: called CC=2 SCAR=7.1
Pod undocumented
23 1 my ($self, $name, $args) = @_;
24 die "Component $name exists" if
25 1 ▶ $self->$plugin_config->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->$plugin_config->{$name} 0 1 1
26 1 $self->$plugin_config->{$name} = $args;
27 }
28
29 15 ▶sub map_dependencies {
Subroutine map_dependencies: called CC=2 SCAR=6.9
Pod documented
30 15 my $self = shift;
31 15 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 15 16
32 1 $self->map_dependency(shift, shift);
33 }
34  
35 15 return $self->$plugin_config;
36 }
37  
38 14 ▶around 'config_for', sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
39 14 my ($orig, $app_or_ctx, $component_name, @args) = @_;
40 14 my $config = $app_or_ctx->$orig($component_name, @args);
41 14 my $component_suffix = Catalyst::Utils::class2classsuffix($component_name);
42  
43 # Walk the values and expand as needed
44 14 $config = Catalyst::Plugin::MapComponentDependencies::Utils::_expand_config($app_or_ctx, $component_name, $config);
45  
46 14 ▶ if(my $dependencies = $app_or_ctx->map_dependencies->{$component_suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dependencies = $app_or_ctx->map_dependencies->{$component_suffix}) { } 3 11 14
47 # walk the value tree for $dependencies.
48 3 my $mapped_config = +{}; # shallow clone... might need something better than all this later
49 3 foreach my $key (keys %$dependencies) {
50 6 ▶ if((ref($dependencies->{$key}) ||'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $dependencies->{$key} || '') eq 'CODE') { } 3 3 6
Condition100%2/2
ref $dependencies->{$key} || ''
l!l
33
MC/DC100%1/1
ref $dependencies->{$key} || ''
ref $dependencies->{$key}
Truth table100%2/2
A
A= ref $dependencies->{$key}
Aresult
11
00
51 3 $mapped_config->{$key} = $dependencies->{$key}->($app_or_ctx, $component_name, $config);
52 } else {
53 3 ▶ $mapped_config->{$key} = $app_or_ctx->component($dependencies->{$key}) ||
Condition50%1/2
$app_or_ctx->component($dependencies->{$key}) || die("'${\$dependencies->{$key};}' is not a component...")
l!l
30
MC/DC0%0/1
$app_or_ctx->component($dependencies->{$key}) || die("'${\$dependencies->{$key};}' is not a component...")
$app_or_ctx->component($dependencies->{$key})
Truth table50%1/2
A
A= $app_or_ctx->component($dependencies->{$key})
Aresult
11
00
54 0 die "'${\$dependencies->{$key}}' is not a component...";
55 }
56 }
57  
58 3 return my $merged = Catalyst::Utils::merge_hashes($config, $mapped_config);
59 } else {
60 11 return $config;
61 }
62 4 };
63  
64 4 1;
65  
66 =head1 NAME
67  
68 Catalyst::Plugin::MapComponentDependencies - Allow components to depend on other components
69  
70 =head1 SYNOPSIS
71  
72 package MyApp;
73  
74 use Moose;
75 use Catalyst;
76  
77 with 'Catalyst::Plugin::MapComponentDependencies';
78  
79 MyApp->map_dependencies(
80 'Model::Foo' => {
81 bar => 'Model::Bar',
82 baz => sub {
83 my ($app_or_ctx, $component_name) = @_;
84 return ...;
85 },
86 },
87 );
88  
89 MyApp->config(
90 'Model::Foo' => { another_param => 'value' }
91 )
92  
93 MyApp->setup;
94  
95 During setup when 'Model::Foo' is created it will get all three key / value pairs
96 send to ->new.
97  
98 B<NOTE:> You need to compose this plugin via the L<Moose> 'with' subroutine if you
99 want to get the handy class methods 'map_dependencies' and 'map_dependency'. If you
100 prefer you may setup you dependencies via configuration:
101  
102 package MyApp;
103  
104 use Catalyst 'MapComponentDependencies';
105  
106 MyApp->config(
107 'Model::Foo' => { another_param => 'value' },
108 'Plugin::MapComponentDependencies' => {
109 map_dependencies => {
110 'Model::Foo' => {
111 bar => 'Model::Bar',
112 baz => sub {
113 my ($app_or_ctx, $component_name, $from_config_args) = @_;
114 return ...;
115 },
116 },
117 },
118 },
119 );
120  
121 MyApp->setup;
122  
123 Alternatively you may choose a 'shorthand' version of the configuration based
124 approach:
125  
126 package MyApp;
127  
128 use Catalyst 'MapComponentDependencies';
129  
130 MyApp->config(
131 'Model::Foo' => { another_param => 'value' },
132 'Plugin::MapComponentDependencies' => {
133 'Model::Foo' => {
134 bar => 'Model::Bar',
135 baz => sub {
136 my ($app_or_ctx, $component_name, $from_config_args) = @_;
137 return ...;
138 },
139 },
140 },
141 );
142  
143 MyApp->setup;
144  
145 You may prefer this if your dependencies will map differently based on environment
146 and configuration settings.
147  
148 Lastly you may use the helper utilities to create a single merged configuration
149 for your dependencies:
150  
151 package MyApp;
152  
153 use Moose;
154 use Catalyst 'MapComponentDependencies;
155 use Catalyst::Plugin::MapComponentDependencies::Utils ':All';
156  
157 MyApp->config(
158 'Model::Bar' => { key => 'value' },
159 'Model::Foo' => {
160 bar => FromModel 'Bar',
161 baz => FromCode {
162 my ($app_or_ctx, $component_name) = @_;
163 return ...;
164 },
165 another_param => 'value',
166 },
167 );
168  
169 MyApp->setup;
170  
171 See L<Catalyst::Plugin::MapComponentDependencies::Utils> for more.
172  
173 =head1 DESCRIPTION
174  
175 Sometimes you would like a L<Catalyst> component to depend on the value of an
176 existing component. Since components are resolved during application setup (or
177 at request time, in the cause of a component that does ACCEPT_CONTEXT) you cannot
178 specify this dependency mapping in the 'normal' L<Catalyst> configuration hash.
179  
180 This plugin, which requires a recent L<Catalyst> of version 5.90090+, allows you to
181 define components which depend on each other. You can also set the value of an
182 initial argument to the value of a coderef, for added dynamic flexibility.
183  
184 You may define dependencies in one of two ways. The first way is to use a key/value
185 pair to map a configuation key attribute to the value of an existing L<Catalyst>
186 model. When the depending model is called, we get the value of that model in the
187 same way as if we called '$c->model($name)'. You can by the way use any type of
188 L<Catalys> component as a value (models, views and even controllers).
189  
190 The second way is to use a coderef, which is expected to return a value suitable for
191 the depending model. This gives you a little more flexibility for crafting very
192 custom types of dependencies. If you use a coderef you will get three values,
193 the application (or context depending on if the depending model does ACCEPT_CONTEXT),
194 the component name and a reference to any static configuration for the model (from
195 the global configuration, for example).
196  
197 B<NOTE>: Currently we only map dependencies 'one level' into the configuration
198 hash. Which means the following works as expected:
199  
200 MyApp->map_dependencies(
201 'Model::Foo' => {
202 baz => sub {
203 my ($app_or_ctx, $component_name, $config) = @_;
204 return ...;
205 },
206 },
207 );
208  
209 But not this:
210  
211 MyApp->map_dependencies(
212 'Model::Foo' => {
213 baz => {
214 bar => sub {
215 my ($app_or_ctx, $component_name, $config) = @_;
216 return ...;
217 },
218 },
219 },
220 );
221  
222 I'm not sure if I consider this limitation a feature or not... If you have good
223 use cases let me know and I'll consider a fix.
224  
225 =head1 METHODS
226  
227 This plugin defines the following methods
228  
229 =head2 map_dependencies
230  
231 Example:
232  
233 MyApp->map_dependencies(
234 'Model::AnotherModel' => { aaa => 'Model::Foo' },
235 'Model::Foo' => {
236 bar => 'Model::Bar',
237 baz => sub {
238 my ($app_or_ctx, $component_name, $config) = @_;
239 return ...;
240 },
241 },
242 );
243  
244 Maps a list of components and dependencies.
245  
246 =head1 map_dependency
247  
248 Maps a single component to a hashref of dependencies.
249  
250 =head1 CONFIGURATION
251  
252 This plugin defines the configuration namespace 'Plugin::MapComponentDependencies'
253 and defines the following keys:
254  
255 =head2 map_dependencies
256  
257 A Hashref where the key is a target component and the value is a hashref of arguments
258 that will be sent to it during initializion.
259  
260 MyApp->config(
261 'Plugin::MapComponentDependencies' => {
262 map_dependencies => {
263 'Model::Foo' => {
264 bar => 'Model::Bar',
265 baz => sub {
266 my ($app_or_ctx, $component_name, $from_config_args) = @_;
267 return ...;
268 },
269 },
270 },
271 },
272 );
273  
274 B<NOTE:> for simplicity you can place your hashref of configuration directly under the
275 plugin namespace.
276  
277 MyApp->config(
278 'Plugin::MapComponentDependencies' => {
279 'Model::Foo' => {
280 bar => 'Model::Bar',
281 baz => sub {
282 my ($app_or_ctx, $component_name, $from_config_args) = @_;
283 return ...;
284 },
285 },
286 },
287 );
288  
289 =head1 SEE ALSO
290  
291 L<Catalyst>, L<Catalyst::Plugin::InjectionHelpers>.
292  
293 =head1 AUTHOR
294
295 John Napiorkowski L<email:jjnapiork@cpan.org>
296
297 =head1 COPYRIGHT & LICENSE
298
299 Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
300
301 This library is free software; you can redistribute it and/or modify it under
302 the same terms as Perl itself.
303  
304 =cut
« MapComponentDependencies/Utils.pm ↑ Summary