TheSchwartz.pmをCatalyst::Model::AdaptorでModel化して扱う時のメモ。次のURLを参考にして設定を行ったのだが問題発生。
Catalyst::Model::AdaptorでTheSchwartzをModel化 – hide-k.net#blog
http://blog.hide-k.net/archives/2008/02/catalystmodelad_1.php
MyApp::Model::TheSchwartz
package MyApp::Model::TheSchwartz; use strict; use warnings; use base 'Catalyst::Model::Adaptor'; 1;
myapp.yml(部分)
Model::TheSchwartz:
class: "TheSchwartz"
constructor: "new"
args:
databases:
-
dsn: "dbi:mysql:myapp_db:localhost:3306"
user: "ユーザー名"
pass: "パスワード"
実行しようとすると、
$ sudo /etc/rc.d/init.d/httpd start
httpd を起動中: Syntax error on line 54 of /etc/httpd/conf.d/perl.conf:
Couldn't instantiate component "MyApp::Model::TheSchwartz", "unknown options HASH(0xae3ee38) at /usr/local/lib/perl5/site_perl/5.10.1/Catalyst/Model/Adaptor/Base.pm line 27"Compilation failed in require at (eval 2) line 3.\n
[失敗]
などというエラーが出てうまくいかないのだ。調べてみると、TheSchwartz->new()に渡す引数で怒られてるらしい。とりあえずTheSchwartz.pmを次のように修正して解決した。
*** /tmp/TheSchwartz.pm.orig 2010-02-18 10:06:45.000000000 +0900
--- /usr/local/lib/perl5/site_perl/5.10.1/TheSchwartz.pm 2010-02-18 10:17:20.000000000 +0900
***************
*** 27,33 ****
sub new {
my TheSchwartz $client = shift;
! my %args = @_;
$client = fields::new($client) unless ref $client;
croak "databases must be an arrayref if specified"
--- 27,33 ----
sub new {
my TheSchwartz $client = shift;
! my %args = ref $_[0] ? %{ $_[0] } : @_;
$client = fields::new($client) unless ref $client;
croak "databases must be an arrayref if specified"

TheSchewartzにパッチ当てないでMyApp::Model::TheSchwartzの方でmangle_argumentsをオーバーライドした方がいいと思います。
sub mangle_arguments {
my ( $self, $args ) = @_;
return %$args;
}
http://www.slideshare.net/hidek/catalystmodeladaptor
これの12ページくらいを参考にしてみてください。
おお。これはありがとうございます!
確かにTheSchwartz自体をいじるのは乱暴でしたね。
こんな鮮やかな解決法があるとは知りませんでした。