1 package DBIx::Class::Schema;
6 use base qw/Class::Data::Inheritable/;
9 __PACKAGE__->mk_classdata('class_registrations' => {});
13 DBIx::Class::Schema - composable schemas
21 use base qw/DBIx::Class::Schema/;
23 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
27 package My::Schema::Foo;
29 use base qw/DBIx::Class::Core/;
31 __PACKAGE__->table('foo');
38 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
42 my @obj = My::DB::Foo->retrieve_all; # My::DB::Foo isa My::Schema::Foo My::DB
53 my ($class, $name, $to_register) = @_;
54 my %reg = %{$class->class_registrations};
55 $reg{$name} = $to_register;
56 $class->class_registrations(\%reg);
59 sub registered_classes {
60 return values %{shift->class_registrations};
65 my @comp = grep { $_ !~ /^#/ } @_;
66 foreach my $comp (@comp) {
67 my $comp_class = "${class}::${comp}";
68 eval "use $comp_class";
70 $class->register_class($comp => $comp_class);
74 sub compose_connection {
75 my ($class, $target, @info) = @_;
76 $class->setup_connection_class($target, @info);
77 my %reg = %{ $class->class_registrations };
78 while (my ($comp, $comp_class) = each %reg) {
79 my $target_class = "${target}::${comp}";
80 $class->inject_base($target_class, $comp_class, $target);
84 sub setup_connection_class {
85 my ($class, $target, @info) = @_;
86 $class->inject_base($target => 'DBIx::Class');
87 $target->load_components('DB');
88 $target->connection(@info);
92 my ($class, $target, @to_inject) = @_;
95 unshift(@{"${target}::ISA"}, @to_inject);
105 Matt S. Trout <perl-stuff@trout.me.uk>
109 You may distribute this code under the same terms as Perl itself.