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