9cf27abb9b3de2e1e43a28a87209cdb620679123
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / SchemaLoader.pm
1 package Catalyst::Helper::Model::DBIC::SchemaLoader;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 =head1 NAME
8
9 Catalyst::Helper::Model::DBIC::SchemaLoader - Helper for AutoLoaded DBIC Schema Models
10
11 =head1 SYNOPSIS
12
13   script/myapp_create.pl model Foo DBIC::SchemaLoader [ connect info arguments ]
14
15   Where:
16     Foo is the short name for the Model class being generated
17     connect_info arguments are the same as what DBIx::Class::Schema::connect
18       expects, and are storage_type-specific.  For DBI-based storage, these
19       arguments are the dsn, username, password, and connect options,
20       respectively.
21
22 =head1 TYPICAL EXAMPLE
23
24   script/myapp_create.pl model Foo DBIC::SchemaLoader dbi:mysql:foodb myuname mypass '{ AutoCommit => 1 }'
25
26 =head1 DESCRIPTION
27
28 This helper generates two classes:
29
30 First, it generates a L<DBIx::Class::Schema::Loader> class at
31 F<lib/MyApp/SchemaLoader/Foo.pm> based on your supplied dsn/user/pass.
32
33 Then, it generates a L<Catalyst::Model::DBIC::Schema> at
34 F<lib/MyApp/M/Foo.pm>, which references the above-generated loader.
35
36 Models generated by this Helper require the seperate package
37 L<DBIx::Class::Schema::Loader> to be installed.  It is on the recommended
38 list for this package, but is not required for installation.
39
40 =head2 METHODS
41
42 =head3 mk_compclass
43
44 =cut
45
46 sub mk_compclass {
47     my ($self, $helper, @connect_info) = @_;
48
49     for(@connect_info) {
50         $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
51     }
52
53     $helper->{loader_class} = $helper->{class};
54     $helper->{loader_class} =~ s/\:\:M(?:odel)?\:\:/::SchemaLoader::/;
55     my @loader_parts = split(/\:\:/, $helper->{loader_class});
56     my $loader_file_part = pop @loader_parts;
57
58     my $loader_dir  = File::Spec->catfile( $helper->{base}, 'lib', @loader_parts );
59     my $loader_file = File::Spec->catfile( $loader_dir, $loader_file_part . '.pm' );
60
61     $helper->mk_dir($loader_dir);
62     $helper->{connect_info} = \@connect_info;
63
64     $helper->mk_dir( $loader_dir );
65     $helper->render_file( 'loaderclass', $loader_file );
66
67
68     my $file = $helper->{file};
69     $helper->render_file( 'compclass', $file );
70 }
71
72 =head1 SEE ALSO
73
74 General Catalyst Stuff:
75
76 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
77 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
78
79 Stuff related to DBIC and this Model style:
80
81 L<DBIx::Class>, L<DBIx::Class::Schema>,
82 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>,
83 L<Catalyst::Helper::Model::DBIC::Schema>
84
85 =head1 AUTHOR
86
87 Brandon L Black, C<blblack@gmail.com>
88
89 =head1 LICENSE
90
91 This library is free software, you can redistribute it and/or modify
92 it under the same terms as Perl itself.
93
94 =cut
95
96 1;
97
98 __DATA__
99
100 =begin pod_to_ignore
101
102 __loaderclass__
103 package [% loader_class %];
104
105 use strict;
106 use base qw/DBIx::Class::Schema::Loader/;
107
108 __PACKAGE__->load_from_connection(
109     connect_info => [
110         [% FOREACH arg = connect_info %][% arg %],
111         [% END %]
112     ],
113     relationships => 1,
114     # debug => 1,
115 );
116
117 =head1 NAME
118
119 [% loader_class %] - Loader-generated DBIx::Class::Schema class
120
121 =head1 SYNOPSIS
122
123 See L<[% app %]>
124
125 =head1 DESCRIPTION
126
127 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
128
129 =head1 AUTHOR
130
131 [% author %]
132
133 =head1 LICENSE
134
135 This library is free software, you can redistribute it and/or modify
136 it under the same terms as Perl itself.
137
138 =cut
139
140 1;
141
142 __compclass__
143 package [% class %];
144
145 use strict;
146 use base 'Catalyst::Model::DBIC::Schema';
147
148 __PACKAGE__->config(
149     schema_class => '[% loader_class %]',
150 );
151
152 =head1 NAME
153
154 [% class %] - Catalyst DBIC Schema Model
155
156 =head1 SYNOPSIS
157
158 See L<[% app %]>
159
160 =head1 DESCRIPTION
161
162 L<Catalyst::Model::DBIC::Schema> Model using L<DBIx::Class::Schema::Loader>
163 generated Schema: L<[% loader_class %]>
164
165 =head1 AUTHOR
166
167 [% author %]
168
169 =head1 LICENSE
170
171 This library is free software, you can redistribute it and/or modify
172 it under the same terms as Perl itself.
173
174 =cut
175
176 1;
177