File Coverage

blib/lib/DBIx/Class/Smooth/Helper/Row/Creation.pm
Criterion Covered Total %
statement 90 99 90.9
branch 9 18 50.0
condition n/a
subroutine 16 17 94.1
pod 1 6 16.6
total 116 140 82.8


line stmt bran cond sub pod time code
1 2     2   3118 use 5.20.0;
  2         26  
2 2     2   11 use strict;
  2         4  
  2         59  
3 2     2   9 use warnings;
  2         3  
  2         110  
4              
5             package DBIx::Class::Smooth::Helper::Row::Creation;
6              
7             # ABSTRACT: Short intro
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.0102';
10              
11 2     2   12 use parent 'DBIx::Class::Row';
  2         2  
  2         9  
12 2     2   136 use String::CamelCase;
  2         4  
  2         75  
13 2     2   763 use Module::Loader;
  2         22986  
  2         56  
14 2     2   775 use Syntax::Keyword::Try;
  2         1243  
  2         11  
15 2     2   120 use Carp qw/croak/;
  2         5  
  2         79  
16 2     2   11 use DBIx::Class::Candy::Exports;
  2         3  
  2         33  
17 2     2   1669 use DBIx::Class::Smooth::Helper::Util qw/result_source_to_class result_source_to_relation_name /;
  2         5  
  2         8  
18              
19 2     2   176 use experimental qw/postderef signatures/;
  2         5  
  2         7  
20              
21             export_methods [qw/
22             col
23             primary
24             foreign
25             belongs
26             unique
27             primary_belongs
28             ManyToMany
29             /];
30              
31             state $module_loader = Module::Loader->new;
32              
33 12     12 0 122 sub col($self, $name, $definition) {
  12         19  
  12         15  
  12         16  
  12         19  
34 12         80 $self->add_columns($name => $definition);
35             }
36              
37 6     6 1 3586 sub primary($self, $name, $definition) {
  6         13  
  6         9  
  6         9  
  6         8  
38 6         61 $self->add_columns($name => $definition);
39 6         2537 $self->set_primary_key($self->primary_columns, $name);
40             }
41 4     4 0 6 sub primary_belongs($self, @remaining) {
  4         7  
  4         8  
  4         7  
42 4         15 my $column_name = $self->belongs(@remaining);
43 4         121 $self->set_primary_key($self->primary_columns, $column_name);
44              
45             }
46 6     6 0 8 sub foreign($self, $column_name, $definition) {
  6         11  
  6         9  
  6         9  
  6         8  
47 6         11 $definition->{'is_foreign_key'} = 1;
48 6         55 $self->add_column($column_name => $definition);
49             }
50              
51             # assumes that the primary key is called 'id'
52 6     6 0 25 sub belongs($self, $other_source, $relation_name_or_definition, $definition_or_undef = {}) {
  6         10  
  6         7  
  6         9  
  6         9  
  6         11  
53 6         16 my $belongs_to_class = result_source_to_class($self, $other_source);
54 6         24 my $relation_name = result_source_to_relation_name($other_source);
55 6         14 my $definition = {};
56              
57             # two-param call
58 6 50       15 if(ref $relation_name_or_definition eq 'HASH') {
    0          
59 6         13 $definition = $relation_name_or_definition;
60             }
61             # three-param call
62             elsif(ref $definition_or_undef eq 'HASH') {
63 0         0 $definition = $definition_or_undef;
64 0         0 $relation_name = $relation_name_or_definition;
65             }
66             else {
67 0         0 croak "Bad call to belongs in $self: 'belongs $other_source ...'";
68             }
69 6         13 my $column_name = $relation_name . '_id';
70              
71              
72             # Its a ForeignKey field!
73 6 50       22 if(exists $definition->{'_smooth_foreign_key'}) {
74 6         11 delete $definition->{'_smooth_foreign_key'};
75 6         28 $module_loader->load($belongs_to_class);
76              
77 6         106 my $primary_key_col = undef;
78              
79             try {
80             $primary_key_col = $belongs_to_class->column_info('id');
81             }
82 6         15 catch {
83             croak "$belongs_to_class has no column 'id'";
84             }
85 6         1087 $definition->{'data_type'} = $primary_key_col->{'data_type'};
86 6         14 $definition->{'is_foreign_key'} = 1;
87              
88 6         11 for my $attr (qw/size is_numeric/) {
89 12 100       29 if(exists $primary_key_col->{ $attr }) {
90 6         12 $definition->{ $attr } = $primary_key_col->{ $attr };
91             }
92             }
93             }
94              
95 6 50       16 if(!exists $definition->{'data_type'}) {
96 0         0 croak qq{ResultSource '$self' column '$column_name' => definition is missing 'data_type'};
97             }
98 6 50       17 my $sql = exists $definition->{'sql'} ? delete $definition->{'sql'} : {};
99 6 50       23 my $related_name = exists $definition->{'related_name'} ? delete $definition->{'related_name'}
100             : result_source_to_relation_name($self, 1)
101             ;
102 6 50       20 my $related_sql = exists $definition->{'related_sql'} ? delete $definition->{'related_sql'} : {};
103              
104 6         33 $self->foreign($column_name => $definition);
105 6         2218 $self->belongs_to($relation_name, $belongs_to_class, { "foreign.id" => "self.$column_name" }, $sql);
106              
107 6 50       3527 if(defined $related_name) {
108 6         23 $module_loader->load($belongs_to_class);
109 6         273 $belongs_to_class->has_many($related_name, $self, { "foreign.$column_name" => "self.id" }, $related_sql);
110             }
111              
112 6         2638 return $column_name;
113              
114             }
115              
116             sub unique {
117 0     0 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             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.0102, released 2019-12-22.
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