clean up some pod whitespace issues
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
1 package Catalyst::Helper::Model::DBIC::Schema;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use UNIVERSAL::require;
7
8 =head1 NAME
9
10 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
11
12 =head1 SYNOPSIS
13
14   script/create.pl model ModelName DBIC::Schema My::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
15
16 =head1 DESCRIPTION
17
18 Helper for the DBIC Schema Models.
19
20 =head2 Arguments:
21
22     ModelName is the short name for the Model class being generated
23
24     My::SchemaClass is the fully qualified classname of your Schema,
25       which might or might not yet exist.
26
27     create=dynamic instructs this Helper to generate the named Schema
28       class for you, basing it on L<DBIx::Class::Schema::Loader> (which
29       means the table information will always be dynamically loaded at
30       runtime from the database).
31
32     create=static instructs this Helper to generate the named Schema
33       class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
34       mode to create a standard, manually-defined L<DBIx::Class::Schema>
35       setup, based on what the Loader sees in your database at this moment.
36       A Schema/Model pair generated this way will not require
37       L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
38       adapt itself to changes in your database structure.  You can edit
39       the generated classes by hand to refine them.
40
41     connect_info arguments are the same as what DBIx::Class::Schema::connect
42       expects, and are storage_type-specific.  For DBI-based storage, these
43       arguments are the dsn, username, password, and connect options,
44       respectively.  These are optional for existing Schemas, but required
45       if you use either of the C<create=> options.
46
47 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
48
49 =head1 TYPICAL EXAMPLES
50
51   # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
52   #  and a Model which references it:
53   script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass create=static dbi:mysql:foodb myuname mypass
54
55   # Create a dynamic DBIx::Class::Schema::Loader-based Schema,
56   #  and a Model which references it:
57   script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
58
59   # Reference an existing Schema of any kind, and provide some connection information for ->config:
60   script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass dbi:mysql:foodb myuname mypass
61
62   # Same, but don't supply connect information yet (you'll need to do this
63   #  in your app config, or [not recommended] in the schema itself).
64   script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
65
66 =head2 METHODS
67
68 =head3 mk_compclass
69
70 =cut
71
72 sub mk_compclass {
73     my ( $self, $helper, $schema_class, @connect_info) = @_;
74
75     $helper->{schema_class} = $schema_class
76         or die "Must supply schema class name";
77
78     my $create = '';
79     if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
80         $create = $1;
81         shift @connect_info;
82     }
83
84     if(@connect_info) {
85         $helper->{setup_connect_info} = 1;
86         for(@connect_info) {
87             $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
88         }
89         $helper->{connect_info} = \@connect_info;
90     }
91
92     my $file = $helper->{file};
93     $helper->render_file( 'compclass', $file );
94
95     if($create eq 'dynamic') {
96         my @schema_parts = split(/\:\:/, $helper->{schema_class});
97         my $schema_file_part = pop @schema_parts;
98
99         my $schema_dir  = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts );
100         my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' );
101
102         $helper->mk_dir($schema_dir);
103         $helper->render_file( 'schemaclass', $schema_file );
104     }
105     elsif($create eq 'static') {
106        my $schema_dir  = File::Spec->catfile( $helper->{base}, 'lib' );
107        DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at')
108            or die "Cannot load DBIx::Class::Schema::Loader: $@";
109        make_schema_at(
110            $schema_class,
111            { relationships => 1 },
112            \@connect_info,
113        );
114     }
115 }
116
117 =head1 SEE ALSO
118
119 General Catalyst Stuff:
120
121 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
122 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
123
124 Stuff related to DBIC and this Model style:
125
126 L<DBIx::Class>, L<DBIx::Class::Schema>,
127 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>,
128 L<Catalyst::Helper::Model::DBIC::SchemaLoader>
129
130 =head1 AUTHOR
131
132 Brandon L Black, C<blblack@gmail.com>
133
134 =head1 LICENSE
135
136 This library is free software, you can redistribute it and/or modify
137 it under the same terms as Perl itself.
138
139 =cut
140
141 1;
142
143 __DATA__
144
145 =begin pod_to_ignore
146
147 __schemaclass__
148 package [% schema_class %];
149
150 use strict;
151 use base qw/DBIx::Class::Schema::Loader/;
152
153 __PACKAGE__->loader_options(
154     relationships => 1,
155     # debug => 1,
156 );
157
158 =head1 NAME
159
160 [% schema_class %] - DBIx::Class::Schema::Loader class
161
162 =head1 SYNOPSIS
163
164 See L<[% app %]>
165
166 =head1 DESCRIPTION
167
168 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
169
170 =head1 AUTHOR
171
172 [% author %]
173
174 =head1 LICENSE
175
176 This library is free software, you can redistribute it and/or modify
177 it under the same terms as Perl itself.
178
179 =cut
180
181 1;
182
183 __compclass__
184 package [% class %];
185
186 use strict;
187 use base 'Catalyst::Model::DBIC::Schema';
188
189 __PACKAGE__->config(
190     schema_class => '[% schema_class %]',
191     [% IF setup_connect_info %]connect_info => [
192         [% FOREACH arg = connect_info %][% arg %],
193         [% END %]
194     ],[% END %]
195 );
196
197 =head1 NAME
198
199 [% class %] - Catalyst DBIC Schema Model
200 =head1 SYNOPSIS
201
202 See L<[% app %]>
203
204 =head1 DESCRIPTION
205
206 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
207
208 =head1 AUTHOR
209
210 [% author %]
211
212 =head1 LICENSE
213
214 This library is free software, you can redistribute it and/or modify
215 it under the same terms as Perl itself.
216
217 =cut
218
219 1;