initial checkin
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema.pm
1 package Catalyst::Model::DBIC::Schema;
2
3 use strict;
4 use base qw/Catalyst::Base Class::Accessor::Fast Class::Data::Accessor/;
5 use NEXT;
6 use UNIVERSAL::require;
7 use Carp;
8
9 our $VERSION = '0.01';
10
11 __PACKAGE__->mk_classdata('composed_schema');
12 __PACKAGE__->mk_accessors('schema');
13
14 =head1 NAME
15
16 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
17
18 =head1 SYNOPSIS
19
20     package MyApp::Model::Foo;
21     use strict;
22     use base 'Catalyst::Model::DBIC::Schema';
23
24     __PACKAGE__->config(
25         schema_class => 'Foo::SchemaClass',
26         connect_info => [ 'dbi:Pg:dbname=foodb',
27                           'postgres',
28                           '',
29                           { AutoCommit => 1 },
30                         ],
31     );
32
33     1;
34
35     # In controller code:
36
37     # ->schema To access schema methods:
38     $c->model('Foo')->schema->source(...);
39
40     # Shortcut to the schema resultset monikers for ->search et al:
41     $c->model('Foo::Bar')->search(...);
42     # is the same as $c->model('Foo')->schema->resultset('Bar')->search(...);
43
44     # To get the composed schema for making new connections:
45     my $newconn = $c->model('Foo')->composed_schema->connect(...);
46
47     # Or the same thing via a convenience shortcut:
48     my $newconn = $c->model('Foo')->connect(...);
49
50     # or, if your schema works on different storage drivers:
51     my $newconn = $c->model('Foo')->composed_schema->clone();
52     $newconn->storage_type('::LDAP');
53     $newconn->connect(...);
54
55     # and again, a convenience shortcut
56     my $newconn = $c->model('Foo')->clone();
57     $newconn->storage_type('::LDAP');
58     $newconn->connect(...);
59
60 =head1 DESCRIPTION
61
62 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models.
63
64 =head1 CONFIG PARAMETERS
65
66 =over 4
67
68 =item schema_class
69
70 This is the classname of your L<DBIx::Class::Schema> Schema.  It needs
71 to be findable in C<@INC>, but it does not need to be underneath
72 C<Catalyst::Model::>.
73
74 =item connect_info
75
76 This is an arrayref of connection parameters, which are specific to your
77 C<storage_type>.  For C<::DBI>, which is the only supported C<storage_type>
78 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
79 dsn, username, password, and connect options hashref.
80
81 =item storage_type
82
83 Allows the use of a different C<storage_type> than what is set in your
84 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
85 L<DBIx::Class>).  Completely optional, and probably unneccesary for most
86 people, until other storage backends become available for L<DBIx::Class>.
87
88 =back
89
90 =head1 METHODS
91
92 =over 4
93
94 =item new
95
96 Instantiates the Model based on the above-documented ->config parameters.
97
98 =item schema
99
100 Accessor which returns the connected schema being used by the this model.
101
102 =item composed_schema
103
104 Accessor which returns the composed schema, which has no connection info,
105 which was used in constructing the C<schema> above.  Useful for creating
106 new connections based on the same schema/model.
107
108 =item clone
109
110 Shortcut for ->composed_schema->clone
111
112 =item connect
113
114 Shortcut for ->composed_schema->connect
115
116 =back
117
118 =cut
119
120 sub new {
121     my ( $self, $c ) = @_;
122     $self = $self->NEXT::new($c);
123     
124     my $class = ref($self);
125     my $model_name = $class;
126     $model_name =~ s/^[\w:]+::(?:Model|M):://;
127
128     foreach (qw/ connect_info schema_class /) {
129         croak "->config->{$_} must be defined for this model"
130             unless $self->{$_};
131     }
132
133     my $schema_class = $self->{schema_class};
134
135     $schema_class->require
136         or croak "Cannot load schema class '$schema_class': $@";
137
138     $self->composed_schema($schema_class->compose_namespace($class));
139     $self->schema($self->composed_schema->clone);
140     $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
141     $self->schema->connect(@{$self->{connect_info}});
142
143     no strict 'refs';
144     foreach my $moniker ($self->schema->sources) {
145         *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub {
146             shift;
147             shift->model($model_name)->schema->resultset($moniker);
148         }
149     }
150
151     return $self;
152 }
153
154 # convenience method
155 sub clone { shift->composed_schema->clone(@_); }
156
157 # convenience method
158 sub connect { shift->composed_schema->connect(@_); }
159
160 =head1 SEE ALSO
161
162 L<Catalyst>, L<DBIx::Class>, L<DBIx::Class::Schema>,
163 L<DBIx::Class::Schema::Loader>
164
165 =head1 AUTHOR
166
167 Brandon L Black, C<blblack@gmail.com>
168
169 =head1 COPYRIGHT
170
171 This program is free software, you can redistribute it and/or modify it
172 under the same terms as Perl itself.
173
174 =cut
175
176 1;