Smooth/Helper/Row/Definition.pm

filter: Statementstmt 87.8% 72 / 82 Branchbran 50.0% 9 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.1% 16 / 17 Podpod 16.6% 1 / 6 totaltotal 79.6% 98 / 123 CC 12 SCAR 19.5
CC
12
Cov
81%
belongs
15.0
unique
2.0
BEGIN
1.0
CRAP 7.0
« Smooth/Lookup/substring.pm ↑ Summary Smooth/Q.pm »
1 2 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DBIx::Class::Smooth::Helper::Row::Definition;
6  
7 # ABSTRACT: Short intro
8 2 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 2 our $VERSION = '0.0108';
10  
11 2 ▶use parent 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use String::CamelCase;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Module::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use DBIx::Class::Candy::Exports;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use DBIx::Class::Smooth::Helper::Util qw/result_source_to_class result_source_to_relation_name /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use experimental qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 export_methods [qw/
23 col
24 primary
25 foreign
26 belongs
27 unique
28 primary_belongs
29 /];
30  
31 2 state $module_loader = Module::Loader->new;
32  
33 9 ▶sub col($self, $name, $definition) {
Subroutine col: called CC=1 SCAR=0.0
Pod undocumented
34 9 $self->add_columns($name => $definition);
35 }
36  
37 5 ▶sub primary($self, $name, $definition) {
Subroutine primary: called CC=1 SCAR=0.0
Pod documented
38 5 $self->add_columns($name => $definition);
39 5 $self->set_primary_key($self->primary_columns, $name);
40 }
41 2 ▶sub primary_belongs($self, @remaining) {
Subroutine primary_belongs: called CC=1 SCAR=0.0
Pod undocumented
42 2 my $column_name = $self->belongs(@remaining);
43 2 $self->set_primary_key($self->primary_columns, $column_name);
44  
45 }
46 5 ▶sub foreign($self, $column_name, $definition) {
Subroutine foreign: called CC=1 SCAR=0.0
Pod undocumented
47 5 $definition->{'is_foreign_key'} = 1;
48 5 $self->add_column($column_name => $definition);
49 }
50  
51 # assumes that the primary key is called 'id'
52 5 ▶sub belongs($self, $other_source, $relation_name_or_definition, $definition_or_undef = {}) {
Subroutine belongs: called CC=12 SCAR=27.1
Pod undocumented
53 5 my $belongs_to_class = result_source_to_class($self, $other_source);
54 5 my $relation_name = result_source_to_relation_name($other_source);
55 5 my $definition = {};
56  
57 # two-param call
58 5 ▶ if(ref $relation_name_or_definition eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $relation_name_or_definition eq 'HASH') { } 5 0 5
elsif (ref $definition_or_undef eq 'HASH') { } 0 0 0
59 5 $definition = $relation_name_or_definition;
60 }
61 # three-param call
62 elsif(ref $definition_or_undef eq 'HASH') {
63 0 $definition = $definition_or_undef;
64 0 $relation_name = $relation_name_or_definition;
65 }
66 else {
67 0 croak "Bad call to belongs in $self: 'belongs $other_source ...'";
68 }
69 5 my $column_name = $relation_name . '_id';
70  
71  
72 # Its a ForeignKey field!
73 5 ▶ if(exists $definition->{'_smooth_foreign_key'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $definition->{"_smooth_foreign_key"}) 5 0 5
74 5 delete $definition->{'_smooth_foreign_key'};
75 5 $module_loader->load($belongs_to_class);
76  
77 5 my $primary_key_col = undef;
78  
79 try {
80 5 $primary_key_col = $belongs_to_class->column_info('id');
81 }
82 5 catch {
83 0 croak "$belongs_to_class has no column 'id'";
84 }
85 5 $definition->{'data_type'} = $primary_key_col->{'data_type'};
86 5 $definition->{'is_foreign_key'} = 1;
87  
88 5 for my $attr (qw/size is_numeric/) {
89 10 ▶ if(exists $primary_key_col->{ $attr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $primary_key_col->{$attr}) 5 5 10
90 5 $definition->{ $attr } = $primary_key_col->{ $attr };
91 }
92 }
93 }
94  
95 5 ▶ if(!exists $definition->{'data_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $definition->{"data_type"}) 0 5 5
96 0 croak qq{ResultSource '$self' column '$column_name' => definition is missing 'data_type'};
97 }
98 5 ▶ my $sql = exists $definition->{'sql'} ? delete $definition->{'sql'} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $definition->{'sql'} ? : 0 5 5
99 5 ▶ my $related_name = exists $definition->{'related_name'} ? delete $definition->{'related_name'}
Branch: 1 branch
BranchTrueFalseTotal
exists $definition->{'related_name'} ? : 0 5 5
100 : result_source_to_relation_name($self, 1)
101 ;
102 5 ▶ my $related_sql = exists $definition->{'related_sql'} ? delete $definition->{'related_sql'} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $definition->{'related_sql'} ? : 0 5 5
103  
104 5 $self->foreign($column_name => $definition);
105 5 $self->belongs_to($relation_name, $belongs_to_class, { "foreign.id" => "self.$column_name" }, $sql);
106  
107 5 ▶ if(defined $related_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $related_name) 5 0 5
108 5 $module_loader->load($belongs_to_class);
109 5 $belongs_to_class->has_many($related_name, $self, { "foreign.$column_name" => "self.id" }, $related_sql);
110 }
111  
112 5 return $column_name;
113  
114 }
115  
116 0 ▶sub unique {
Subroutine unique: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my $self = shift;
118 0 my $column_name = shift;
119 0 my $args = shift;
120  
121 0 $self->add_columns($column_name => $args);
122 0 $self->add_unique_constraint([ $column_name ]);
123 }
124  
125 2 1;
126  
127 __END__
« Smooth/Lookup/substring.pm ↑ Summary Smooth/Q.pm »