converted tabs to spaces, removed trailing whitespace
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Componentised.pm
1 package # hide from PAUSE
2     DBIx::Class::Componentised;
3
4 use strict;
5 use warnings;
6
7 use Class::C3;
8
9 sub inject_base {
10   my ($class, $target, @to_inject) = @_;
11   {
12     no strict 'refs';
13     unshift(@{"${target}::ISA"}, grep { $target ne $_ && !$target->isa($_)} @to_inject);
14   }
15
16   # Yes, this is hack. But it *does* work. Please don't submit tickets about
17   # it on the basis of the comments in Class::C3, the author was on #dbix-class
18   # while I was implementing this.
19
20   my $table = { Class::C3::_dump_MRO_table };
21   eval "package $target; import Class::C3;" unless exists $table->{$target};
22 }
23
24 sub load_components {
25   my $class = shift;
26   my $base = $class->component_base_class;
27   my @comp = map { /^\+(.*)$/ ? $1 : "${base}::$_" } grep { $_ !~ /^#/ } @_;
28   $class->_load_components(@comp);
29   Class::C3::reinitialize();
30 }
31
32 sub load_own_components {
33   my $class = shift;
34   my @comp = map { "${class}::$_" } grep { $_ !~ /^#/ } @_;
35   $class->_load_components(@comp);
36 }
37
38 sub _load_components {
39   my ($class, @comp) = @_;
40   foreach my $comp (@comp) {
41     eval "use $comp";
42     die $@ if $@;
43   }
44   $class->inject_base($class => @comp);
45 }
46
47 1;