Data/Data.pm

filter: Statementstmt 27.7% 20 / 72 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 5 totaltotal 24.3% 26 / 107 SCAR 46.7
CC
14
Cov
22%
insert
90.0
new
42.0
data
2.0
CRAP 106.2
↑ Summary Data/Plugin.pm »
1 package Schema::Data::Data;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = 0.05;
12  
13 sub new {
14 0 ▶ my ($class, @params) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
15  
16 # Create object.
17 0 my $self = bless {}, $class;
18  
19 # db debug.
20 0 $self->{'db_debug'} = 0;
21  
22 # db user password.
23 0 $self->{'db_password'} = undef;
24  
25 # db user name.
26 0 $self->{'db_user'} = undef;
27  
28 # db options.
29 0 $self->{'db_options'} = {};
30  
31 # DSN.
32 0 $self->{'dsn'} = undef;
33  
34 # Schema module.
35 0 $self->{'schema_module'} = undef;
36  
37 # Process parameters.
38 0 set_params($self, @params);
39  
40 0 ▶ if (! defined $self->{'schema_module'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"schema_module"}) 0 0 0
41 0 err "Parameter 'schema_module' is required.";
42 }
43  
44 # Load schema.
45 0 eval 'require '.$self->{'schema_module'};
46 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Schema::Data::Data::EVAL_ERROR) 0 0 0
47 err 'Cannot load Schema module.',
48 0 'Module name', $self->{'schema_module'},
49 'Error', $EVAL_ERROR,
50 ;
51 }
52  
53 0 $self->{'_schema'} = eval {
54 $self->{'schema_module'}->connect(
55 $self->{'dsn'},
56 $self->{'db_user'},
57 $self->{'db_password'},
58 0 $self->{'db_options'},
59 );
60 };
61 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Schema::Data::Data::EVAL_ERROR) 0 0 0
62 0 err 'Cannot connect to Schema database.',
63 'Error', $EVAL_ERROR,
64 ;
65 }
66 0 ▶ if (! $self->{'_schema'}->isa('DBIx::Class::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_schema"}->isa("DBIx::Class::Schema")) 0 0 0
67 err "Instance of schema must be a 'DBIx::Class::Schema' object.",
68 0 'Reference', $self->{'_schema'}->isa,
69 ;
70 }
71  
72 0 ▶ if ($self->{'db_debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"db_debug"}) 0 0 0
73 0 $self->{'_schema'}->storage->debug(1);
74 }
75  
76 0 return $self;
77 }
78  
79 sub data {
80 0 ▶ my ($self, $variables_hr) = @_;
Subroutine data: not called CC=1 SCAR=6.9
Pod undocumented
81  
82 0 err 'Package __PACKAGE__ is abstract class. data() method must be '.
83 'defined in inherited class.';
84 }
85  
86 sub insert {
87 0 ▶ my ($self, $variables_hr) = @_;
Subroutine insert: not called CC=9 SCAR=45.0
Pod undocumented
88  
89 # Check required variables.
90 0 foreach my $required_variable ($self->required_variables) {
91 0 ▶ if (! exists $variables_hr->{$required_variable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $variables_hr->{$required_variable}) 0 0 0
92 0 err "Variable '$required_variable' is required.";
93 }
94 }
95  
96 0 my $data_hr = $self->data($variables_hr);
97 0 my %sources = map { ($self->{'_schema'}->source($_)->name => $_ ) }
98 0 $self->{'_schema'}->sources;
99  
100 # Explicit order.
101 0 my @order;
102 0 ▶ if ($data_hr->{'_order'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_hr->{'_order'}) { } 0 0 0
103 0 @order = @{$data_hr->{'_order'}};
104 0 foreach my $db_name (keys %sources) {
105 0 ▶ if (none { $_ eq $db_name } @order) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_ eq $db_name; } , @order)) 0 0 0
106 0 push @order, $db_name;
107 }
108 }
109 # Random DB names as order.
110 } else {
111 0 @order = keys %sources;
112 }
113  
114 # Insert.
115 0 foreach my $db_name (@order) {
116  
117 # Skip db table without data.
118 0 ▶ if (! exists $data_hr->{$db_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data_hr->{$db_name}) 0 0 0
119 0 next;
120 }
121  
122 0 foreach my $data_hr (@{$data_hr->{$db_name}}) {
123 0 $self->{'_schema'}->resultset($sources{$db_name})->create($data_hr);
124 }
125 }
126  
127 0 return;
128 }
129  
130 sub required_variables {
131 0 ▶ my $self = shift;
Subroutine required_variables: not called CC=1 SCAR=6.9
Pod undocumented
132  
133 0 err 'Package __PACKAGE__ is abstract class. required_variables() method must be '.
134 'defined in inherited class.';
135 }
136  
137 sub schema {
138 0 ▶ my $self = shift;
Subroutine schema: not called CC=1 SCAR=6.9
Pod undocumented
139  
140 0 return $self->{'_schema'};
141 }
142  
143 2 1;
144  
145 __END__
↑ Summary Data/Plugin.pm »