« 雪だるま | Home | 巻き込み重要 »

[Catalyst]   Catalyst::Model::AdaptorでTheSchwartzをModel化

今さらTheSchwartzを使い始めました。
で、jobの状態を監視するWebインターフェースをCatalystで作ることになったのですが、いちいちTheSchwartzのインスタンスを作るのが面倒くさい。
でもそのためにModel作るのもアホらしい。
で、Catalyst::Model::Adaptorを使ってMyApp::Model::TheSchwartzを作ってモデルとして使うのをやってみたのでメモ。

HelperスクリプトでMyApp::Model::TheSchwartzを作成
script/myapp_create.pl model TheSchwartz Adaptor TheSchwartz
出来上がったMyApp::Model::TheSchwartzを変更
package MyApp::Model::TheSchwartz;
use strict;
use warnings;
use base 'Catalyst::Model::Adaptor';

__PACKAGE__->config(
    class       => 'TheSchwartz',
    constructor => 'new',
);

sub prepare_arguments {
    my ( $self, $app ) = @_;

    my $args = $app->config->{"Model::TheSchwartz"};
    return $args;
}

sub mangle_arguments {
    my ( $self, $args ) = @_;

    return %$args;
}

1;
myapp.ymlに接続情報を追加
Model::TheSchwartz:
  databases:
    -
      dsn: dbi:mysql:theschwartz
      user: root
      pass:

コントローラとかで

jobを登録
$c->model('TheSchwartz')->insert($job);
jobのリストを取得
my @jobs = $c->model('TheSchwartz')->list_jobs({'funcname' => 'MyApp::Worker'});

楽勝。

Trackbacks:

このエントリーのトラックバックURL:

コメントを投稿