Add automatic naming of unique constraints
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Componentised.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 DBIx::Class::Componentised;
227d4dee 3
bf5ecff9 4use strict;
5use warnings;
6
147dd158 7use Class::C3;
c037c03a 8use Class::Inspector;
147dd158 9
227d4dee 10sub inject_base {
11 my ($class, $target, @to_inject) = @_;
12 {
13 no strict 'refs';
eb47985e 14 foreach my $to (reverse @to_inject) {
15 unshift( @{"${target}::ISA"}, $to )
16 unless ($target eq $to || $target->isa($to));
17 }
227d4dee 18 }
20518cb4 19
20 # Yes, this is hack. But it *does* work. Please don't submit tickets about
21 # it on the basis of the comments in Class::C3, the author was on #dbix-class
22 # while I was implementing this.
23
9a15732e 24 my $table = { Class::C3::_dump_MRO_table };
75a23b3e 25 eval "package $target; import Class::C3;" unless exists $table->{$target};
227d4dee 26}
27
28sub load_components {
29 my $class = shift;
7411204b 30 my $base = $class->component_base_class;
6db94aca 31 my @comp = map { /^\+(.*)$/ ? $1 : "${base}::$_" } grep { $_ !~ /^#/ } @_;
227d4dee 32 $class->_load_components(@comp);
971c0085 33 Class::C3::reinitialize();
227d4dee 34}
35
36sub load_own_components {
37 my $class = shift;
38 my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
39 $class->_load_components(@comp);
40}
41
42sub _load_components {
43 my ($class, @comp) = @_;
44 foreach my $comp (@comp) {
c037c03a 45 $class->ensure_class_loaded($comp);
227d4dee 46 }
47 $class->inject_base($class => @comp);
48}
49
c037c03a 50# TODO: handle ->has_many('rel', 'Class'...) instead of
51# ->has_many('rel', 'Some::Schema::Class'...)
52sub ensure_class_loaded {
53 my ($class, $f_class) = @_;
54 eval "require $f_class";
55 my $err = $@;
56 Class::Inspector->loaded($f_class)
57 or die $err || "require $f_class was successful but the package".
58 "is not defined";
59}
60
227d4dee 611;