blib/lib/DBIx/QuickORM/ORM.pm

filter: Statementstmt 100% 92 / 92 Branchbran 100% 30 / 30 Conditioncond 75.0% 15 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 93.2% 153 / 164 CC 34 SCAR 22.3
CC
34
Cov
96%
_table_classes
13.0
_merge
12.0
new
7.0
CRAP 9.3
« blib/lib/DBIx/QuickORM/LookupTable.pm ↑ Summary blib/lib/DBIx/QuickORM/Handle.pm »
1 package DBIx::QuickORM::ORM;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 our $VERSION = '0.000032';
6  
7 17 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use DBIx::QuickORM::DSL();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 ▶use parent 'DBIx::QuickORM';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =pod
14  
15 =encoding UTF-8
16  
17 =head1 NAME
18  
19 DBIx::QuickORM::ORM - Base class for an ORM defined as classes.
20  
21 =head1 DESCRIPTION
22  
23 The C<orm> block of L<DBIx::QuickORM::DSL> makes its package a subclass of
24 this one. C<new> gathers what the C<orm> block and the table classes collected
25 into arguments for L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">,
26 then constructs the ORM with them.
27  
28 The table classes are the modules directly under the ORM class's namespace
29 (C<My::ORM::*> for C<My::ORM>), found in C<@INC>. Every module there is
30 loaded, and those with a C<table> block are used. The C<tables> keyword
31 replaces that search with another namespace or a list of classes. A table
32 that needs nothing adjusted needs no class.
33  
34 =head1 SYNOPSIS
35  
36 package My::ORM;
37 use DBIx::QuickORM::DSL ':orm';
38  
39 orm {
40 retain_cache 1;
41 };
42  
43 package main;
44  
45 my $orm = My::ORM->new(
46 dbh => $dbh,
47 types => {users => {prefs => undef}},
48 );
49  
50 =head1 PUBLIC METHODS
51  
52 =over 4
53  
54 =item $orm = My::ORM->new(%args)
55  
56 =item $orm = My::ORM->new(\%args)
57  
58 Constructs the ORM from the collected specs and C<%args>, which takes the
59 arguments of L<DBIx::QuickORM's C<new>|DBIx::QuickORM/"PUBLIC METHODS">.
60  
61 Precedence, lowest to highest: the C<orm> block's keywords, its C<override>,
62 the table classes, then C<%args>. C<time_zone> and its alias C<tz> count as
63 one argument, so either spelling in C<%args> beats either in C<override>.
64  
65 C<types> merge per field and C<fks> per target table: a higher source's
66 entry for a field (or target table) replaces the lower one's, and the rest
67 stay.
68  
69 C<omit>, C<default_fields>, and C<row_classes> apply per table: a higher
70 source's entry for a table replaces the lower one's entry for that table
71 whole. C<omit> and C<default_fields> count as one setting there, so a higher
72 source's C<omit> for a table also drops a lower one's C<default_fields> for
73 it, and the other way around.
74  
75 Croaks when:
76  
77 =over 4
78  
79 =item *
80  
81 The class has no C<orm> block of its own. Specs are not inherited, so a
82 subclass of an ORM class needs its own.
83  
84 =item *
85  
86 Two table classes name one table.
87  
88 =item *
89  
90 A table class names a table the database does not have.
91  
92 =item *
93  
94 C<tables> lists a class without a C<table> block.
95  
96 =back
97  
98 =back
99  
100 =cut
101  
102 17 my %TABLE_ARG = (
103 omit => 'omit',
104 default_fields => 'default_fields',
105 types => 'types',
106 fks => 'fks',
107 row_class => 'row_classes',
108 );
109  
110 17 my %PER_TABLE = map { $_ => 1 } values %TABLE_ARG;
111 17 my %PER_ENTRY = (types => 1, fks => 1);
112  
113 29 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
114 29 my $class = shift;
115 29 ▶ my %args = @_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 1 28 29
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2801
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
116  
117 29 ▶ my $spec = DBIx::QuickORM::DSL->orm_spec($class) or croak "$class has no orm block; DBIx::QuickORM::ORM specs are not inherited";
Branch: 1 branch
BranchTrueFalseTotal
my $spec = "DBIx::QuickORM::DSL"->orm_spec($class) or croak("$class has no orm block; DBIx::QuickORM::ORM specs are not inherited") 28 1 29
118  
119 28 my %orm = %$spec;
120 28 my $tables = delete $orm{tables};
121 28 ▶ my $override = delete($orm{override}) // {};
Condition100%2/2
delete $orm{"override"} // {}
l!l
1612
MC/DC100%1/1
delete $orm{"override"} // {}
delete $orm{"override"}
Truth table100%2/2
A
A= delete $orm{"override"}
Aresult
11
00
122  
123 28 my ($from_tables, $claimed) = $class->_table_args($tables);
124 24 my $self = $class->SUPER::new($class->_merge({%orm, %$override}, $from_tables, \%args), caller => [caller]);
125  
126 21 for my $table (sort keys %$claimed) {
127 20 ▶ croak "$claimed->{$table} names the table '$table', which is not in the database map" unless $self->db_map->table($table);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->db_map->table($table) 1 19 20
128 }
129  
130 20 return $self;
131 }
132  
133 =pod
134  
135 =head1 PRIVATE METHODS
136  
137 =over 4
138  
139 =item (\%args, \%claimed) = $class->_table_args($tables)
140  
141 The table classes' C<new> arguments, and which class named each table.
142  
143 =back
144  
145 =cut
146  
147 28 ▶sub _table_args {
Subroutine _table_args: called CC=5 SCAR=16.1
148 28 my $class = shift;
149 28 my ($tables) = @_;
150  
151 28 my (%args, %claimed);
152 28 for my $table_class ($class->_table_classes($tables)) {
153 24 my $spec = DBIx::QuickORM::DSL->table_spec($table_class);
154 24 my $name = $spec->{name};
155  
156 24 ▶ croak "Both $claimed{$name} and $table_class name the table '$name'" if $claimed{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $claimed{$name} 1 23 24
157 23 $claimed{$name} = $table_class;
158  
159 23 for my $key (keys %TABLE_ARG) {
160 115 ▶ $args{$TABLE_ARG{$key}}->{$name} = $spec->{$key} if exists $spec->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{$key} 45 70 115
161 }
162 }
163  
164 24 return (\%args, \%claimed);
165 }
166  
167 =pod
168  
169 =over 4
170  
171 =item @classes = $class->_table_classes($tables)
172  
173 The table classes: those listed, or those found under a namespace (the class's
174 own unless C<$tables> names another). Loads each class it looks at.
175  
176 =back
177  
178 =cut
179  
180 28 ▶sub _table_classes {
Subroutine _table_classes: called CC=13 SCAR=25.7
181 28 my $class = shift;
182 28 my ($tables) = @_;
183  
184 28 ▶ if (ref($tables) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tables eq "ARRAY") 9 19 28
185 9 for my $table_class (@$tables) {
186 10 ▶ $class->load_class("'tables' lists $table_class, which", $table_class) unless DBIx::QuickORM::DSL->table_spec($table_class);
Branch: 1 branch
BranchTrueFalseTotal
unless "DBIx::QuickORM::DSL"->table_spec($table_class) 2 8 10
187 9 ▶ croak "'tables' lists $table_class, which has no 'table' block" unless DBIx::QuickORM::DSL->table_spec($table_class);
Branch: 1 branch
BranchTrueFalseTotal
unless "DBIx::QuickORM::DSL"->table_spec($table_class) 1 8 9
188 }
189 7 return @$tables;
190 }
191  
192 19 ▶ my $namespace = $tables // $class;
Condition66%2/3
$tables // $class
l!l&&r!l&&!r
4150
MC/DC0%0/2
$tables // $class
$tables $class
Truth table66%2/3
A // B
A= $tablesB= $class
ABresult
1X1
011
000
193 19 ▶ croak "'tables' must be a namespace or an arrayref of class names" if ref($namespace) || !length($namespace);
Branch: 1 branch
BranchTrueFalseTotal
if ref $namespace or not length $namespace 1 18 19
Condition66%2/3
ref $namespace or not length $namespace
l!l&&r!l&&!r
1018
MC/DC50%1/2
ref $namespace or not length $namespace
ref $namespace not length $namespace
Truth table66%2/3
A or not(B)
A= ref $namespaceB= not length $namespace
ABresult
1X1
011
000
194  
195 18 (my $dir = $namespace) =~ s{::}{/}g;
196 18 my %found;
197 18 for my $inc (grep { !ref($_) } @INC) {
198 146 ▶ opendir(my $dh, "$inc/$dir") or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, "$inc/$dir" or next 9 137 146
199 9 ▶ $found{"${namespace}::$_"} = 1 for map { m/^(\w+)\.pm$/ ? $1 : () } readdir($dh);
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+)\.pm$/ ? : 23 18 41
200 }
201  
202 18 my @classes = sort keys %found;
203 18 $class->load_class("The table class '$_'", $_) for @classes;
204 18 return grep { DBIx::QuickORM::DSL->table_spec($_) } @classes;
205 }
206  
207 =pod
208  
209 =over 4
210  
211 =item %args = $class->_merge(\%lowest, ..., \%highest)
212  
213 Merges argument sets, a higher one replacing a lower one's argument, or for
214 the per-table arguments its entry for each table it names. C<types> and
215 C<fks> go one level deeper: per field and per target table. A table named in a
216 higher set's C<omit> or C<default_fields> loses its entries in both from the
217 lower sets. C<time_zone> and its alias C<tz> count as one argument.
218  
219 =back
220  
221 =cut
222  
223 24 ▶sub _merge {
Subroutine _merge: called CC=12 SCAR=24.9
224 24 my $class = shift;
225  
226 24 my %merged;
227 24 for my $args (@_) {
228 72 ▶ delete @merged{qw/time_zone tz/} if grep { exists $args->{$_} } qw/time_zone tz/;
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $args->{$_};} "time_zone", "tz" 9 63 72
229  
230 72 ▶ my @named = map { ref($args->{$_}) eq 'HASH' ? keys %{$args->{$_}} : () } qw/omit default_fields/;
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{$_} eq 'HASH' ? : 19 125 144
231 72 for my $key (grep { ref($merged{$_}) eq 'HASH' } qw/omit default_fields/) {
232 17 my %kept = %{$merged{$key}};
233 17 delete @kept{@named};
234 17 $merged{$key} = \%kept;
235 }
236  
237 72 for my $key (keys %$args) {
238 110 my ($have, $new) = ($merged{$key}, $args->{$key});
239 110 ▶ unless ($PER_TABLE{$key} && ref($have) eq 'HASH' && ref($new) eq 'HASH') { $merged{$key} = $new; next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($PER_TABLE{$key} and ref $have eq "HASH" and ref $new eq "HASH") 97 13 110
Condition83%5/6
$PER_TABLE{$key} and ref $have eq "HASH" and ref $new eq "HASH"
!ll&&!rl&&r
97013
$PER_TABLE{$key} and ref $have eq "HASH"
!ll&&!rl&&r
574013
MC/DC66%2/3
$PER_TABLE{$key} and ref $have eq "HASH" and ref $new eq "HASH"
$PER_TABLE{$key} ref $have eq "HASH" ref $new eq "HASH"
Truth table75%3/4
A and B and C
A= $PER_TABLE{$key}B= ref $have eq "HASH"C= ref $new eq "HASH"
ABCresult
0XX0
10X0
1100
1111
240  
241 13 my %both = (%$have, %$new);
242 13 ▶ if ($PER_ENTRY{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PER_ENTRY{$key}) 4 9 13
243 4 ▶ for my $table (grep { ref($have->{$_}) eq 'HASH' && ref($new->{$_}) eq 'HASH' } keys %$new) {
Condition66%2/3
ref $have->{$_} eq 'HASH' && ref $new->{$_} eq 'HASH'
!ll&&!rl&&r
103
MC/DC50%1/2
ref $have->{$_} eq 'HASH' && ref $new->{$_} eq 'HASH'
ref $have->{$_} eq 'HASH' ref $new->{$_} eq 'HASH'
Truth table66%2/3
A && B
A= ref $have->{$_} eq 'HASH'B= ref $new->{$_} eq 'HASH'
ABresult
0X0
100
111
244 3 $both{$table} = {%{$have->{$table}}, %{$new->{$table}}};
245 }
246 }
247 13 $merged{$key} = \%both;
248 }
249 }
250  
251 24 return %merged;
252 }
253  
254 17 1;
255  
256 __END__
« blib/lib/DBIx/QuickORM/LookupTable.pm ↑ Summary blib/lib/DBIx/QuickORM/Handle.pm »