0.08, and the removal of the halfhearted ->require attempt
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema.pm
CommitLineData
ad91060a 1package Catalyst::Model::DBIC::Schema;
2
3use strict;
14d912a2 4use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
ad91060a 5use NEXT;
6use UNIVERSAL::require;
7use Carp;
8
d08cea50 9our $VERSION = '0.08';
ad91060a 10
14d912a2 11__PACKAGE__->mk_classaccessor('composed_schema');
ad91060a 12__PACKAGE__->mk_accessors('schema');
13
14=head1 NAME
15
16Catalyst::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
c12b7310 40 # certain ->schema methods (source, resultset, class) have shortcuts
41 $c->model('Foo')->source(...);
42 $c->model('Foo')->resultset(...);
43 $c->model('Foo')->class(...);
44
45 # For resultsets, there's an even quicker shortcut:
46 $c->model('Foo::Bar')
47 # is the same as $c->model('Foo')->resultset('Bar')
ad91060a 48
49 # To get the composed schema for making new connections:
50 my $newconn = $c->model('Foo')->composed_schema->connect(...);
51
52 # Or the same thing via a convenience shortcut:
53 my $newconn = $c->model('Foo')->connect(...);
54
55 # or, if your schema works on different storage drivers:
56 my $newconn = $c->model('Foo')->composed_schema->clone();
57 $newconn->storage_type('::LDAP');
f7a944f8 58 $newconn->connection(...);
ad91060a 59
60 # and again, a convenience shortcut
61 my $newconn = $c->model('Foo')->clone();
62 $newconn->storage_type('::LDAP');
f7a944f8 63 $newconn->connection(...);
ad91060a 64
65=head1 DESCRIPTION
66
1aeb6e1e 67NOTE: This is the first public release, there's probably a higher than
68average chance of random bugs and shortcomings: you've been warned.
69
7b39f3f0 70This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
71the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
1aeb6e1e 72L<Catalyst::Helper::Model::DBIC::SchemaLoader> for information
73on generating these Models via Helper scripts. The latter of the two
74will also generated a L<DBIx::Class::Schema::Loader>-based Schema class
75for you.
ad91060a 76
77=head1 CONFIG PARAMETERS
78
79=over 4
80
81=item schema_class
82
83This is the classname of your L<DBIx::Class::Schema> Schema. It needs
84to be findable in C<@INC>, but it does not need to be underneath
0f2fd2c0 85C<Catalyst::Model::>. This parameter is required.
ad91060a 86
87=item connect_info
88
89This is an arrayref of connection parameters, which are specific to your
90C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
91in L<DBIx::Class> at the time of this writing, the 4 parameters are your
92dsn, username, password, and connect options hashref.
93
0f2fd2c0 94This is not required if C<schema_class> already has connection information
95defined in itself (which would be the case for a Schema defined by
96L<DBIx::Class::Schema::Loader>, for instance).
97
ad91060a 98=item storage_type
99
100Allows the use of a different C<storage_type> than what is set in your
101C<schema_class> (which in turn defaults to C<::DBI> if not set in current
102L<DBIx::Class>). Completely optional, and probably unneccesary for most
103people, until other storage backends become available for L<DBIx::Class>.
104
105=back
106
107=head1 METHODS
108
109=over 4
110
111=item new
112
113Instantiates the Model based on the above-documented ->config parameters.
0f2fd2c0 114The only required parameter is C<schema_class>. C<connect_info> is
115required in the case that C<schema_class> does not already have connection
116information defined for it.
ad91060a 117
118=item schema
119
120Accessor which returns the connected schema being used by the this model.
c12b7310 121There are already direct shortcuts on the model class itself for
122schema->resultset, schema->source, and schema->class.
ad91060a 123
124=item composed_schema
125
126Accessor which returns the composed schema, which has no connection info,
127which was used in constructing the C<schema> above. Useful for creating
c12b7310 128new connections based on the same schema/model. There are direct shortcuts
129from the model object for composed_schema->clone and composed_schema->connect
ad91060a 130
131=item clone
132
133Shortcut for ->composed_schema->clone
134
135=item connect
136
137Shortcut for ->composed_schema->connect
138
c12b7310 139=item source
140
141Shortcut for ->schema->source
142
143=item class
144
145Shortcut for ->schema->class
146
147=item resultset
148
149Shortcut for ->schema->resultset
150
ad91060a 151=back
152
153=cut
154
155sub new {
46878f2e 156 my $self = shift->NEXT::new(@_);
ad91060a 157
158 my $class = ref($self);
159 my $model_name = $class;
160 $model_name =~ s/^[\w:]+::(?:Model|M):://;
161
0f2fd2c0 162 croak "->config->{schema_class} must be defined for this model"
163 unless $self->{schema_class};
ad91060a 164
165 my $schema_class = $self->{schema_class};
166
1aeb6e1e 167 $schema_class->require
168 or croak "Cannot load schema class '$schema_class': $@";
ad91060a 169
0f2fd2c0 170 if( !$self->{connect_info} ) {
171 if($schema_class->storage && $schema_class->storage->connect_info) {
172 $self->{connect_info} = $schema_class->storage->connect_info;
173 }
174 else {
175 croak "Either ->config->{connect_info} must be defined for $class"
1aeb6e1e 176 . " or $schema_class must have connect info defined on it";
0f2fd2c0 177 }
178 }
179
ad91060a 180 $self->composed_schema($schema_class->compose_namespace($class));
181 $self->schema($self->composed_schema->clone);
182 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
46878f2e 183 $self->schema->connection(@{$self->{connect_info}});
ad91060a 184
185 no strict 'refs';
186 foreach my $moniker ($self->schema->sources) {
0b2a7108 187 my $classname = "${class}::$moniker";
0b2a7108 188 *{"${classname}::ACCEPT_CONTEXT"} = sub {
ad91060a 189 shift;
c12b7310 190 shift->model($model_name)->resultset($moniker);
ad91060a 191 }
192 }
193
194 return $self;
195}
196
ad91060a 197sub clone { shift->composed_schema->clone(@_); }
198
ad91060a 199sub connect { shift->composed_schema->connect(@_); }
200
201=head1 SEE ALSO
202
7b39f3f0 203General Catalyst Stuff:
204
205L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
206L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
207
208Stuff related to DBIC and this Model style:
209
210L<DBIx::Class>, L<DBIx::Class::Schema>,
211L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
1aeb6e1e 212L<Catalyst::Helper::Model::DBIC::SchemaLoader>
ad91060a 213
214=head1 AUTHOR
215
216Brandon L Black, C<blblack@gmail.com>
217
218=head1 COPYRIGHT
219
220This program is free software, you can redistribute it and/or modify it
221under the same terms as Perl itself.
222
223=cut
224
2251;