Smooth/Helper/Row/Creation.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 3 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DBIx::Class::Smooth::Helper::Row::Creation;
6  
7 # ABSTRACT: Short intro
8 3 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 3 our $VERSION = '0.0104';
10  
11 3 ▶use parent 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use String::CamelCase;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Module::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use DBIx::Class::Candy::Exports;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶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 3 ▶use experimental qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 export_methods [qw/
23 col
24 primary
25 foreign
26 belongs
27 unique
28 primary_belongs
29 /];
30  
31 3 state $module_loader = Module::Loader->new;
32  
33 ▶sub col($self, $name, $definition) {
Subroutine col: called CC=1 SCAR=0.0
Pod undocumented
34 12 $self->add_columns($name => $definition);
35 }
36  
37 ▶sub primary($self, $name, $definition) {
Subroutine primary: called CC=1 SCAR=0.0
Pod documented
38 6 $self->add_columns($name => $definition);
39 6 $self->set_primary_key($self->primary_columns, $name);
40 }
41 ▶sub primary_belongs($self, @remaining) {
Subroutine primary_belongs: called CC=1 SCAR=0.0
Pod undocumented
42 4 my $column_name = $self->belongs(@remaining);
43 4 $self->set_primary_key($self->primary_columns, $column_name);
44  
45 }
46 ▶sub foreign($self, $column_name, $definition) {
Subroutine foreign: called CC=1 SCAR=0.0
Pod undocumented
47 6 $definition->{'is_foreign_key'} = 1;
48 6 $self->add_column($column_name => $definition);
49 }
50  
51 # assumes that the primary key is called 'id'
52 6 ▶sub belongs($self, $other_source, $relation_name_or_definition, $definition_or_undef = {}) {
Subroutine belongs: called CC=12 SCAR=27.1
Pod undocumented
53 6 my $belongs_to_class = result_source_to_class($self, $other_source);
54 6 my $relation_name = result_source_to_relation_name($other_source);
55 6 my $definition = {};
56  
57 # two-param call
58 6 ▶ if(ref $relation_name_or_definition eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $relation_name_or_definition eq 'HASH') { } 6 0 6
elsif (ref $definition_or_undef eq 'HASH') { } 0 0 0
59 6 $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 6 my $column_name = $relation_name . '_id';
70  
71  
72 # Its a ForeignKey field!
73 6 ▶ if(exists $definition->{'_smooth_foreign_key'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $definition->{"_smooth_foreign_key"}) 6 0 6
74 6 delete $definition->{'_smooth_foreign_key'};
75 6 $module_loader->load($belongs_to_class);
76  
77 6 my $primary_key_col = undef;
78  
79 try {
80 6 $primary_key_col = $belongs_to_class->column_info('id');
81 }
82 6 catch {
83 0 croak "$belongs_to_class has no column 'id'";
84 }
85 6 $definition->{'data_type'} = $primary_key_col->{'data_type'};
86 6 $definition->{'is_foreign_key'} = 1;
87  
88 6 for my $attr (qw/size is_numeric/) {
89 12 ▶ if(exists $primary_key_col->{ $attr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $primary_key_col->{$attr}) 6 6 12
90 6 $definition->{ $attr } = $primary_key_col->{ $attr };
91 }
92 }
93 }
94  
95 6 ▶ if(!exists $definition->{'data_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $definition->{"data_type"}) 0 6 6
96 0 croak qq{ResultSource '$self' column '$column_name' => definition is missing 'data_type'};
97 }
98 6 ▶ my $sql = exists $definition->{'sql'} ? delete $definition->{'sql'} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $definition->{'sql'} ? : 0 6 6
99 6 ▶ my $related_name = exists $definition->{'related_name'} ? delete $definition->{'related_name'}
Branch: 1 branch
BranchTrueFalseTotal
exists $definition->{'related_name'} ? : 0 6 6
100 : result_source_to_relation_name($self, 1)
101 ;
102 6 ▶ my $related_sql = exists $definition->{'related_sql'} ? delete $definition->{'related_sql'} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $definition->{'related_sql'} ? : 0 6 6
103  
104 6 $self->foreign($column_name => $definition);
105 6 $self->belongs_to($relation_name, $belongs_to_class, { "foreign.id" => "self.$column_name" }, $sql);
106  
107 6 ▶ if(defined $related_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $related_name) 6 0 6
108 6 $module_loader->load($belongs_to_class);
109 6 $belongs_to_class->has_many($related_name, $self, { "foreign.$column_name" => "self.id" }, $related_sql);
110 }
111  
112 6 return $column_name;
113  
114 }
115  
116 sub unique {
117 0 ▶ my $self = shift;
Subroutine unique: not called CC=1 SCAR=6.9
Pod undocumented
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 3 1;
126  
127 __END__
128  
129 =pod
130  
131 =encoding UTF-8
132  
133 =head1 NAME
134  
135 DBIx::Class::Smooth::Helper::Row::Creation - Short intro
136  
137 =head1 VERSION
138  
139 Version 0.0104, released 2020-08-30.
140  
141 =head1 SOURCE
142  
143 L<https://github.com/Csson/p5-DBIx-Class-Smooth>
144  
145 =head1 HOMEPAGE
146  
147 L<https://metacpan.org/release/DBIx-Class-Smooth>
148  
149 =head1 AUTHOR
150  
151 Erik Carlsson <info@code301.com>
152  
153 =head1 COPYRIGHT AND LICENSE
154  
155 This software is copyright (c) 2018 by Erik Carlsson.
156  
157 This is free software; you can redistribute it and/or modify it under
158 the same terms as the Perl 5 programming language system itself.
159  
160 =cut
« Smooth/Lookup/substring.pm ↑ Summary Smooth/Q.pm »