blib/lib/DBIx/Class/EasyConf/YAML.pm

filter: Statementstmt 14.4% 14 / 97 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 4 / 12 Podpod 0.0% 0 / 3 totaltotal 12.0% 18 / 149 CC 25 SCAR 33.8
CC
25
Cov
11%
_set_relationships
56.0
print_configuration
56.0
configure
23.2
CRAP 29.3
↑ Summary
1 package DBIx::Class::EasyConf::YAML;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = '0.11';
8 1 our $VERBOSE = undef;
9  
10 0 ▶sub configure {
Subroutine configure: not called CC=5 SCAR=31.5
Pod undocumented
11 0 my ($package, $fh) = @_;
12  
13 0 my %args = map { lc $_ => 1 } @ARGV;
14 0 ▶ $VERBOSE = $args{verbose} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args{'verbose'} ? : 0 0 0
15 0 ▶ my $config = $args{configuration} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$args{'configuration'} ? : 0 0 0
16  
17 {
18 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
19 0 ▶ $fh ||= *{"${package}::DATA"};
Condition0%0/3
$fh ||= *{"${package}::DATA";}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fh ||= *{"${package}::DATA";}
$fh *{"${package}::DATA";}
Truth table0%0/3
A ||= B
A= $fhB= *{"${package}::DATA";}
ABresult
1X1
011
000
20 }
21  
22 0 my $yaml = do { local $/; <$fh> };
23 0 my $data = YAML::Load($yaml);
24  
25 0 _set_table ($package, $data);
26 0 _set_columns ($package, $data);
27 0 _set_pk ($package, $data);
28 0 _set_unique ($package, $data);
29 0 _set_relationships ($package, $data);
30  
31 0 ▶ print_configuration($package) if $config;
Branch: 1 branch
BranchTrueFalseTotal
if $config 0 0 0
32 }
33  
34  
35 0 ▶sub _set_table {
Subroutine _set_table: not called CC=2 SCAR=17.9
36 0 my ($package, $data) = @_;
37 my $table = $data->{table}
38 0 ▶ or die "Cannot configure $package: table not specified.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $table = $data->{"table"} or die "Cannot configure ${package}: table not specified.\n" 0 0 0
39 0 $package->table($table);
40 }
41  
42 0 ▶sub _set_pk {
Subroutine _set_pk: not called CC=3 SCAR=24.8
43 0 my ($package, $data) = @_;
44 my $pk = $data->{primary_key}
45 0 ▶ or die "Cannot configure $package: primary key not specified.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $pk = $data->{"primary_key"} or die "Cannot configure ${package}: primary key not specified.\n" 0 0 0
46 0 ▶ $package->set_primary_key(ref $pk eq 'ARRAY' ? @$pk : $pk);
Branch: 1 branch
BranchTrueFalseTotal
ref $pk eq 'ARRAY' ? : 0 0 0
47 }
48  
49 0 ▶sub _set_columns {
Subroutine _set_columns: not called CC=2 SCAR=17.9
50 0 my ($package, $data) = @_;
51 die "Cannot configure $package: columns not properly specified.\n"
52 0 ▶ unless ref $data->{columns} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"columns"} eq "HASH" 0 0 0
53 0 my $columns = $data->{columns};
54 0 $package->add_columns(%{ $columns });
55 }
56  
57 0 ▶sub _set_unique {
Subroutine _set_unique: not called CC=4 SCAR=30.0
58 0 my ($package, $data) = @_;
59 0 ▶ return unless ref $data->{unique} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"unique"} eq "HASH" 0 0 0
60 0 for my $name (keys %{ $data->{unique} }) {
61 0 my $spec = $data->{unique}->{$name};
62 0 ▶ my $cols = ref $spec eq 'ARRAY' ? $spec : [ $spec ];
Branch: 1 branch
BranchTrueFalseTotal
ref $spec eq 'ARRAY' ? : 0 0 0
63 0 $package->add_unique_constraint($name => $cols);
64 }
65 }
66  
67 0 ▶sub _set_relationships {
Subroutine _set_relationships: not called CC=7 SCAR=40.3
68 0 my ($package, $data) = @_;
69 0 ▶ return unless ref $data->{relationships} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"relationships"} eq "ARRAY" 0 0 0
70 0 for my $relation (@{ $data->{relationships} }) {
71 0 my ($relation, $value) = each %{ $relation };
72 0 my ($type, $class, $condition, $attrs) = @{ $value };
73 0 ▶ $class = resolve_relative_class($class, $package)
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "many_to_many" 0 0 0
74 unless $type eq 'many_to_many';
75 0 $package->$type($relation, $class, $condition, $attrs);
76  
77 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 0 0
78 0 my %relationships = map { $_ => 1 } $package->relationships;
79 0 ▶ my $ok = exists $relationships{$relation} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $relationships{$relation} ? : 0 0 0
80 0 my $space_one = " " x (50 - length $package);
81 0 my $space_two = " " x (20 - length $relation);
82 0 ▶ print STDERR
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
83 ($ok ? "RELATION OK:" : "RELATION FAIL: "),
84 "$package $space_one $relation $space_two $type\n",
85 "\t$package->$type($relation, $class, $condition)\n\n";
86 }
87 }
88 }
89
90 0 ▶sub resolve_relative_class {
Subroutine resolve_relative_class: not called CC=2 SCAR=17.9
Pod undocumented
91 0 my ($class, $package) = @_;
92 0 ▶ return $class if $class =~ /::/; # fully qualified already.
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /::/ 0 0 0
93 0 (my $new_package = $package) =~ s/::[^:]+$/::$class/;
94 0 return $new_package;
95 }
96  
97 0 ▶sub print_configuration {
Subroutine print_configuration: not called CC=7 SCAR=40.3
Pod undocumented
98 0 my $package = shift;
99 0 local $\ = "\n";
100 0 print "Table: ", $package->table;
101 0 print "Primary Key: ", join ", " => $package->primary_columns;
102
103 0 print "Columns: ";
104 0 for my $col ($package->columns) {
105 0 my %info = %{ $package->column_info($col) };
106 0 print "\t$col:";
107 0 print "\t\t$_ => $info{$_}" for sort keys %info;
108 }
109
110 0 print "Relationships:";
111 0 for my $rel ($package->relationships) {
112 0 my %info = %{ $package->relationship_info($rel) };
113 0 print "\t$rel:";
114 0 for my $key (sort keys %info) {
115 0 ▶ if (ref $info{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info{$key}) { } 0 0 0
116 0 print "\t\t$key:";
117 0 print "\t\t\t$_ => $info{$key}->{$_}" for sort keys %{ $info{$key} };
118 }
119 else {
120 0 print "\t\t$key => $info{$key}";
121 }
122 }
123 }
124  
125 0 print "Constraints: ";
126 0 my %uniq = $package->unique_constraints;
127 0 map { print "\t$_ => ", join ", " => @{ $uniq{$_} } } keys %uniq;
128 }
129  
130  
131 1 1;
132 __END__
↑ Summary