« 本とCD | メイン | リポジトリみれるようにした。 »

PlaggerLDR::Model::DBIC

実際動くか試してないけど、Catalyst::Model::DBIC::Schemaを使うとたぶん、こうなる。

Index: lib/PlaggerLDR/Model/DBIC.pm
===================================================================
--- lib/PlaggerLDR/Model/DBIC.pm        (revision 0)
+++ lib/PlaggerLDR/Model/DBIC.pm        (revision 0)
@@ -0,0 +1,19 @@
+package PlaggerLDR::Model::DBIC;
+
+use strict;
+use warnings;
+use base qw/Catalyst::Model::DBIC::Schema/;
+use YAML;
+use List::Util qw(first);
+
+my $config = YAML::LoadFile( PlaggerLDR->path_to('root', 'config.yaml') );
+my $module = first { $_->{module} eq 'Store::DBIC' } @{$config->{plugins}};
+
+__PACKAGE__->config(
+    schema_class => 'Plagger::Schema::SQLite',
+    connect_info => $module->{config}->{connect_info}
+
+);
+
+1;
+
Index: lib/PlaggerLDR/Controller/Notify.pm
===================================================================
--- lib/PlaggerLDR/Controller/Notify.pm (revision 712)
+++ lib/PlaggerLDR/Controller/Notify.pm (working copy)
@@ -4,18 +4,9 @@
 use warnings;
 use base 'Catalyst::Controller';
 
-# XXX anti-DRY
-use YAML;
-use List::Util qw(first);
-use Plagger::Schema::SQLite;
-
-my $config = YAML::LoadFile( PlaggerLDR->path_to('root', 'config.yaml') );
-my $module = first { $_->{module} eq 'Store::DBIC' } @{$config->{plugins}};
-my $schema = Plagger::Schema::SQLite->connect(@{$module->{config}->{connect_info}});
-
 sub notify : Global {
     my($self, $c) = @_;
-    my $unread = $schema->resultset('Entry')->search({ read => 0 })->count;
+    my $unread = $c->model('DBIC::Entry')->search({ read => 0 })->count;
     $c->response->content_type('text/plain');
     $c->response->body("|$unread|http://reader.livedoor.com/reader/\n");
 }


Catalyst::Model::DBIC::SchemaをベースにしたModelを作ってそこにconnect_infoを書く。
DRYにできますよぉ。


API.pmの分は追記に。

Index: lib/PlaggerLDR/Controller/API.pm
===================================================================
--- lib/PlaggerLDR/Controller/API.pm    (revision 712)
+++ lib/PlaggerLDR/Controller/API.pm    (working copy)
@@ -4,14 +4,6 @@
 use warnings;
 use base 'Catalyst::Controller';
 
-use YAML;
-use List::Util qw(first);
-use Plagger::Schema::SQLite;
-
-my $config = YAML::LoadFile( PlaggerLDR->path_to('root', 'config.yaml') );
-my $module = first { $_->{module} eq 'Store::DBIC' } @{$config->{plugins}};
-my $schema = Plagger::Schema::SQLite->connect(@{$module->{config}->{connect_info}});
-
 sub default : Private {
     my($self, $c) = @_;
 }
@@ -20,7 +12,7 @@
     my($self, $c) = @_;
 
     my @subs;
-    for my $feed ( $schema->resultset('Feed')->search({ }) ) {
+    for my $feed ( $c->model('DBIC::Feed')->search({ }) ) {
         my $unread = $feed->entries({ read => 0 })->count;
         next if $c->req->param('unread') && $unread == 0;
 
@@ -46,7 +38,7 @@
     my $data;
     my @entries;
 
-    my $feed = $schema->resultset('Feed')->find($c->req->param('subscribe_id'));
+    my $feed = $c->model('DBIC::Feed')->find($c->req->param('subscribe_id'));
 
     $data->{subscribe_id} = $feed->id;
     $data->{channel} = {
@@ -95,7 +87,7 @@
 sub touch_all : Local {
     my($self, $c) = @_;
 
-    my $feed = $schema->resultset('Feed')->find( $c->req->param('subscribe_id') );
+    my $feed = $c->model('DBIC::Feed')->find( $c->req->param('subscribe_id') );
     for my $entry ($feed->entries({ read => 0 })) {
         $entry->read(1);
         $entry->update;