blib/lib/Mojolicious/Plugin/AnyData.pm

filter: Statementstmt 69.3% 34 / 49 Branchbran 38.8% 7 / 18 Conditioncond 25.0% 5 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 63.6% 7 / 11 Podpod 60.0% 3 / 5 totaltotal 49.1% 56 / 114 CC 18 SCAR 22.5
CC
18
Cov
53%
register
19.2
ad_import
9.5
load_data
6.0
CRAP 9.5
↑ Summary
1 ################################################################################
2 #
3 # Mojolicious::Plugin::AnyData 1.20
4 #
5 # uses perl data in the memory like a database source.
6 # author: Alexander Ponomarev <shootnix@cpan.org>
7 #
8 ################################################################################
9  
10 package Mojolicious::Plugin::AnyData;
11  
12 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 $Mojolicious::Plugin::AnyData::VERSION = '1.20';
14 }
15  
16 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 has 'dbh';
21 2 has 'app';
22  
23 # Run on startup
24 1 ▶sub register {
Subroutine register: called CC=8 SCAR=29.5
Pod documented
25 1 my ($self, $app, $param) = @_;
26
27 1 ▶ return if $app->mode && $app->mode eq 'production';
Branch: 1 branch
BranchTrueFalseTotal
if $app->mode and $app->mode eq "production" 0 1 1
Condition33%1/3
$app->mode and $app->mode eq "production"
!ll&&!rl&&r
010
MC/DC0%0/2
$app->mode and $app->mode eq "production"
$app->mode $app->mode eq "production"
Truth table33%1/3
A and B
A= $app->modeB= $app->mode eq "production"
ABresult
0X0
100
111
28  
29 1 my ($data, $data_file);
30 1 my $func = $param->{func};
31 1 my $helper = $param->{helper};
32 1 ▶ $helper ||= 'db';
Condition50%1/2
$helper ||= "db"
l!l
10
MC/DC0%0/1
$helper ||= "db"
$helper
Truth table50%1/2
A
A= $helper
Aresult
11
00
33 1 my $dbh = DBI->connect('dbi:AnyData:(RaiseError=>1)');
34
35 1 $self->dbh($dbh);
36 1 $self->app($app);
37
38 1 ▶ if ( $param->{load_data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"load_data"}) 1 0 1
39 1 $self->load_data( $param->{load_data} );
40 }
41
42 0 ▶ if ( $func && ref $func eq 'ARRAY' && scalar @$func > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func and ref $func eq "ARRAY" and scalar @$func > 0) 0 0 0
Condition0%0/6
$func and ref $func eq "ARRAY" and scalar @$func > 0
!ll&&!rl&&r
000
$func and ref $func eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$func and ref $func eq "ARRAY" and scalar @$func > 0
$func ref $func eq "ARRAY" scalar @$func > 0
Truth table0%0/4
A and B and C
A= $funcB= ref $func eq "ARRAY"C= scalar @$func > 0
ABCresult
0XX0
10X0
1100
1111
43 #$dbh->func(@$func);
44 0 $self->func(@$func);
45 }
46
47 0 ▶ $app->helper( $param->{helper} => sub { return $dbh } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ $app->helper( any_data => sub { return $self } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 }
50  
51 # Load data into the memory using different ways
52 # in: arrayref (data structure) or scalar (filename)
53 1 ▶sub load_data {
Subroutine load_data: called CC=4 SCAR=17.9
Pod documented
54 1 my ($self, $data) = @_;
55
56 1 ▶ return unless $self->dbh && $self->app;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh and $self->app 0 1 1
Condition33%1/3
$self->dbh and $self->app
!ll&&!rl&&r
001
MC/DC0%0/2
$self->dbh and $self->app
$self->dbh $self->app
Truth table33%1/3
A and B
A= $self->dbhB= $self->app
ABresult
0X0
100
111
57
58 1 ▶ if ( ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) { } 1 0 1
59 1 $self->ad_import($data);
60 }
61 else {
62 0 my $data_file = $data;
63 0 $data = $self->app->plugin(config => {
64 file => $data_file,
65 stash_key => 'any_data',
66 });
67 0 $self->ad_import($data);
68 }
69 }
70  
71 # Provide the wrapper method for DBD::AnyData::func method
72 0 ▶sub func {
Subroutine func: not called CC=2 SCAR=17.9
Pod documented
73 0 my ($self, $table_name, $table_type, $table_data, $table_method) = @_;
74
75 0 ▶ return unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 0 0 0
76
77 0 $self->dbh->func($table_name, 'ad_clear');
78 0 $self->dbh->func( $table_name, $table_type, $table_data, $table_method );
79 }
80  
81 # Execute DBD::AnyData::func method in order to load data into the memory
82 1 ▶sub ad_import {
Subroutine ad_import: called CC=7 SCAR=22.5
Pod undocumented
83 1 my ($self, $data) = @_;
84
85 1 ▶ return unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 0 1 1
86
87 1 ▶ if ( $data && ref $data eq 'HASH' && keys %$data > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq "HASH" and keys %$data > 0) 1 0 1
Condition33%2/6
$data and ref $data eq "HASH" and keys %$data > 0
!ll&&!rl&&r
001
$data and ref $data eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/3
$data and ref $data eq "HASH" and keys %$data > 0
$data ref $data eq "HASH" keys %$data > 0
Truth table25%1/4
A and B and C
A= $dataB= ref $data eq "HASH"C= keys %$data > 0
ABCresult
0XX0
10X0
1100
1111
88 TABLE:
89 1 for my $table_name ( keys %$data ) {
90 1 ▶ next TABLE unless ref $data->{$table_name} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{$table_name} eq "ARRAY" 0 1 1
91
92 1 $self->dbh->func($table_name, 'ad_clear');
93 1 $self->dbh->func($table_name, 'ARRAY', $data->{$table_name}, 'ad_import');
94 }
95 }
96 }
97  
98 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my ($self) = @_;
100
101 0 return $Mojolicious::Plugin::AnyData::VERSION;
102 }
103  
104 2 1;
105 __END__
↑ Summary