release 0.06, fixed pod_to_ignore stuff, added an optional half-decent helper test
[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 =begin pod_to_ignore
93
94 __loaderclass__
95 package [% loader_class %];
96
97 use strict;
98 use base qw/DBIx::Class::Schema::Loader/;
99
100 __PACKAGE__->load_from_connection(
101     dsn     => '[% dsn %]',
102     user    => '[% user %]',
103     pass    => '[% pass %]',
104     options => {
105                   RaiseError         => 1,
106                   PrintError         => 0,
107                   ShowErrorStatement => 1,
108                   TraceLevel         => 0,
109                   AutoCommit         => 1,
110                 },
111     relationships => 1,
112     # debug => 1,
113 );
114
115 =head1 NAME
116
117 [% loader_class %] - Loader-generated DBIx::Class::Schema class
118
119 =head1 SYNOPSIS
120
121 See L<[% app %]>
122
123 =head1 DESCRIPTION
124
125 Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
126
127 =head1 AUTHOR
128
129 [% author %]
130
131 =head1 LICENSE
132
133 This library is free software, you can redistribute it and/or modify
134 it under the same terms as Perl itself.
135
136 =cut
137
138 1;
139
140 __compclass__
141 package [% class %];
142
143 use strict;
144 use base 'Catalyst::Model::DBIC::Schema';
145
146 __PACKAGE__->config(
147     schema_class => '[% loader_class %]',
148 );
149
150 =head1 NAME
151
152 [% class %] - Catalyst DBIC Schema Model
153
154 =head1 SYNOPSIS
155
156 See L<[% app %]>
157
158 =head1 DESCRIPTION
159
160 L<Catalyst::Model::DBIC::Schema> Model using L<DBIx::Class::Schema::Loader>
161 generated Schema: L<[% loader_class %]>
162
163 =head1 AUTHOR
164
165 [% author %]
166
167 =head1 LICENSE
168
169 This library is free software, you can redistribute it and/or modify
170 it under the same terms as Perl itself.
171
172 =cut
173
174 1;
175