minor fixups
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema.pm
CommitLineData
ad91060a 1package Catalyst::Model::DBIC::Schema;
2
3use strict;
4use base qw/Catalyst::Base Class::Accessor::Fast Class::Data::Accessor/;
5use NEXT;
6use UNIVERSAL::require;
7use Carp;
8
9our $VERSION = '0.01';
10
11__PACKAGE__->mk_classdata('composed_schema');
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
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
62This 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
70This is the classname of your L<DBIx::Class::Schema> Schema. It needs
71to be findable in C<@INC>, but it does not need to be underneath
72C<Catalyst::Model::>.
73
74=item connect_info
75
76This is an arrayref of connection parameters, which are specific to your
77C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
78in L<DBIx::Class> at the time of this writing, the 4 parameters are your
79dsn, username, password, and connect options hashref.
80
81=item storage_type
82
83Allows the use of a different C<storage_type> than what is set in your
84C<schema_class> (which in turn defaults to C<::DBI> if not set in current
85L<DBIx::Class>). Completely optional, and probably unneccesary for most
86people, 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
96Instantiates the Model based on the above-documented ->config parameters.
97
98=item schema
99
100Accessor which returns the connected schema being used by the this model.
101
102=item composed_schema
103
104Accessor which returns the composed schema, which has no connection info,
105which was used in constructing the C<schema> above. Useful for creating
106new connections based on the same schema/model.
107
108=item clone
109
110Shortcut for ->composed_schema->clone
111
112=item connect
113
114Shortcut for ->composed_schema->connect
115
116=back
117
118=cut
119
120sub 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
155sub clone { shift->composed_schema->clone(@_); }
156
157# convenience method
158sub connect { shift->composed_schema->connect(@_); }
159
160=head1 SEE ALSO
161
162L<Catalyst>, L<DBIx::Class>, L<DBIx::Class::Schema>,
163L<DBIx::Class::Schema::Loader>
164
165=head1 AUTHOR
166
167Brandon L Black, C<blblack@gmail.com>
168
169=head1 COPYRIGHT
170
171This program is free software, you can redistribute it and/or modify it
172under the same terms as Perl itself.
173
174=cut
175
1761;