Dash.pm

filter: Statementstmt 52.6% 188 / 357 Branchbran 23.2% 27 / 116 Conditioncond 21.9% 9 / 41 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 69.2% 27 / 39 Podpod 50.0% 2 / 4 totaltotal 43.6% 254 / 582 CC 94 SCAR 50.4
CC
94
Cov
44%
_filter_resources
506.0
_process_callback_arguments
313.9
__ANON__
90.0
CRAP 154.2
↑ Summary Dash/Html/ComponentsFunctions.pm »
1 package Dash;
2  
3 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.11'; # VERSION
8  
9 # ABSTRACT: Analytical Web Apps in Perl (Port of Plotly's Dash to Perl)
10  
11 # TODO Enable signatures?
12  
13 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Browser::Open;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Dash::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Dash::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Dash::Exceptions::NoLayoutException;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Dash::Exceptions::PreventUpdate;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Dash::Backend::Mojolicious::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # TODO Add ci badges
25  
26 6 has app_name => ( is => 'ro',
27 default => __PACKAGE__ );
28  
29 6 has port => ( is => 'ro',
30 default => 8080 );
31  
32 has external_stylesheets => ( is => 'rw',
33 11 ▶ default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35 has _layout => ( is => 'rw',
36 11 ▶ default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 has _callbacks => ( is => 'rw',
39 11 ▶ default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 6 has _rendered_scripts => ( is => 'rw',
42 default => "" );
43  
44 6 has _rendered_external_stylesheets => ( is => 'rw',
45 default => "" );
46  
47 has backend => ( is => 'rw',
48 11 ▶ default => sub { Dash::Backend::Mojolicious::App->new( dash_app => shift ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 has config => ( is => 'rw',
51 11 ▶ default => sub { Dash::Config->new() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
52  
53 13 ▶sub layout {
Subroutine layout: called CC=5 SCAR=16.2
Pod documented
54 13 my $self = shift;
55 13 my $layout = shift;
56 13 ▶ if ( defined $layout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $layout) { } 11 2 13
57 11 my $type = ref $layout;
58 11 ▶ if ( $type eq 'CODE' || ( Scalar::Util::blessed($layout) && $layout->isa('Dash::BaseComponent') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'CODE' or builtin::blessed($layout) and $layout->isa('Dash::BaseComponent')) { } 9 2 11
Condition66%4/6
$type eq "CODE" or builtin::blessed($layout) and $layout->isa("Dash::BaseComponent")
l!l&&r!l&&!r
092
builtin::blessed($layout) and $layout->isa("Dash::BaseComponent")
!ll&&!rl&&r
209
MC/DC33%1/3
$type eq "CODE" or builtin::blessed($layout) and $layout->isa("Dash::BaseComponent")
$type eq "CODE" builtin::blessed($layout) $layout->isa("Dash::BaseComponent")
Truth table50%2/4
A or B and C
A= $type eq "CODE"B= builtin::blessed($layout)C= $layout->isa("Dash::BaseComponent")
ABCresult
1XX1
0111
00X0
0100
59 9 $self->_layout($layout);
60 } else {
61 2 Dash::Exceptions::NoLayoutException->throw(
62 'Layout must be a dash component or a function that returns a dash component');
63 }
64 } else {
65 2 $layout = $self->_layout;
66 }
67 11 return $layout;
68 }
69  
70 8 ▶sub callback {
Subroutine callback: called CC=1 SCAR=0.0
Pod documented
71 8 my $self = shift;
72 8 my %callback = $self->_process_callback_arguments(@_);
73  
74 # TODO check_callback
75 # TODO Callback map
76 8 my $output = $callback{Output};
77 8 my $callback_id = $self->_create_callback_id($output);
78 8 my $callbacks = $self->_callbacks;
79 8 $callbacks->{$callback_id} = \%callback;
80 8 return $self;
81 }
82  
83 6 my $no_update;
84 6 my $internal_no_update = bless( \$no_update, 'Dash::Internal::NoUpdate' );
85  
86 0 ▶sub no_update {
Subroutine no_update: not called CC=1 SCAR=6.9
Pod undocumented
87 0 return $internal_no_update;
88 }
89  
90 8 ▶sub _process_callback_arguments {
Subroutine _process_callback_arguments: called CC=24 SCAR=57.5
91 8 my $self = shift;
92  
93 8 my %callback;
94  
95 # 1. all refs: 1 blessed, 1 array, 1 code or 2 array, 1 code
96 # Hash with keys Output, Inputs, callback
97 # 2. Values content: hashref or arrayref[hashref], arrayref[hashref], coderef
98 # 3. Values content: blessed output or arrayref[blessed], arrayref[blessed], coderef
99  
100 8 ▶ if ( scalar @_ < 5 ) { # Unamed arguments, put names
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 5) { } 0 8 8
101 0 my ( $output_index, $input_index, $state_index, $callback_index );
102  
103 0 my $index = 0;
104 0 for my $argument (@_) {
105 0 my $type = ref $argument;
106 0 ▶ if ( $type eq 'CODE' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 0 0 0
elsif (builtin::blessed($argument)) { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'SCALAR') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
elsif ($type eq '') { } 0 0 0
107 0 $callback_index = $index;
108 } elsif ( Scalar::Util::blessed $argument) {
109 0 ▶ if ( $argument->isa('Dash::Dependencies::Output') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argument->isa("Dash::Dependencies::Output")) 0 0 0
110 0 $output_index = $index;
111 }
112 } elsif ( $type eq 'ARRAY' ) {
113 0 ▶ if ( scalar @$argument > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$argument > 0) { } 0 0 0
114 0 my $first_element = $argument->[0];
115 0 ▶ if ( Scalar::Util::blessed $first_element) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($first_element)) 0 0 0
116 0 ▶ if ( $first_element->isa('Dash::Dependencies::Output') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($first_element->isa('Dash::Dependencies::Output')) { } 0 0 0
elsif ($first_element->isa('Dash::Dependencies::Input')) { } 0 0 0
elsif ($first_element->isa('Dash::Dependencies::State')) { } 0 0 0
117 0 $output_index = $index;
118 } elsif ( $first_element->isa('Dash::Dependencies::Input') ) {
119 0 $input_index = $index;
120 } elsif ( $first_element->isa('Dash::Dependencies::State') ) {
121 0 $state_index = $index;
122 }
123 }
124 } else {
125 0 die "Can't use empty arrayrefs as arguments";
126 }
127 } elsif ( $type eq 'SCALAR' ) {
128 0 die
129 "Can't mix scalarref arguments with objects when not using named paremeters. Please use named parameters for all arguments or classes for all arguments";
130 } elsif ( $type eq 'HASH' ) {
131 0 die
132 "Can't mix hashref arguments with objects when not using named parameters. Please use named parameters for all arguments or classes for all arguments";
133 } elsif ( $type eq '' ) {
134 0 die
135 "Can't mix scalar arguments with objects when not using named parameters. Please use named parameters for all arguments or classes for all arguments";
136 }
137 0 $index++;
138 }
139 0 ▶ if ( !defined $output_index ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output_index) 0 0 0
140 0 die "Can't find callback output";
141 }
142 0 ▶ if ( !defined $input_index ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input_index) 0 0 0
143 0 die "Can't find callback inputs";
144 }
145 0 ▶ if ( !defined $callback_index ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $callback_index) 0 0 0
146 0 die "Can't find callback function";
147 }
148  
149 0 $callback{Output} = $_[$output_index];
150 0 $callback{Inputs} = $_[$input_index];
151 0 $callback{callback} = $_[$callback_index];
152 0 ▶ if ( defined $state_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state_index) 0 0 0
153 0 $callback{State} = $_[$state_index];
154 }
155 } else { # Named arguments
156 # TODO check keys ¿Params::Validate or similar?
157 8 %callback = @_;
158 }
159  
160 # Convert Output & input to hashrefs
161 8 for my $key ( keys %callback ) {
162 26 my $value = $callback{$key};
163  
164 26 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 12 14 26
elsif (builtin::blessed($value)) { } 0 14 14
165 12 my @hashes;
166 12 for my $dependency (@$value) {
167 14 ▶ if ( Scalar::Util::blessed $dependency) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($dependency)) { } 0 14 14
168 0 my %dependency_hash = %$dependency;
169 0 push @hashes, \%dependency_hash;
170 } else {
171 14 push @hashes, $dependency;
172 }
173 }
174 12 $callback{$key} = \@hashes;
175 } elsif ( Scalar::Util::blessed $value) {
176 0 my %dependency_hash = %$value;
177 0 $callback{$key} = \%dependency_hash;
178 }
179 }
180  
181 8 return %callback;
182 }
183  
184 8 ▶sub _create_callback_id {
Subroutine _create_callback_id: called CC=2 SCAR=6.9
185 8 my $self = shift;
186 8 my $output = shift;
187  
188 8 ▶ if ( ref $output eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $output eq "ARRAY") 2 6 8
189 2 return ".." . join( "...", map { $_->{component_id} . "." . $_->{component_property} } @$output ) . "..";
190 }
191  
192 6 return $output->{component_id} . "." . $output->{component_property};
193 }
194  
195 0 ▶sub run_server {
Subroutine run_server: not called CC=2 SCAR=17.9
Pod undocumented
196 0 my $self = shift;
197  
198 0 $self->_render_and_cache_scripts();
199 0 $self->_render_and_cache_external_stylesheets();
200  
201 # Opening the browser before starting the daemon works because
202 # open_browser returns inmediately
203 # TODO Open browser optional
204 0 ▶ if ( not caller(1) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller 1) 0 0 0
205 0 Browser::Open::open_browser( 'http://127.0.0.1:' . $self->port );
206 0 $self->backend->start( 'daemon', '-l', 'http://*:' . $self->port );
207 }
208 0 return $self->backend;
209 }
210  
211 4 ▶sub _dependencies {
Subroutine _dependencies: called CC=7 SCAR=19.5
212 4 my $self = shift;
213 4 my $dependencies = [];
214 4 for my $callback ( values %{ $self->_callbacks } ) {
215 3 my $rendered_callback = { clientside_function => JSON::null };
216 3 my $states = [];
217 3 for my $state ( @{ $callback->{State} } ) {
218 my $rendered_state = { id => $state->{component_id},
219 property => $state->{component_property}
220 1 };
221 1 push @$states, $rendered_state;
222 }
223 3 $rendered_callback->{state} = $states;
224 3 my $inputs = [];
225 3 for my $input ( @{ $callback->{Inputs} } ) {
226 my $rendered_input = { id => $input->{component_id},
227 property => $input->{component_property}
228 3 };
229 3 push @$inputs, $rendered_input;
230 }
231 3 $rendered_callback->{inputs} = $inputs;
232 3 my $output_type = ref $callback->{Output};
233 3 ▶ if ( $output_type eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($output_type eq 'ARRAY') { } 1 2 3
elsif ($output_type eq 'HASH') { } 2 0 2
234 1 $rendered_callback->{'output'} .= '.';
235 1 for my $output ( @{ $callback->{'Output'} } ) {
236 $rendered_callback->{'output'} .=
237 2 '.' . join( '.', $output->{component_id}, $output->{component_property} ) . '..';
238 }
239 } elsif ( $output_type eq 'HASH' ) {
240 $rendered_callback->{'output'} =
241 2 join( '.', $callback->{'Output'}{component_id}, $callback->{'Output'}{component_property} );
242 } else {
243 0 die 'Dependecy type for callback not implemented';
244 }
245 3 push @$dependencies, $rendered_callback;
246 }
247 4 return $dependencies;
248 }
249  
250 6 ▶sub _update_component {
Subroutine _update_component: called CC=21 SCAR=32.6
251 6 my $self = shift;
252 6 my $request = shift;
253  
254 6 ▶ if ( scalar( values %{ $self->_callbacks } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar values %{$self->_callbacks;} > 0) { } 5 1 6
255 5 my $callbacks = $self->_search_callback( $request->{'output'} );
256 5 ▶ if ( scalar @$callbacks > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$callbacks > 1) { } 0 5 5
elsif (scalar @$callbacks == 1) { } 5 0 5
257 0 die 'Not implemented multiple callbacks';
258 } elsif ( scalar @$callbacks == 1 ) {
259 5 my $callback = $callbacks->[0];
260 5 my @callback_arguments = ();
261 5 my $callback_context = {};
262 5 for my $callback_input ( @{ $callback->{Inputs} } ) {
263 5 my ( $component_id, $component_property ) = @{$callback_input}{qw(component_id component_property)};
264 5 for my $change_input ( @{ $request->{inputs} } ) {
265 5 my ( $id, $property, $value ) = @{$change_input}{qw(id property value)};
266 5 ▶ if ( $component_id eq $id && $component_property eq $property ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component_id eq $id and $component_property eq $property) 5 0 5
Condition33%1/3
$component_id eq $id and $component_property eq $property
!ll&&!rl&&r
005
MC/DC0%0/2
$component_id eq $id and $component_property eq $property
$component_id eq $id $component_property eq $property
Truth table33%1/3
A and B
A= $component_id eq $idB= $component_property eq $property
ABresult
0X0
100
111
267 5 push @callback_arguments, $value;
268 5 $callback_context->{inputs}{ $id . "." . $property } = $value;
269 5 last;
270 }
271 }
272 }
273 5 for my $callback_input ( @{ $callback->{State} } ) {
274 1 my ( $component_id, $component_property ) = @{$callback_input}{qw(component_id component_property)};
275 1 for my $change_input ( @{ $request->{state} } ) {
276 1 my ( $id, $property, $value ) = @{$change_input}{qw(id property value)};
277 1 ▶ if ( $component_id eq $id && $component_property eq $property ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component_id eq $id and $component_property eq $property) 1 0 1
Condition33%1/3
$component_id eq $id and $component_property eq $property
!ll&&!rl&&r
001
MC/DC0%0/2
$component_id eq $id and $component_property eq $property
$component_id eq $id $component_property eq $property
Truth table33%1/3
A and B
A= $component_id eq $idB= $component_property eq $property
ABresult
0X0
100
111
278 1 push @callback_arguments, $value;
279 1 $callback_context->{states}{ $id . "." . $property } = $value;
280 1 last;
281 }
282 }
283 }
284  
285 5 $callback_context->{triggered} = [];
286 5 for my $triggered_input ( @{ $request->{changedPropIds} } ) {
287 5 push @{ $callback_context->{triggered} },
288 { prop_id => $triggered_input,
289 5 value => $callback_context->{inputs}{$triggered_input}
290 };
291 }
292 5 push @callback_arguments, $callback_context;
293  
294 5 my $output_type = ref $callback->{Output};
295 5 ▶ if ( $output_type eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($output_type eq 'ARRAY') { } 1 4 5
elsif ($output_type eq 'HASH') { } 4 0 4
296 1 my @return_value = $callback->{callback}(@callback_arguments);
297 1 my $props_updated = {};
298 1 my $index_output = 0;
299 1 my $some_updated = 0;
300 1 for my $output ( @{ $callback->{'Output'} } ) {
301 2 my $output_value = $return_value[ $index_output++ ];
302 2 ▶ if ( !( Scalar::Util::blessed($output_value) && $output_value->isa('Dash::Internal::NoUpdate') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($output_value) and $output_value->isa("Dash::Internal::NoUpdate")) 2 0 2
Condition33%1/3
builtin::blessed($output_value) and $output_value->isa("Dash::Internal::NoUpdate")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($output_value) and $output_value->isa("Dash::Internal::NoUpdate")
builtin::blessed($output_value) $output_value->isa("Dash::Internal::NoUpdate")
Truth table33%1/3
A and B
A= builtin::blessed($output_value)B= $output_value->isa("Dash::Internal::NoUpdate")
ABresult
0X0
100
111
303 $props_updated->{ $output->{component_id} } =
304 2 { $output->{component_property} => $output_value };
305 2 $some_updated = 1;
306 }
307 }
308 1 ▶ if ($some_updated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($some_updated) { } 1 0 1
309 1 return { response => $props_updated, multi => JSON::true };
310 } else {
311 0 Dash::Exceptions::PreventUpdate->throw;
312 }
313 } elsif ( $output_type eq 'HASH' ) {
314 4 my $updated_value = $callback->{callback}(@callback_arguments);
315 3 ▶ if ( Scalar::Util::blessed($updated_value) && $updated_value->isa('Dash::Internal::NoUpdate') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($updated_value) and $updated_value->isa("Dash::Internal::NoUpdate")) 0 3 3
Condition33%1/3
builtin::blessed($updated_value) and $updated_value->isa("Dash::Internal::NoUpdate")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($updated_value) and $updated_value->isa("Dash::Internal::NoUpdate")
builtin::blessed($updated_value) $updated_value->isa("Dash::Internal::NoUpdate")
Truth table33%1/3
A and B
A= builtin::blessed($updated_value)B= $updated_value->isa("Dash::Internal::NoUpdate")
ABresult
0X0
100
111
316 0 Dash::Exceptions::PreventUpdate->throw;
317 }
318 3 my $updated_property = ( split( /\./, $request->{output} ) )[-1];
319 3 my $props_updated = { $updated_property => $updated_value };
320 3 return { response => { props => $props_updated } };
321 } else {
322 0 die 'Callback not supported';
323 }
324 } else {
325 0 return { response => "There is no matching callback" };
326 }
327  
328 } else {
329 1 return { response => "There is no registered callbacks" };
330 }
331 0 return { response => "Internal error" };
332 }
333  
334 5 ▶sub _search_callback {
Subroutine _search_callback: called CC=1 SCAR=0.0
335 5 my $self = shift;
336 5 my $output = shift;
337  
338 5 my $callbacks = $self->_callbacks;
339 5 my @matching_callbacks = ( $callbacks->{$output} );
340 5 return \@matching_callbacks;
341 }
342  
343 1 ▶sub _rendered_stylesheets {
Subroutine _rendered_stylesheets: called CC=1 SCAR=0.0
344 1 return '';
345 }
346  
347 0 ▶sub _render_external_stylesheets {
Subroutine _render_external_stylesheets: not called CC=2 SCAR=17.9
348 0 my $self = shift;
349 0 my $stylesheets = $self->external_stylesheets;
350 0 my $rendered_external_stylesheets = "";
351 0 for my $stylesheet (@$stylesheets) {
352 0 $rendered_external_stylesheets .= '<link rel="stylesheet" href="' . $stylesheet . '">' . "\n";
353 }
354 0 return $rendered_external_stylesheets;
355 }
356  
357 0 ▶sub _render_and_cache_external_stylesheets {
Subroutine _render_and_cache_external_stylesheets: not called CC=1 SCAR=6.9
358 0 my $self = shift;
359 0 my $stylesheets = $self->_render_external_stylesheets();
360 0 $self->_rendered_external_stylesheets($stylesheets);
361 }
362  
363 0 ▶sub _render_and_cache_scripts {
Subroutine _render_and_cache_scripts: not called CC=1 SCAR=6.9
364 0 my $self = shift;
365 0 my $scripts = $self->_render_scripts();
366 0 $self->_rendered_scripts($scripts);
367 }
368  
369 0 ▶sub _render_dash_config {
Subroutine _render_dash_config: not called CC=1 SCAR=6.9
370 0 my $self = shift;
371 0 my $json = JSON->new->utf8->allow_blessed->convert_blessed;
372 0 return '<script id="_dash-config" type="application/json">' . $json->encode( $self->config ) . '</script>';
373 }
374  
375 0 ▶sub _dash_renderer_js_dependencies {
Subroutine _dash_renderer_js_dependencies: not called CC=3 SCAR=24.8
376 0 my $js_dist_dependencies = Dash::Renderer::_js_dist_dependencies();
377 0 my @js_deps = ();
378 0 for my $deps (@$js_dist_dependencies) {
379 0 my $external_url = $deps->{external_url};
380 0 my $relative_package_path = $deps->{relative_package_path};
381 0 my $namespace = $deps->{namespace};
382 0 my $dep_count = 0;
383 0 for my $dep ( @{ $relative_package_path->{prod} } ) {
384 my $js_dep = { namespace => $namespace,
385 relative_package_path => $dep,
386 dev_package_path => $relative_package_path->{dev}[$dep_count],
387 0 external_url => $external_url->{prod}[$dep_count]
388 };
389 0 push @js_deps, $js_dep;
390 0 $dep_count++;
391 }
392 }
393 0 \@js_deps;
394 }
395  
396 0 ▶sub _dash_renderer_js_deps {
Subroutine _dash_renderer_js_deps: not called CC=1 SCAR=6.9
397 0 return Dash::Renderer::_js_dist();
398 }
399  
400 0 ▶sub _render_dash_renderer_script {
Subroutine _render_dash_renderer_script: not called CC=1 SCAR=6.9
401 0 return '<script id="_dash-renderer" type="application/javascript">var renderer = new DashRenderer();</script>';
402 }
403  
404 0 ▶sub _render_scripts {
Subroutine _render_scripts: not called CC=5 SCAR=34.0
405 0 my $self = shift;
406  
407 # First dash_renderer dependencies
408 0 my $scripts_dependencies = $self->_dash_renderer_js_dependencies;
409  
410 # Traverse layout and recover javascript dependencies
411 # TODO auto register dependencies on component creation to avoid traversing and filter too much dependencies
412 0 my $layout = $self->layout;
413  
414 0 my $visitor;
415 0 my $stack_depth_limit = 1000;
416 0 ▶ $visitor = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
417 0 my $node = shift;
418 0 my $stack_depth = shift;
419 0 ▶ if ( $stack_depth++ >= $stack_depth_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack_depth++ >= $stack_depth_limit) 0 0 0
420  
421 # TODO warn user that layout is too deep
422 0 return;
423 }
424 0 my $type = ref $node;
425 0 ▶ if ( $type eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type ne '') { } 0 0 0
426 0 for my $key ( keys %$node ) {
427 0 $visitor->( $node->{$key}, $stack_depth );
428 }
429 } elsif ( $type eq 'ARRAY' ) {
430 0 for my $element (@$node) {
431 0 $visitor->( $element, $stack_depth );
432 }
433 } elsif ( $type ne '' ) {
434 0 my $node_dependencies = $node->_js_dist();
435 0 ▶ push @$scripts_dependencies, @$node_dependencies if defined $node_dependencies;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node_dependencies 0 0 0
436 0 ▶ if ( $node->can('children') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("children")) 0 0 0
437 0 $visitor->( $node->children, $stack_depth );
438 }
439 }
440 0 };
441  
442 0 $visitor->( $layout, 0 );
443  
444 0 my $rendered_scripts = "";
445 0 $rendered_scripts .= $self->_render_dash_config();
446 0 push @$scripts_dependencies, @{ $self->_dash_renderer_js_deps() };
447 0 my $filtered_resources = $self->_filter_resources($scripts_dependencies);
448 0 my %rendered = ();
449 0 for my $dep (@$filtered_resources) {
450 0 ▶ my $dynamic = $dep->{dynamic} // 0;
Condition0%0/2
$dep->{"dynamic"} // 0
l!l
00
MC/DC0%0/1
$dep->{"dynamic"} // 0
$dep->{"dynamic"}
Truth table0%0/2
A
A= $dep->{"dynamic"}
Aresult
11
00
451 0 ▶ if ( !$dynamic ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dynamic) 0 0 0
452 0 my $resource_path_part = join( "/", $dep->{namespace}, $dep->{relative_package_path} );
453 0 ▶ if ( !$rendered{$resource_path_part} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rendered{$resource_path_part}) 0 0 0
454 0 $rendered_scripts .=
455 '<script src="/' . join( "/", '_dash-component-suites', $resource_path_part ) . '"></script>' . "\n";
456 0 $rendered{$resource_path_part} = 1;
457 }
458 }
459 }
460 0 $rendered_scripts .= $self->_render_dash_renderer_script();
461  
462 0 return $rendered_scripts;
463 }
464  
465 0 ▶sub _filter_resources {
Subroutine _filter_resources: not called CC=22 SCAR=62.3
466 0 my $self = shift;
467 0 my $resources = shift;
468 0 my %params = @_;
469 0 ▶ my $dev_bundles = $params{dev_bundles} // 0;
Condition0%0/2
$params{"dev_bundles"} // 0
l!l
00
MC/DC0%0/1
$params{"dev_bundles"} // 0
$params{"dev_bundles"}
Truth table0%0/2
A
A= $params{"dev_bundles"}
Aresult
11
00
470 0 ▶ my $eager_loading = $params{eager_loading} // 0;
Condition0%0/2
$params{"eager_loading"} // 0
l!l
00
MC/DC0%0/1
$params{"eager_loading"} // 0
$params{"eager_loading"}
Truth table0%0/2
A
A= $params{"eager_loading"}
Aresult
11
00
471 0 ▶ my $serve_locally = $params{serve_locally} // 1;
Condition0%0/2
$params{"serve_locally"} // 1
l!l
00
MC/DC0%0/1
$params{"serve_locally"} // 1
$params{"serve_locally"}
Truth table0%0/2
A
A= $params{"serve_locally"}
Aresult
11
00
472  
473 0 my $filtered_resources = [];
474 0 for my $resource (@$resources) {
475 0 my $filtered_resource = {};
476 0 my $dynamic = $resource->{dynamic};
477 0 ▶ if ( defined $dynamic ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dynamic) 0 0 0
478 0 $filtered_resource->{dynamic} = $dynamic;
479 }
480 0 my $async = $resource->{async};
481 0 ▶ if ( defined $async ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $async) 0 0 0
482 0 ▶ if ( defined $dynamic ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dynamic) 0 0 0
483 0 die "A resource can't have both dynamic and async: " + to_json($resource);
484 }
485 0 my $dynamic = 1;
486 0 ▶ if ( $async eq 'lazy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($async eq 'lazy') { } 0 0 0
487 0 $dynamic = 1;
488 } else {
489 0 ▶ if ( $async eq 'eager' && !$eager_loading ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($async eq 'eager' and not $eager_loading) { } 0 0 0
Condition0%0/3
$async eq "eager" and not $eager_loading
!ll&&!rl&&r
000
MC/DC0%0/2
$async eq "eager" and not $eager_loading
$async eq "eager" not $eager_loading
Truth table0%0/3
A and not(B)
A= $async eq "eager"B= not $eager_loading
ABresult
0X0
100
111
490 0 $dynamic = 1;
491 } else {
492 0 ▶ if ( $async && !$eager_loading ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($async and not $eager_loading) { } 0 0 0
Condition0%0/3
$async and not $eager_loading
!ll&&!rl&&r
000
MC/DC0%0/2
$async and not $eager_loading
$async not $eager_loading
Truth table0%0/3
A and not(B)
A= $asyncB= not $eager_loading
ABresult
0X0
100
111
493 0 $dynamic = 1;
494 } else {
495 0 $dynamic = 0;
496 }
497 }
498 }
499 0 $filtered_resource->{dynamic} = $dynamic;
500 }
501 0 my $namespace = $resource->{namespace};
502 0 ▶ if ( defined $namespace ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace) 0 0 0
503 0 $filtered_resource->{namespace} = $namespace;
504 }
505 0 my $external_url = $resource->{external_url};
506 0 ▶ if ( defined $external_url && !$serve_locally ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $external_url and not $serve_locally) { } 0 0 0
Condition0%0/3
defined $external_url and not $serve_locally
!ll&&!rl&&r
000
MC/DC0%0/2
defined $external_url and not $serve_locally
defined $external_url not $serve_locally
Truth table0%0/3
A and not(B)
A= defined $external_urlB= not $serve_locally
ABresult
0X0
100
111
507 0 $filtered_resource->{external_url} = $external_url;
508 } else {
509 0 my $dev_package_path = $resource->{dev_package_path};
510 0 ▶ if ( defined $dev_package_path && $dev_bundles ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dev_package_path and $dev_bundles) { } 0 0 0
Condition0%0/3
defined $dev_package_path and $dev_bundles
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dev_package_path and $dev_bundles
defined $dev_package_path $dev_bundles
Truth table0%0/3
A and B
A= defined $dev_package_pathB= $dev_bundles
ABresult
0X0
100
111
511 0 $filtered_resource->{relative_package_path} = $dev_package_path;
512 } else {
513 0 my $relative_package_path = $resource->{relative_package_path};
514 0 ▶ if ( defined $relative_package_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $relative_package_path) { } 0 0 0
515 0 $filtered_resource->{relative_package_path} = $relative_package_path;
516 } else {
517 0 my $absolute_path = $resource->{absolute_path};
518 0 ▶ if ( defined $absolute_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $absolute_path) { } 0 0 0
519 0 $filtered_resource->{absolute_path} = $absolute_path;
520 } else {
521 0 my $asset_path = $resource->{asset_path};
522 0 ▶ if ( defined $asset_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $asset_path) { } 0 0 0
523 0 my $stat_info = path( $resource->{filepath} )->stat;
524 0 $filtered_resource->{asset_path} = $asset_path;
525 0 $filtered_resource->{ts} = $stat_info->mtime;
526 } else {
527 0 ▶ if ($serve_locally) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serve_locally) { } 0 0 0
528 0 warn
529 'There is no local version of this resource. Please consider using external_scripts or external_stylesheets : '
530 + to_json($resource);
531 0 next;
532 } else {
533 0 die
534 'There is no relative_package-path, absolute_path or external_url for this resource : '
535 + to_json($resource);
536 }
537 }
538 }
539 }
540 }
541 }
542  
543 0 push @$filtered_resources, $filtered_resource;
544 }
545 0 return $filtered_resources;
546 }
547  
548 1 ▶sub _filename_from_file_with_fingerprint {
Subroutine _filename_from_file_with_fingerprint: called CC=3 SCAR=12.6
549 1 my $self = shift;
550 1 my $file = shift;
551 1 my @path_parts = split( /\//, $file );
552 1 my @name_parts = split( /\./, $path_parts[-1] );
553  
554 # Check if the resource has a fingerprint
555 1 ▶ if ( ( scalar @name_parts ) > 2 && $name_parts[1] =~ /^v[\w-]+m[0-9a-fA-F]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @name_parts > 2 and $name_parts[1] =~ /^v[\w-]+m[0-9a-fA-F]+$/u) 0 1 1
Condition33%1/3
scalar @name_parts > 2 and $name_parts[1] =~ /^v[\w-]+m[0-9a-fA-F]+$/u
!ll&&!rl&&r
010
MC/DC0%0/2
scalar @name_parts > 2 and $name_parts[1] =~ /^v[\w-]+m[0-9a-fA-F]+$/u
scalar @name_parts > 2 $name_parts[1] =~ /^v[\w-]+m[0-9a-fA-F]+$/u
Truth table33%1/3
A and B
A= scalar @name_parts > 2B= $name_parts[1] =~ /^v[\w-]+m[0-9a-fA-F]+$/u
ABresult
0X0
100
111
556 0 my $original_name = join( ".", $name_parts[0], @name_parts[ 2 .. ( scalar @name_parts - 1 ) ] );
557 0 $file = join( "/", @path_parts[ 0 .. ( scalar @path_parts - 2 ) ], $original_name );
558 }
559  
560 1 return $file;
561 }
562  
563 6 1;
564  
565 __END__
↑ Summary Dash/Html/ComponentsFunctions.pm »