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