Commit | Line | Data |
ad91060a |
1 | package Catalyst::Model::DBIC::Schema; |
2 | |
0fbbc8d5 |
3 | use Moose; |
0f3de2c4 |
4 | use mro 'c3'; |
0fbbc8d5 |
5 | extends 'Catalyst::Model'; |
fb691af9 |
6 | with 'CatalystX::Component::Traits'; |
0fbbc8d5 |
7 | |
078c5263 |
8 | our $VERSION = '0.26'; |
f090a149 |
9 | |
73f72d28 |
10 | use namespace::autoclean; |
bd309c0c |
11 | use Carp::Clan '^Catalyst::Model::DBIC::Schema'; |
bfcd6e3d |
12 | use Data::Dumper; |
2201c2e4 |
13 | use DBIx::Class (); |
0fbbc8d5 |
14 | use Moose::Autobox; |
46a2eb0c |
15 | use Class::Inspector (); |
ad91060a |
16 | |
61ed82a5 |
17 | use Catalyst::Model::DBIC::Schema::Types |
7314403a |
18 | qw/ConnectInfo LoadedClass/; |
61ed82a5 |
19 | |
41bcf32f |
20 | use MooseX::Types::Moose qw/ArrayRef Str ClassName Undef/; |
0fbbc8d5 |
21 | |
ad91060a |
22 | =head1 NAME |
23 | |
24 | Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class |
25 | |
26 | =head1 SYNOPSIS |
27 | |
aabc1d75 |
28 | Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema: |
29 | |
30 | =over |
31 | |
32 | =item 1. |
33 | |
34 | Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm: |
35 | |
36 | package MyApp::Schema::FilmDB; |
37 | use base qw/DBIx::Class::Schema/; |
38 | |
39 | __PACKAGE__->load_classes(qw/Actor Role/); |
40 | |
41 | =item 2. |
42 | |
43 | Create some classes for the tables in the database, for example an |
44 | Actor in MyApp/Schema/FilmDB/Actor.pm: |
45 | |
46 | package MyApp::Schema::FilmDB::Actor; |
47 | use base qw/DBIx::Class/ |
07edc53e |
48 | |
aabc1d75 |
49 | __PACKAGE__->load_components(qw/Core/); |
50 | __PACKAGE__->table('actor'); |
07edc53e |
51 | |
aabc1d75 |
52 | ... |
53 | |
c8ae74f8 |
54 | and a Role in MyApp/Schema/FilmDB/Role.pm: |
aabc1d75 |
55 | |
56 | package MyApp::Schema::FilmDB::Role; |
57 | use base qw/DBIx::Class/ |
07edc53e |
58 | |
aabc1d75 |
59 | __PACKAGE__->load_components(qw/Core/); |
4a3e80e9 |
60 | __PACKAGE__->table('role'); |
07edc53e |
61 | |
aabc1d75 |
62 | ... |
63 | |
64 | Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's |
65 | usable as a standalone module and you can test/run it without Catalyst. |
66 | |
67 | =item 3. |
68 | |
69 | To expose it to Catalyst as a model, you should create a DBIC Model in |
70 | MyApp/Model/FilmDB.pm: |
71 | |
72 | package MyApp::Model::FilmDB; |
73 | use base qw/Catalyst::Model::DBIC::Schema/; |
07edc53e |
74 | |
aabc1d75 |
75 | __PACKAGE__->config( |
76 | schema_class => 'MyApp::Schema::FilmDB', |
2201c2e4 |
77 | connect_info => { |
78 | dsn => "DBI:...", |
79 | user => "username", |
80 | password => "password", |
81 | } |
aabc1d75 |
82 | ); |
83 | |
84 | See below for a full list of the possible config parameters. |
85 | |
86 | =back |
87 | |
6dec11b7 |
88 | Now you have a working Model which accesses your separate DBIC Schema. This can |
aabc1d75 |
89 | be used/accessed in the normal Catalyst manner, via $c->model(): |
90 | |
91 | my $actor = $c->model('FilmDB::Actor')->find(1); |
92 | |
93 | You can also use it to set up DBIC authentication with |
6f6b9c2d |
94 | L<Catalyst::Authentication::Store::DBIx::Class> in MyApp.pm: |
aabc1d75 |
95 | |
96 | package MyApp; |
07edc53e |
97 | |
6f6b9c2d |
98 | use Catalyst qw/... Authentication .../; |
07edc53e |
99 | |
aabc1d75 |
100 | ... |
07edc53e |
101 | |
6f6b9c2d |
102 | __PACKAGE__->config->{authentication} = |
103 | { |
104 | default_realm => 'members', |
105 | realms => { |
106 | members => { |
107 | credential => { |
108 | class => 'Password', |
109 | password_field => 'password', |
110 | password_type => 'hashed' |
111 | password_hash_type => 'SHA-256' |
112 | }, |
113 | store => { |
114 | class => 'DBIx::Class', |
115 | user_model => 'DB::User', |
116 | role_relation => 'roles', |
117 | role_field => 'rolename', |
118 | } |
119 | } |
120 | } |
121 | }; |
aabc1d75 |
122 | |
d52bc376 |
123 | C<< $c->model('Schema::Source') >> returns a L<DBIx::Class::ResultSet> for |
124 | the source name parameter passed. To find out more about which methods can |
125 | be called on a ResultSet, or how to add your own methods to it, please see |
126 | the ResultSet documentation in the L<DBIx::Class> distribution. |
aabc1d75 |
127 | |
128 | Some examples are given below: |
129 | |
f1613faa |
130 | # to access schema methods directly: |
131 | $c->model('FilmDB')->schema->source(...); |
132 | |
133 | # to access the source object, resultset, and class: |
07edc53e |
134 | $c->model('FilmDB')->source(...); |
135 | $c->model('FilmDB')->resultset(...); |
136 | $c->model('FilmDB')->class(...); |
c12b7310 |
137 | |
07edc53e |
138 | # For resultsets, there's an even quicker shortcut: |
139 | $c->model('FilmDB::Actor') |
140 | # is the same as $c->model('FilmDB')->resultset('Actor') |
ad91060a |
141 | |
f1613faa |
142 | # To get the composed schema for making new connections: |
143 | my $newconn = $c->model('FilmDB')->composed_schema->connect(...); |
144 | |
145 | # Or the same thing via a convenience shortcut: |
146 | my $newconn = $c->model('FilmDB')->connect(...); |
147 | |
148 | # or, if your schema works on different storage drivers: |
149 | my $newconn = $c->model('FilmDB')->composed_schema->clone(); |
150 | $newconn->storage_type('::LDAP'); |
151 | $newconn->connection(...); |
152 | |
153 | # and again, a convenience shortcut |
154 | my $newconn = $c->model('FilmDB')->clone(); |
155 | $newconn->storage_type('::LDAP'); |
156 | $newconn->connection(...); |
157 | |
ad91060a |
158 | =head1 DESCRIPTION |
159 | |
7b39f3f0 |
160 | This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See |
ef91bcf9 |
161 | the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for |
162 | information on generating these Models via Helper scripts. |
ad91060a |
163 | |
d52bc376 |
164 | When your Catalyst app starts up, a thin Model layer is created as an |
165 | interface to your DBIC Schema. It should be clearly noted that the model |
166 | object returned by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or |
167 | resultset object, but merely a wrapper proving L<methods|/METHODS> to access |
168 | the underlying schema. |
169 | |
170 | In addition to this model class, a shortcut class is generated for each |
171 | source in the schema, allowing easy and direct access to a resultset of the |
172 | corresponding type. These generated classes are even thinner than the model |
173 | class, providing no public methods but simply hooking into Catalyst's |
174 | model() accessor via the |
175 | L<ACCEPT_CONTEXT|Catalyst::Component/ACCEPT_CONTEXT> mechanism. The complete |
176 | contents of each generated class is roughly equivalent to the following: |
177 | |
178 | package MyApp::Model::FilmDB::Actor |
179 | sub ACCEPT_CONTEXT { |
180 | my ($self, $c) = @_; |
181 | $c->model('FilmDB')->resultset('Actor'); |
182 | } |
183 | |
184 | In short, there are three techniques available for obtaining a DBIC |
185 | resultset object: |
186 | |
187 | # the long way |
188 | my $rs = $c->model('FilmDB')->schema->resultset('Actor'); |
189 | |
190 | # using the shortcut method on the model object |
191 | my $rs = $c->model('FilmDB')->resultset('Actor'); |
192 | |
193 | # using the generated class directly |
194 | my $rs = $c->model('FilmDB::Actor'); |
195 | |
c082639a |
196 | In order to add methods to a DBIC resultset, you cannot simply add them to |
197 | the source (row, table) definition class; you must define a separate custom |
198 | resultset class. See L<DBIx::Class::Manual::Cookbook/"Predefined searches"> |
199 | for more info. |
200 | |
ad91060a |
201 | =head1 CONFIG PARAMETERS |
202 | |
c4fee9b8 |
203 | =head2 schema_class |
ad91060a |
204 | |
205 | This is the classname of your L<DBIx::Class::Schema> Schema. It needs |
aabc1d75 |
206 | to be findable in C<@INC>, but it does not need to be inside the |
207 | C<Catalyst::Model::> namespace. This parameter is required. |
ad91060a |
208 | |
c4fee9b8 |
209 | =head2 connect_info |
ad91060a |
210 | |
211 | This is an arrayref of connection parameters, which are specific to your |
b9a72351 |
212 | C<storage_type> (see your storage type documentation for more details). |
213 | If you only need one parameter (e.g. the DSN), you can just pass a string |
214 | instead of an arrayref. |
ad91060a |
215 | |
0f2fd2c0 |
216 | This is not required if C<schema_class> already has connection information |
d89e6c8a |
217 | defined inside itself (which isn't highly recommended, but can be done) |
0f2fd2c0 |
218 | |
7db6da78 |
219 | For L<DBIx::Class::Storage::DBI>, which is the only supported |
220 | C<storage_type> in L<DBIx::Class> at the time of this writing, the |
221 | parameters are your dsn, username, password, and connect options hashref. |
222 | |
018eb0e2 |
223 | See L<DBIx::Class::Storage::DBI/connect_info> for a detailed explanation |
224 | of the arguments supported. |
7db6da78 |
225 | |
226 | Examples: |
227 | |
2201c2e4 |
228 | connect_info => { |
229 | dsn => 'dbi:Pg:dbname=mypgdb', |
230 | user => 'postgres', |
231 | password => '' |
232 | } |
07edc53e |
233 | |
2201c2e4 |
234 | connect_info => { |
235 | dsn => 'dbi:SQLite:dbname=foo.db', |
236 | on_connect_do => [ |
237 | 'PRAGMA synchronous = OFF', |
238 | ] |
239 | } |
07edc53e |
240 | |
2201c2e4 |
241 | connect_info => { |
242 | dsn => 'dbi:Pg:dbname=mypgdb', |
243 | user => 'postgres', |
244 | password => '', |
245 | pg_enable_utf8 => 1, |
246 | on_connect_do => [ |
247 | 'some SQL statement', |
248 | 'another SQL statement', |
249 | ], |
250 | } |
7db6da78 |
251 | |
8281c933 |
252 | Or using L<Config::General>: |
253 | |
254 | <Model::FilmDB> |
255 | schema_class MyApp::Schema::FilmDB |
c34bcab6 |
256 | traits Caching |
8281c933 |
257 | <connect_info> |
2201c2e4 |
258 | dsn dbi:Pg:dbname=mypgdb |
259 | user postgres |
42e14c31 |
260 | password "" |
2201c2e4 |
261 | auto_savepoint 1 |
a75b6e58 |
262 | quote_char """ |
8281c933 |
263 | on_connect_do some SQL statement |
264 | on_connect_do another SQL statement |
265 | </connect_info> |
266 | </Model::FilmDB> |
267 | |
268 | or |
269 | |
270 | <Model::FilmDB> |
271 | schema_class MyApp::Schema::FilmDB |
272 | connect_info dbi:SQLite:dbname=foo.db |
273 | </Model::FilmDB> |
274 | |
2201c2e4 |
275 | Or using L<YAML>: |
276 | |
277 | Model::MyDB: |
278 | schema_class: MyDB |
279 | connect_info: |
280 | dsn: dbi:Oracle:mydb |
281 | user: mtfnpy |
282 | password: mypass |
283 | LongReadLen: 1000000 |
284 | LongTruncOk: 1 |
285 | on_connect_do: [ "alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'" ] |
286 | cursor_class: 'DBIx::Class::Cursor::Cached' |
a75b6e58 |
287 | quote_char: '"' |
2201c2e4 |
288 | |
289 | The old arrayref style with hashrefs for L<DBI> then L<DBIx::Class> options is also |
290 | supported: |
291 | |
292 | connect_info => [ |
293 | 'dbi:Pg:dbname=mypgdb', |
294 | 'postgres', |
295 | '', |
296 | { |
297 | pg_enable_utf8 => 1, |
298 | }, |
299 | { |
0fbbc8d5 |
300 | auto_savepoint => 1, |
2201c2e4 |
301 | on_connect_do => [ |
302 | 'some SQL statement', |
303 | 'another SQL statement', |
304 | ], |
305 | } |
306 | ] |
307 | |
c34bcab6 |
308 | =head2 traits |
0fbbc8d5 |
309 | |
41bcf32f |
310 | Array of Traits to apply to the instance. Traits are L<Moose::Role>s. |
311 | |
fb691af9 |
312 | They are relative to the C<< MyApp::TraitFor::Model::DBIC::Schema:: >>, then the C<< |
313 | Catalyst::TraitFor::Model::DBIC::Schema:: >> namespaces, unless prefixed with C<+> |
41bcf32f |
314 | in which case they are taken to be a fully qualified name. E.g.: |
2201c2e4 |
315 | |
c34bcab6 |
316 | traits Caching |
fb691af9 |
317 | traits +MyApp::TraitFor::Model::Foo |
2201c2e4 |
318 | |
0fbbc8d5 |
319 | A new instance is created at application time, so any consumed required |
320 | attributes, coercions and modifiers will work. |
2201c2e4 |
321 | |
fb691af9 |
322 | Traits are applied at L<Catalyst::Component/COMPONENT> time using |
323 | L<CatalystX::Component::Traits>. |
0fbbc8d5 |
324 | |
41bcf32f |
325 | C<ref $self> will be an anon class if any traits are applied, C<< |
326 | $self->_original_class_name >> will be the original class. |
f090a149 |
327 | |
c7d7b849 |
328 | When writing a Trait, interesting points to modify are C<BUILD>, L</setup> and |
329 | L</ACCEPT_CONTEXT>. |
0fbbc8d5 |
330 | |
c34bcab6 |
331 | Traits that come with the distribution: |
0fbbc8d5 |
332 | |
333 | =over 4 |
2201c2e4 |
334 | |
fb691af9 |
335 | =item L<Catalyst::TraitFor::Model::DBIC::Schema::Caching> |
0fbbc8d5 |
336 | |
fb691af9 |
337 | =item L<Catalyst::TraitFor::Model::DBIC::Schema::Replicated> |
c4fee9b8 |
338 | |
0fbbc8d5 |
339 | =back |
8281c933 |
340 | |
c4fee9b8 |
341 | =head2 storage_type |
ad91060a |
342 | |
343 | Allows the use of a different C<storage_type> than what is set in your |
344 | C<schema_class> (which in turn defaults to C<::DBI> if not set in current |
f1613faa |
345 | L<DBIx::Class>). Completely optional, and probably unnecessary for most |
346 | people until other storage backends become available for L<DBIx::Class>. |
ad91060a |
347 | |
c7d7b849 |
348 | =head1 ATTRIBUTES |
349 | |
350 | The keys you pass in the model configuration are available as attributes. |
351 | |
352 | Other attributes available: |
353 | |
354 | =head2 connect_info |
355 | |
356 | Your connect_info args normalized to hashref form (with dsn/user/password.) See |
357 | L<DBIx::Class::Storage::DBI/connect_info> for more info on the hashref form of |
358 | L</connect_info>. |
359 | |
360 | =head2 model_name |
361 | |
362 | The model name L<Catalyst> uses to resolve this model, the part after |
363 | C<::Model::> or C<::M::> in your class name. E.g. if your class name is |
364 | C<MyApp::Model::DB> the L</model_name> will be C<DB>. |
365 | |
c7d7b849 |
366 | =head2 _default_cursor_class |
367 | |
6f6b9c2d |
368 | What to reset your L<DBIx::Class::Storage::DBI/cursor_class> to if a custom one |
c7d7b849 |
369 | doesn't work out. Defaults to L<DBIx::Class::Storage::DBI::Cursor>. |
370 | |
fb691af9 |
371 | =head1 ATTRIBUTES FROM L<MooseX::Traits::Pluggable> |
372 | |
373 | =head2 _original_class_name |
374 | |
375 | The class name of your model before any L</traits> are applied. E.g. |
376 | C<MyApp::Model::DB>. |
377 | |
c7d7b849 |
378 | =head2 _traits |
379 | |
6f6b9c2d |
380 | Unresolved arrayref of traits passed in the config. |
c7d7b849 |
381 | |
382 | =head2 _resolved_traits |
383 | |
384 | Traits you used resolved to full class names. |
385 | |
ad91060a |
386 | =head1 METHODS |
387 | |
46a2eb0c |
388 | Methods not listed here are delegated to the connected schema used by the model |
389 | instance, so the following are equivalent: |
390 | |
391 | $c->model('DB')->schema->my_accessor('foo'); |
392 | # or |
393 | $c->model('DB')->my_accessor('foo'); |
394 | |
395 | Methods on the model take precedence over schema methods. |
396 | |
c4fee9b8 |
397 | =head2 new |
ad91060a |
398 | |
399 | Instantiates the Model based on the above-documented ->config parameters. |
0f2fd2c0 |
400 | The only required parameter is C<schema_class>. C<connect_info> is |
401 | required in the case that C<schema_class> does not already have connection |
402 | information defined for it. |
ad91060a |
403 | |
c4fee9b8 |
404 | =head2 schema |
f1613faa |
405 | |
406 | Accessor which returns the connected schema being used by the this model. |
407 | There are direct shortcuts on the model class itself for |
408 | schema->resultset, schema->source, and schema->class. |
409 | |
c4fee9b8 |
410 | =head2 composed_schema |
f1613faa |
411 | |
412 | Accessor which returns the composed schema, which has no connection info, |
413 | which was used in constructing the C<schema> above. Useful for creating |
414 | new connections based on the same schema/model. There are direct shortcuts |
415 | from the model object for composed_schema->clone and composed_schema->connect |
416 | |
c4fee9b8 |
417 | =head2 clone |
f1613faa |
418 | |
419 | Shortcut for ->composed_schema->clone |
420 | |
c4fee9b8 |
421 | =head2 connect |
f1613faa |
422 | |
423 | Shortcut for ->composed_schema->connect |
424 | |
c4fee9b8 |
425 | =head2 source |
c12b7310 |
426 | |
f1613faa |
427 | Shortcut for ->schema->source |
428 | |
c4fee9b8 |
429 | =head2 class |
f1613faa |
430 | |
431 | Shortcut for ->schema->class |
432 | |
c4fee9b8 |
433 | =head2 resultset |
f1613faa |
434 | |
435 | Shortcut for ->schema->resultset |
436 | |
c4fee9b8 |
437 | =head2 storage |
f1613faa |
438 | |
439 | Provides an accessor for the connected schema's storage object. |
440 | Used often for debugging and controlling transactions. |
b8427e0b |
441 | |
ad91060a |
442 | =cut |
443 | |
c34bcab6 |
444 | has schema_class => ( |
0fbbc8d5 |
445 | is => 'ro', |
7314403a |
446 | isa => LoadedClass, |
0fbbc8d5 |
447 | coerce => 1, |
448 | required => 1 |
449 | ); |
450 | |
c34bcab6 |
451 | has storage_type => (is => 'rw', isa => Str); |
0fbbc8d5 |
452 | |
18b829f0 |
453 | has connect_info => (is => 'rw', isa => ConnectInfo, coerce => 1); |
0fbbc8d5 |
454 | |
c7d7b849 |
455 | has model_name => ( |
456 | is => 'ro', |
457 | isa => Str, |
458 | required => 1, |
459 | lazy_build => 1, |
460 | ); |
ad91060a |
461 | |
46a2eb0c |
462 | # method names delegated to schema |
463 | has _delegates => ( |
464 | is => 'ro', |
465 | isa => ArrayRef, |
466 | lazy_build => 1 |
467 | ); |
468 | |
c34bcab6 |
469 | has _default_cursor_class => ( |
61ed82a5 |
470 | is => 'ro', |
7314403a |
471 | isa => LoadedClass, |
61ed82a5 |
472 | default => 'DBIx::Class::Storage::DBI::Cursor', |
473 | coerce => 1 |
474 | ); |
475 | |
0fbbc8d5 |
476 | sub BUILD { |
477 | my $self = shift; |
f2488839 |
478 | my $class = $self->_original_class_name; |
2201c2e4 |
479 | my $schema_class = $self->schema_class; |
ad91060a |
480 | |
2201c2e4 |
481 | if( !$self->connect_info ) { |
f1613faa |
482 | if($schema_class->storage && $schema_class->storage->connect_info) { |
2201c2e4 |
483 | $self->connect_info($schema_class->storage->connect_info); |
f1613faa |
484 | } |
485 | else { |
39f5f008 |
486 | die "Either ->config->{connect_info} must be defined for $class" |
460e3ac8 |
487 | . " or $schema_class must have connect info defined on it." |
488 | . " Here's what we got:\n" |
f1613faa |
489 | . Dumper($self); |
490 | } |
7db6da78 |
491 | } |
492 | |
0fbbc8d5 |
493 | if (exists $self->connect_info->{cursor_class}) { |
494 | eval { Class::MOP::load_class($self->connect_info->{cursor_class}) } |
495 | or croak "invalid connect_info: Cannot load your cursor_class" |
496 | . " ".$self->connect_info->{cursor_class}.": $@"; |
497 | } |
498 | |
0fbbc8d5 |
499 | $self->setup; |
500 | |
f1613faa |
501 | $self->composed_schema($schema_class->compose_namespace($class)); |
2201c2e4 |
502 | |
46a2eb0c |
503 | $self->meta->add_attribute('schema', |
504 | is => 'rw', |
505 | isa => $self->schema_class, |
506 | handles => $self->_delegates |
507 | ); |
508 | |
f1613faa |
509 | $self->schema($self->composed_schema->clone); |
510 | |
2201c2e4 |
511 | $self->schema->storage_type($self->storage_type) |
512 | if $self->storage_type; |
7db6da78 |
513 | |
2201c2e4 |
514 | $self->schema->connection($self->connect_info); |
515 | |
516 | $self->_install_rs_models; |
2201c2e4 |
517 | } |
518 | |
519 | sub clone { shift->composed_schema->clone(@_); } |
520 | |
521 | sub connect { shift->composed_schema->connect(@_); } |
522 | |
c4fee9b8 |
523 | =head2 setup |
2201c2e4 |
524 | |
c7d7b849 |
525 | Called at C<BUILD>> time before configuration, but after L</connect_info> is |
526 | set. To do something after configuuration use C<< after BUILD => >>. |
2201c2e4 |
527 | |
528 | =cut |
529 | |
0fbbc8d5 |
530 | sub setup { 1 } |
2201c2e4 |
531 | |
c4fee9b8 |
532 | =head2 ACCEPT_CONTEXT |
2201c2e4 |
533 | |
73f72d28 |
534 | Point of extension for doing things at C<< $c->model >> time with context, |
535 | returns the model instance, see L<Catalyst::Manual::Intro/ACCEPT_CONTEXT> for |
536 | more information. |
2201c2e4 |
537 | |
0fbbc8d5 |
538 | =cut |
2201c2e4 |
539 | |
0fbbc8d5 |
540 | sub ACCEPT_CONTEXT { shift } |
2201c2e4 |
541 | |
542 | sub _install_rs_models { |
543 | my $self = shift; |
7b1fe8c2 |
544 | my $class = $self->_original_class_name; |
2201c2e4 |
545 | |
ad91060a |
546 | no strict 'refs'; |
39f5f008 |
547 | |
548 | my @sources = $self->schema->sources; |
549 | |
550 | die "No sources found (did you forget to define your tables?)" |
551 | unless @sources; |
552 | |
553 | foreach my $moniker (@sources) { |
0b2a7108 |
554 | my $classname = "${class}::$moniker"; |
7db6da78 |
555 | *{"${classname}::ACCEPT_CONTEXT"} = sub { |
ad91060a |
556 | shift; |
2201c2e4 |
557 | shift->model($self->model_name)->resultset($moniker); |
ad91060a |
558 | } |
559 | } |
2201c2e4 |
560 | } |
ad91060a |
561 | |
61ed82a5 |
562 | sub _reset_cursor_class { |
563 | my $self = shift; |
564 | |
565 | if ($self->storage->can('cursor_class')) { |
566 | $self->storage->cursor_class($self->_default_cursor_class) |
567 | if $self->storage->cursor_class ne $self->_default_cursor_class; |
568 | } |
569 | } |
570 | |
50f488ec |
571 | { |
572 | my %COMPOSED_CACHE; |
573 | |
574 | sub composed_schema { |
575 | my $self = shift; |
576 | my $class = $self->_original_class_name; |
577 | my $store = \$COMPOSED_CACHE{$class}{$self->schema_class}; |
578 | |
579 | $$store = shift if @_; |
580 | |
581 | return $$store |
582 | } |
583 | } |
584 | |
c7d7b849 |
585 | sub _build_model_name { |
586 | my $self = shift; |
587 | my $class = $self->_original_class_name; |
588 | (my $model_name = $class) =~ s/^[\w:]+::(?:Model|M):://; |
589 | |
590 | return $model_name; |
41bcf32f |
591 | } |
592 | |
46a2eb0c |
593 | sub _build__delegates { |
594 | my $self = shift; |
595 | |
596 | # XXX change this to CMOP once CAG is updated |
597 | my @schema_methods = @{ Class::Inspector->methods($self->schema_class) }; |
598 | |
599 | my @my_methods = $self->meta->get_all_method_names; |
600 | my %my_methods; |
601 | @my_methods{@my_methods} = (); |
602 | |
603 | my @delegates; |
604 | for my $method (@schema_methods) { |
605 | push @delegates, $method unless exists $my_methods{$method}; |
606 | } |
607 | |
608 | return \@delegates; |
609 | } |
610 | |
0fbbc8d5 |
611 | __PACKAGE__->meta->make_immutable; |
2201c2e4 |
612 | |
ad91060a |
613 | =head1 SEE ALSO |
614 | |
7b39f3f0 |
615 | General Catalyst Stuff: |
616 | |
617 | L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>, |
618 | L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>, |
619 | |
620 | Stuff related to DBIC and this Model style: |
621 | |
622 | L<DBIx::Class>, L<DBIx::Class::Schema>, |
f090a149 |
623 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>, |
624 | L<MooseX::Object::Pluggable> |
ad91060a |
625 | |
c34bcab6 |
626 | Traits: |
c4fee9b8 |
627 | |
fb691af9 |
628 | L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>, |
629 | L<Catalyst::TraitFor::Model::DBIC::Schema::Replicated> |
c4fee9b8 |
630 | |
ad91060a |
631 | =head1 AUTHOR |
632 | |
c7d7b849 |
633 | Brandon L Black, C<blblack at gmail.com> |
ad91060a |
634 | |
2ff00e2b |
635 | Contributors: |
636 | |
41bcf32f |
637 | Rafael Kitover, C<rkitover at cpan.org> |
2ff00e2b |
638 | |
ad91060a |
639 | =head1 COPYRIGHT |
640 | |
641 | This program is free software, you can redistribute it and/or modify it |
642 | under the same terms as Perl itself. |
643 | |
644 | =cut |
645 | |
646 | 1; |
c7d7b849 |
647 | # vim:sts=4 sw=4 et: |