improved docs.
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Schema.pm
1 package DBIx::Class::Schema;
2
3 use strict;
4 use warnings;
5
6 use base qw/Class::Data::Inheritable/;
7 use base qw/DBIx::Class/;
8
9 __PACKAGE__->load_components(qw/Exception Componentised/);
10 __PACKAGE__->mk_classdata('class_registrations' => {});
11
12 =head1 NAME
13
14 DBIx::Class::Schema - composable schemas
15
16 =head1 SYNOPSIS
17
18   in My/Schema.pm
19
20   package My::Schema;
21
22   use base qw/DBIx::Class::Schema/;
23
24   __PACKAGE__->load_classes(qw/Foo Bar Baz/);
25
26   in My/Schema/Foo.pm
27
28   package My::Schema::Foo;
29
30   use base qw/DBIx::Class::Core/;
31
32   __PACKAGE__->table('foo');
33   ...
34
35   in My/DB.pm
36
37   use My::Schema;
38
39   My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
40
41   then in app code
42
43   my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
44
45 =head1 DESCRIPTION
46
47 =head1 METHODS
48
49 =over 4
50
51 =item register_class <component> <component_class>
52
53 Registers the class in the schema's class_registrations. This is a hash
54 containing components, and their representative classes. It's used by
55 compose_connection to create/modify all the existing database classes.
56
57 =cut
58
59 sub register_class {
60   my ($class, $name, $to_register) = @_;
61   my %reg = %{$class->class_registrations};
62   $reg{$name} = $to_register;
63   $class->class_registrations(\%reg);
64 }
65
66 =item registered_classes
67
68 Simple read-only accessor for the schema's registered classes. See 
69 register_class above if you want to modify it.
70
71
72 =cut
73
74 sub registered_classes {
75   return values %{shift->class_registrations};
76 }
77
78 =item  load_classes [<classes>}
79
80 Uses L<Module::Find> to find all components, unless specified explicitly.
81 Then it loads the component (using L<use>), and registers them (using 
82 B<register_class>
83
84 =cut
85
86 sub load_classes {
87   my $class = shift;
88   my @comp = grep { $_ !~ /^#/ } @_;
89   unless (@comp) {
90     eval "require Module::Find;";
91     $class->throw("No arguments to load_classes and couldn't load".
92       " Module::Find ($@)") if $@;
93     @comp = map { substr $_, length "${class}::"  }
94               Module::Find::findallmod($class);
95   }
96   foreach my $comp (@comp) {
97     my $comp_class = "${class}::${comp}";
98     eval "use $comp_class"; # If it fails, assume the user fixed it
99     $class->register_class($comp => $comp_class);
100   }
101 }
102
103 =item compose_connection
104
105 =cut
106
107 sub compose_connection {
108   my ($class, $target, @info) = @_;
109   my $conn_class = "${target}::_db";
110   $class->setup_connection_class($conn_class, @info);
111   my %reg = %{ $class->class_registrations };
112   my %target;
113   my %map;
114   while (my ($comp, $comp_class) = each %reg) {
115     my $target_class = "${target}::${comp}";
116     $class->inject_base($target_class, $conn_class, $comp_class);
117     $target_class->table($comp_class->table);
118     @map{$comp, $comp_class} = ($target_class, $target_class);
119   }
120   {
121     no strict 'refs';
122     *{"${target}::class"} =
123       sub {
124         my ($class, $to_map) = @_;
125         return $map{$to_map};
126       };
127     *{"${target}::classes"} = sub { return \%map; };
128   }
129   $conn_class->class_resolver($target);
130 }
131
132 =item setup_connection_class <$target> <@info>
133
134 =cut
135
136 sub setup_connection_class {
137   my ($class, $target, @info) = @_;
138   $class->inject_base($target => 'DBIx::Class');
139   $target->load_components('DB');
140   $target->connection(@info);
141 }
142
143 1;
144
145 =back
146
147 =head1 AUTHORS
148
149 Matt S. Trout <mst@shadowcatsystems.co.uk>
150
151 =head1 LICENSE
152
153 You may distribute this code under the same terms as Perl itself.
154
155 =cut
156