« YAPCのVideoを、Plaggerで | メイン | chmod 644 mt-tb.cgi »

CGI::Application::Plugin::DBIC::Schema

思いつきで書いた
SYNOPSISはこんな感じ

  # In your CGI::Application based web application module...
  use CGI::Application::Plugin::DBIC::Schema;

  sub setup{
      my $self = shift;
      $self->schema_config(
          schema_class => 'My::DBIC::Schema',
          connect_info => [
                            "DBI:...",
                            "username",
                            "password",
                            {AutoCommit => 1}
                          ]
      );
  }

  sub my_run_mode {
      my $self = shift;
      my $ret = $self->schema('Actor')->find(1);

  }


使い道はあまりないかもなぁ。

package CGI::Application::Plugin::DBIC::Schema;

use strict;
use base qw/Exporter/;
use UNIVERSAL::require;

our $VERSION = '0.01';
our @EXPORT = qw/schema_config schema/;

sub schema_config {
    my $self = shift;
    my $args = ( ref $_[0] ) : shift : {@_};

    die "schema_config() error: schema_class must be defined"
      unless defined $args->{schema_class};

    my $schema_class = $args->{schema_class};
    $schema_class->require
      or die
      "schema_config() error: Cannot load schema class '$schema_class': $@";

    $self->{DBIC_SCHEMA} = $schema_class->connect($args->{connect_info} || []);
}

sub schema {
    my $self = shift;
    $self->{DBIC_SCHEMA}->resultset(shift);
}

1;