File Coverage

blib/lib/DBIx/QuickORM/Schema/Autofill.pm
Criterion Covered Total %
statement 76 81 93.8
branch 26 34 76.4
condition 8 14 57.1
subroutine 13 14 92.8
pod 0 5 0.0
total 123 148 83.1


line stmt bran cond sub pod time code
1             package DBIx::QuickORM::Schema::Autofill;
2 377     377   2519 use strict;
  377         724  
  377         15348  
3 377     377   1871 use warnings;
  377         694  
  377         28067  
4              
5             our $VERSION = '0.000019';
6              
7 377     377   3282 use List::Util qw/first/;
  377         829  
  377         33962  
8 377     377   820620 use DBIx::QuickORM::Util qw/load_class/;
  377         1493  
  377         3951  
9              
10 377         3351 use DBIx::QuickORM::Util::HashBase qw{
11             <types
12             <affinities
13             <hooks
14             <autorow
15             +skip
16 377     377   309783 };
  377         1373  
17              
18             my %HOOKS = (
19             column => 1,
20             columns => 1,
21             index => 1,
22             indexes => 1,
23             links => 1,
24             post_column => 1,
25             post_table => 1,
26             pre_column => 1,
27             pre_table => 1,
28             primary_key => 1,
29             table => 1,
30             unique_keys => 1,
31             link_accessor => 1,
32             field_accessor => 1,
33             );
34              
35 6 50   6 0 54 sub is_valid_hook { $HOOKS{$_[-1]} ? 1 : 0 }
36              
37             sub hook {
38 622     622 0 5233 my $self = shift;
39 622         1500 my ($hook, $args, $seed) = @_;
40 622         994 my $out = $seed;
41 622   100     958 $out = $_->(%$args, autofill => $self) for @{$self->{+HOOKS}->{$hook} // []};
  622         3244  
42 622         6420 return $out;
43             }
44              
45             sub skip {
46 0     0 0 0 my $self = shift;
47              
48 0         0 my $from = $self->{+SKIP};
49 0         0 while(my $arg = shift @_) {
50 0 0       0 $from = $from->{$arg} or return 0;
51             }
52 0         0 return $from;
53             }
54              
55             sub process_column {
56 87     87 0 196 my $self = shift;
57 87         205 my ($col) = @_;
58              
59 87         188 my $type = $col->{type};
60 87         213 my $tref = ref($type);
61 87 50 33     631 return unless $tref && $tref eq 'SCALAR';
62              
63 87         184 my $new_type;
64 87   33     1019 $new_type = $self->{+TYPES}->{$$type} // $self->{+TYPES}->{uc($$type)} // $self->{+TYPES}->{lc($$type)};
      66        
65              
66 87 100       254 unless ($new_type) {
67 82 50       327 if (my $aff = $col->{affinity}) {
68 82 100       413 if (my $list = $self->{+AFFINITIES}->{$aff}) {
69 17         101 for my $cb (@$list) {
70 17 100       3137 $new_type = $cb->(%$col) and last;
71             }
72             }
73             }
74             }
75              
76 87 100       3490 return unless $new_type;
77              
78 6         23 $col->{type} = $new_type;
79 6         101 $col->{affinity} = $new_type->qorm_affinity(sql_type => $$type);
80             }
81              
82             sub define_autorow {
83 10     10 0 22 my $self = shift;
84 10         27 my ($row_class, $table) = @_;
85              
86 10 100       38 unless(load_class($row_class)) {
87 3         11 my $err = $@;
88 3 50       41 die $@ unless $@ =~ m/Can't locate.*in \@INC/;
89 3         7 my $row_file = $row_class;
90 3         16 $row_file =~ s{::}{/}g;
91 3         8 $row_file .= ".pm";
92 3         13 $INC{$row_file} = __FILE__;
93             }
94              
95 10         65 for my $column ($table->columns) {
96 30         108 my $field = $column->name;
97 30         140 my $accessor = $self->hook(field_accessor => {table => $table, name => $field, field => $field, column => $column}, $field);
98 30 50       94 next unless $accessor;
99              
100 377     377   3421 no strict 'refs';
  377         1130  
  377         96200  
101 30 100       45 next if defined &{"$row_class\::$accessor"};
  30         183  
102 15     10   185 *{"$row_class\::$accessor"} = sub { shift->field($field, @_) };
  15         93  
  10         166  
103             }
104              
105 10         21 for my $link (@{$table->links}) {
  10         77  
106 16         50 my $to = $link->other_table;
107 16         36 my $aliases = $link->aliases;
108              
109 16 100 66     66 unless ($aliases && @$aliases) {
110 4 100       15 $aliases = [$link->unique ? $to : "${to}s" ];
111             }
112              
113 16         53 for my $alias (@$aliases) {
114 16         90 my $accessor = $self->hook(link_accessor => {table => $table, linked_table => $link->other_table, name => $alias, link => $link}, $alias);
115 16 50       55 next unless $accessor;
116 377     377   3090 no strict 'refs';
  377         829  
  377         94642  
117 16 100       23 next if defined &{"$row_class\::$accessor"};
  16         123  
118 8 100   1   92 *{"$row_class\::$accessor"} = $link->unique ? sub { shift->obtain($link) } : sub { shift->follow($link) };
  8         61  
  1         20  
  2         30  
119             }
120             }
121             }
122              
123             1;
124              
125             __END__
126              
127