Plugin/CurrentComponents.pm

filter: Statementstmt 70.5% 60 / 85 Branchbran 48.2% 27 / 56 Conditioncond 60.6% 20 / 33 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 71.4% 10 / 14 Podpod n/a totaltotal 61.0% 127 / 208 CC 40 SCAR 22.1
CC
40
Cov
61%
__ANON__
22.5
_build_model_instance_from_state
12.0
_build_view_instance_from_return
12.0
CRAP 9.2
↑ Summary ActionRole/CurrentModel.pm »
1 package Catalyst::Plugin::CurrentComponents;
2  
3 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 requires 'model', 'view', 'stash';
7  
8 1 our $VERSION = '0.010';
9  
10 1 has 'model_instance_from_return' => (is=>'lazy');
11  
12 10 ▶ sub _build_model_instance_from_return {
Subroutine _build_model_instance_from_return: called CC=3 SCAR=13.1
13 10 ▶ if(my $config = shift->config->{'Plugin::CurrentComponents'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = +(shift())->config->{'Plugin::CurrentComponents'}) { } 10 0 10
14 10 ▶ return exists $config->{model_instance_from_return} ? $config->{model_instance_from_return} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'model_instance_from_return'} ? : 10 0 10
15 } else {
16 0 return 0;
17 }
18 }
19  
20 1 has 'model_instance_from_state' => (is=>'lazy');
21  
22 0 ▶ sub _build_model_instance_from_state {
Subroutine _build_model_instance_from_state: not called CC=3 SCAR=24.8
23 0 ▶ if(my $config = shift->config->{'Plugin::CurrentComponents'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = +(shift())->config->{'Plugin::CurrentComponents'}) { } 0 0 0
24 0 ▶ return exists $config->{model_instance_from_state} ? $config->{model_instance_from_state} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'model_instance_from_state'} ? : 0 0 0
25 } else {
26 0 return 0;
27 }
28 }
29  
30 1 has 'view_instance_from_return' => (is=>'lazy');
31  
32 0 ▶ sub _build_view_instance_from_return {
Subroutine _build_view_instance_from_return: not called CC=3 SCAR=24.8
33 0 ▶ if(my $config = shift->config->{'Plugin::CurrentComponents'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config = +(shift())->config->{'Plugin::CurrentComponents'}) { } 0 0 0
34 0 ▶ return exists $config->{view_instance_from_return} ? $config->{view_instance_from_return} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'view_instance_from_return'} ? : 0 0 0
35 } else {
36 0 return 0;
37 }
38 }
39  
40 3 ▶sub current_model {
Subroutine current_model: called CC=3 SCAR=11.3
41 3 my ($self, $model) = @_;
42 3 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 3 3
43 3 ▶ if(defined($model)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $model) 3 0 3
44 3 $self->stash->{current_model} = $model;
45 }
46 3 return $self->stash->{current_model};
47 }
48  
49 3 ▶sub current_model_instance {
Subroutine current_model_instance: called CC=4 SCAR=14.5
50 3 my ($self, $model, @args) = @_;
51 3 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 3 3
52 3 ▶ if(defined($model)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $model) 3 0 3
53 3 ▶ $model = $self->model($model, @args) unless ref $model;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $model 0 3 3
54 3 $self->stash->{current_model_instance} = $model;
55 }
56 3 return $self->stash->{current_model_instance};
57 }
58  
59 1 ▶sub current_view {
Subroutine current_view: called CC=3 SCAR=11.3
60 1 my ($self, $view) = @_;
61 1 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
62 1 ▶ if(defined($view)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $view) 1 0 1
63 1 $self->stash->{current_view} = $view;
64 }
65 1 return $self->stash->{current_view};
66 }
67  
68 2 ▶sub current_view_instance {
Subroutine current_view_instance: called CC=4 SCAR=14.5
69 2 my ($self, $view, @args) = @_;
70 2 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 2 2
71 2 ▶ if(defined($view)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $view) 2 0 2
72 2 ▶ $view = $self->view($view, @args) unless ref $view;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $view 0 2 2
73 2 $self->stash->{current_view_instance} = $view;
74 }
75 2 return $self->stash->{current_view_instance};
76 }
77  
78 0 ▶sub build_view {
Subroutine build_view: not called CC=2 SCAR=17.9
79 0 my ($self, @args) = @_;
80 0 my ($view_name) = @{$self->action->attributes->{View}};
81 0 ▶ die "Action '@{[ $self->action ]}' doesn't have a 'View' attribute" unless $view_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $view_name 0 0 0
82 0 my $view = $self->view($view_name, @args);
83 0 $self->stash(current_view_instance=>$view);
84 0 return $view;
85 }
86  
87 0 ▶sub build_model {
Subroutine build_model: not called CC=2 SCAR=17.9
88 0 my ($self, @args) = @_;
89 0 my ($model_name) = @{$self->action->attributes->{Model}};
90 0 ▶ die "Action '@{[ $self->action ]}' doesn't have a 'Model' attribute" unless $model_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $model_name 0 0 0
91 0 my $model = $self->model($model_name, @args);
92 0 $self->stash(current_model_instance=>$model);
93 0 return $model;
94 }
95  
96 64 ▶around 'execute', sub {
Subroutine __ANON__: called CC=10 SCAR=31.1
97 64 my ($orig, $self, $class, $code, @rest ) = @_;
98 64 my $state = $self->$orig($class, $code, @rest);
99  
100 64 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->model_instance_from_return || $self->view_instance_from_return and defined $state and builtin::blessed($state)) 1 63 64
Condition44%4/9
$self->model_instance_from_return || $self->view_instance_from_return and defined $state and builtin::blessed($state)
!ll&&!rl&&r
0631
$self->model_instance_from_return || $self->view_instance_from_return and defined $state
!ll&&!rl&&r
0064
$self->model_instance_from_return or $self->view_instance_from_return
l!l&&r!l&&!r
6400
MC/DC25%1/4
$self->model_instance_from_return || $self->view_instance_from_return and defined $state and builtin::blessed($state)
$self->model_instance_from_return $self->view_instance_from_return defined $state builtin::blessed($state)
Truth table28%2/7
A or B and C and D
A= $self->model_instance_from_returnB= $self->view_instance_from_returnC= defined $stateD= builtin::blessed($state)
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
101 ($self->model_instance_from_return || $self->view_instance_from_return) &&
102 defined $state &&
103 Scalar::Util::blessed($state)
104 ) {
105 1 my $state_class = ref($state);
106 1 my $app_class = ref($self);
107 1 $state_class =~s/^$app_class\:\:(Model|View)\:\://;
108
109 1 ▶ if($self->model_instance_from_return && $self->model($state_class)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->model_instance_from_return and $self->model($state_class)) { } 1 0 1
elsif ($self->view_instance_from_return and $self->view($state_class)) { } 0 0 0
elsif ($self->model_instance_from_state) { } 0 0 0
Condition33%1/3
$self->model_instance_from_return and $self->model($state_class)
!ll&&!rl&&r
001
MC/DC0%0/2
$self->model_instance_from_return and $self->model($state_class)
$self->model_instance_from_return $self->model($state_class)
Truth table33%1/3
A and B
A= $self->model_instance_from_returnB= $self->model($state_class)
ABresult
0X0
100
111
110 1 $self->current_model_instance($state);
111 ▶ } elsif($self->view_instance_from_return && $self->view($state_class)) {
Condition0%0/3
$self->view_instance_from_return and $self->view($state_class)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->view_instance_from_return and $self->view($state_class)
$self->view_instance_from_return $self->view($state_class)
Truth table0%0/3
A and B
A= $self->view_instance_from_returnB= $self->view($state_class)
ABresult
0X0
100
111
112 0 $self->current_view_instance($state);
113 } elsif($self->model_instance_from_state) {
114 # Its an object but its not a view, but allow it anyway. Maybe terrible
115 # idea but for backcompat at least.
116 0 $self->current_model_instance($state);
117 }
118 }
119  
120 64 return $state;
121 1 };
122  
123 12 ▶around 'model', sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
124 12 my ($orig, $self, $name, @args) = @_;
125 12 ▶ if(!defined($name) && ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name and ref $self) 5 7 12
Condition66%2/3
not defined $name and ref $self
!ll&&!rl&&r
705
MC/DC50%1/2
not defined $name and ref $self
not defined $name ref $self
Truth table66%2/3
not(A) and B
A= not defined $nameB= ref $self
ABresult
0X0
100
111
126 5 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->stash->{'current_model_instance'} and $self->controller->can('current_model_instance')) { } 1 4 5
elsif (not defined $self->stash->{'current_model'} and $self->controller->can('current_model')) { } 1 4 5
Condition100%3/3
not defined $self->stash->{"current_model_instance"} and $self->controller->can("current_model_instance")
!ll&&!rl&&r
221
MC/DC100%2/2
not defined $self->stash->{"current_model_instance"} and $self->controller->can("current_model_instance")
not defined $self->stash->{"current_model_instance"} $self->controller->can("current_model_instance")
Truth table100%3/3
not(A) and B
A= not defined $self->stash->{"current_model_instance"}B= $self->controller->can("current_model_instance")
ABresult
0X0
100
111
127 !defined($self->stash->{current_model_instance}) &&
128 $self->controller->can('current_model_instance')
129 ) {
130 1 $self->current_model_instance(
131 $self->controller->current_model_instance($self));
132 ▶ } elsif(
Condition100%3/3
not defined $self->stash->{"current_model"} and $self->controller->can("current_model")
!ll&&!rl&&r
121
MC/DC100%2/2
not defined $self->stash->{"current_model"} and $self->controller->can("current_model")
not defined $self->stash->{"current_model"} $self->controller->can("current_model")
Truth table100%3/3
not(A) and B
A= not defined $self->stash->{"current_model"}B= $self->controller->can("current_model")
ABresult
0X0
100
111
133 !defined($self->stash->{current_model}) &&
134 $self->controller->can('current_model')
135 ) {
136 1 $self->current_model($self->controller->current_model($self));
137 }
138 }
139 12 return $self->$orig($name, @args);
140 1 };
141  
142 8 ▶around 'view', sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
143 8 my ($orig, $self, $name, @args) = @_;
144 8 ▶ if(!defined($name) && ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name and ref $self) 5 3 8
Condition66%2/3
not defined $name and ref $self
!ll&&!rl&&r
305
MC/DC50%1/2
not defined $name and ref $self
not defined $name ref $self
Truth table66%2/3
not(A) and B
A= not defined $nameB= ref $self
ABresult
0X0
100
111
145 5 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->stash->{'current_view_instance'} and $self->controller->can('current_view_instance')) { } 1 4 5
elsif (not defined $self->stash->{'current_view'} and $self->controller->can('current_view')) { } 1 3 4
Condition100%3/3
not defined $self->stash->{"current_view_instance"} and $self->controller->can("current_view_instance")
!ll&&!rl&&r
131
MC/DC100%2/2
not defined $self->stash->{"current_view_instance"} and $self->controller->can("current_view_instance")
not defined $self->stash->{"current_view_instance"} $self->controller->can("current_view_instance")
Truth table100%3/3
not(A) and B
A= not defined $self->stash->{"current_view_instance"}B= $self->controller->can("current_view_instance")
ABresult
0X0
100
111
146 !defined($self->stash->{current_view_instance}) &&
147 $self->controller->can('current_view_instance')
148 ) {
149 1 $self->current_view_instance(
150 $self->controller->current_view_instance($self));
151 ▶ } elsif(
Condition66%2/3
not defined $self->stash->{"current_view"} and $self->controller->can("current_view")
!ll&&!rl&&r
031
MC/DC50%1/2
not defined $self->stash->{"current_view"} and $self->controller->can("current_view")
not defined $self->stash->{"current_view"} $self->controller->can("current_view")
Truth table66%2/3
not(A) and B
A= not defined $self->stash->{"current_view"}B= $self->controller->can("current_view")
ABresult
0X0
100
111
152 !defined($self->stash->{current_view}) &&
153 $self->controller->can('current_view')
154 ) {
155 1 $self->current_view($self->controller->current_view($self));
156 }
157 }
158 8 return $self->$orig($name, @args);
159 1 };
160  
161 1 1;
162  
163 =head1 NAME
164  
165 Catalyst::Plugin::CurrentComponents - Declare current components more easily.
166  
167 =head1 SYNOPSIS
168  
169 Use the plugin in your application class:
170  
171 package MyApp;
172 use Catalyst 'CurrentComponents';
173  
174 # Optional configuration
175 MyApp->config(
176 'Plugin::CurrentComponents' => {
177 model_instance_from_return => 1,
178 view_instance_from_return => 1,
179 },
180 );
181  
182 MyApp->setup;
183  
184 Then you can use it in your controllers:
185  
186 package MyApp::Controller::Example;
187  
188 use base 'Catalyst::Controller';
189  
190 sub current_model_instance {
191 my ($self, $c) = @_;
192 return $c->model("Form::Login", user_database => $c->model('Users'));
193 }
194  
195 sub myaction :Local {
196 my ($self, $c) = @_;
197 my $c->model; # Isa 'MyApp::Model::Form::Login', or whatever that returns;
198 }
199  
200 sub set_model :Local {
201 my ($self, $c) = @_;
202 $c->current_model_instance($c->model('Foo')); # $c->model ISA 'MyApp::Model::Foo
203 }
204  
205 sub set_view :Local {
206 my ($self, $c) = @_;
207 $c->current_view_instance($c->view('Bar')); # $c->view ISA 'MyApp::View::Bar
208 }
209  
210 =head1 DESCRIPTION
211  
212 This plugin gives you an alternative to setting the current_view|model(_instance)
213 via a controller method or via context helper methods. You may find this a
214 more readable approach than setting it via the stash.
215  
216 You may also enable a global option to set the current_model_instance or the
217 current_view_instance via the return value of an action. See L</CONFIGURATION>
218  
219 Please Seee documention about Views and Models in L<Catalyst>.
220  
221 =head1 METHODS
222  
223 This plugin adds the following methods to your context.
224  
225 =head2 current_model
226  
227 Sets $c->stash->{current_model} if an argument is passed. Always returns the
228 current value of this stash key. Expects the string name of a model.
229  
230 =head2 current_model_instance
231  
232 Sets $c->stash->{current_model_instance} if an argument is passed. Always returns the
233 current value of this stash key. Expects either the instance of an already created
234 model or can accept arguments that can be validly submitted to $c->model.
235  
236 =head2 current_view
237  
238 Sets $c->stash->{current_view} if an argument is passed. Always returns the
239 current value of this stash key. Expects the string new of a view.
240  
241 =head2 current_view_instance
242  
243 Sets $c->stash->{current_view_instance} if an argument is passed. Always returns the
244 current value of this stash key. Expects either the instance of an already created
245 view or can accept arguments that can be validly submitted to $c->view.
246  
247 =head2 build_view
248  
249 =head2 build_model
250  
251 Builds the view or model and makes it the current instance.
252  
253 =head1 CONTROLLER METHODS
254  
255 This plugin will inspect the current controller for the following methods
256  
257 =head2 current_model
258  
259 =head2 current_model_instance
260  
261 Same as the context methods, but lets you set this at a controller level. Useful
262 for base classes or roles. Example:
263  
264  
265 =head1 CONFIGURATION
266  
267 This plugin supports configuration under the "Plugin::CurrentComponents" key.
268 For example:
269  
270 MyApp->config(
271 'Plugin::CurrentComponents' => {
272 model_instance_from_return => 1,
273 view_instance_from_return => 1,
274 },
275 );
276  
277 =head2 model_instance_from_return
278  
279 Allows one to set the current_model_instance from the return value of a matched
280 action. Please note this is an experimental option which is off by default.
281 The return value must be a defined, blessed objected that ISA L<Catalyst::Model>
282 for this to work. Example:
283  
284 sub set_model_by_return :Chained(/) CaptureArgs(0) {
285 my ($self, $c) = @_;
286 return $c->model('CurrentModel'); # $c->model ISA 'MyApp::Model::CurrentModel'
287 }
288  
289 =head2 view_instance_from_return
290  
291 Allows one to set the current_view_instance from the return value of a matched
292 action. Please note this is an experimental option which is off by default.
293 The return value must be a defined, blessed objected that ISA L<Catalyst::View>
294 for this to work. Example:
295  
296 sub set_view_by_return :Chained(/) CaptureArgs(0) {
297 my ($self, $c) = @_;
298 return $c->view('CurrentView'); # $c->view ISA 'MyApp::View::CurrentView'
299 }
300  
301 =head2 model_instance_from_state
302  
303 Often you want to set your current model instance to 'any type of object'. The
304 configuration L</model_instance_from_return> expects the object to be something
305 in the 'MyApp::Model' namespace. If this is not the case you can use this option.
306  
307 sub set_model_from_resultset :Chained CaptureArgs(1) {
308 my ($self, $c, $id) = @_;
309 return $c->model("Schema::User")->find($id);
310 }
311  
312 In this case the object returned is probably a 'MyApp::Schema::Result::User' so
313 the option L</model_instance_from_return> would not have worked.
314  
315 =head1 AUTHOR
316  
317 John Napiorkowski L<email:jjnapiork@cpan.org>
318
319 =head1 SEE ALSO
320
321 L<Catalyst>, L<Catalyst::Response>
322  
323 =head1 COPYRIGHT & LICENSE
324
325 Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org>
326
327 This library is free software; you can redistribute it and/or modify it under
328 the same terms as Perl itself.
329
330 =cut
↑ Summary ActionRole/CurrentModel.pm »