Plagger::Plugin::Store::DBIC

作ってみたけどびみょーー。Schema をこっちの指定通りに作ってないとだめなんだよな。せっかく外部におけるのに。

package Plagger::Plugin::Store::DBIC;
use strict;
use warnings;
use base qw/Plagger::Plugin Class::Accessor::Fast/;

use UNIVERSAL::require;

our $VERSION = '0.01';

__PACKAGE__->mk_accessors(qw/schema/);

sub register {
    my ( $self, $c ) = @_;

    unless ( $self->conf->{schema_class} and $self->conf->{connect_info} ) {
        $c->error('schema_class and connect_info are required');
    }

    $self->conf->{schema_class}->require
        or $c->error(
        qq/Can't load schema class "@{[ $self->conf->{schema_class} ]}", $!/);

    $self->schema( $self->conf->{schema_class}
            ->connect( @{ $self->conf->{connect_info} } ) );

    $c->register_hook( $self, 'publish.feed' => \&store, );
}

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

    # feed
    my $feed = $self->schema->resultset('Feed')
        ->find_or_new(
        { link => $args->{feed}->link, type => $args->{feed}->type } );

    $feed->$_( $args->{feed}->$_ )
        for (qw/url image description language author updated/);

    $feed->insert_or_update;

    # feed meta
    $feed->meta->delete_all;
    for my $k ( keys %{ $args->{feed}->meta } ) {
        $feed->add_to_meta( { key => $k, value => $args->{feed}->meta->{$k}, } );
    }

    # entry
    for my $feed_entry ( @{ $args->{feed}->entries } ) {
        my $entry = $self->schema->resultset('Entry')->find_or_new(
            {   feed => $feed->id,
                link => $feed_entry->link,
            }
        );
        $entry->$_( $feed_entry->$_ )
            for qw/title author date summary body rate icon/;
        $entry->insert_or_update;

        # meta
        $entry->meta->delete_all;
        for my $k ( keys %{ $feed_entry->meta } ) {
            $entry->add_to_meta( { key => $k, value => $feed_entry->meta->{$k}, } );
        }

        # tag
        $entry->tag_mappings->delete_all;
        for my $entry_tag ( @{ $feed_entry->tags } ) {
            my $tag = $self->schema->resultset('Tag')
                ->find_or_create( { name => $entry_tag } );
            $entry->add_to_tag_mappings(
                { entry => $entry->id, tag => $tag->id } );
        }
    }
}

1;

config.yaml は

 - module: Store::DBIC
   config:
     schema_class: 'Plaggerer::Schema'
     connect_info: [ 'dbi:SQLite:/home/murase/plaggerer/db/plaggerer.db' ]

Schema ファイルを置く場所もまた問題。

この場合はPlugin指定のSchemaしか扱えないから普通にこのプラグイン以下に置けばいいのかも知れんけどなぁ。

by typester / at 2006-02-27T23:35:00 / perl · plagger / Comments(0)