Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

nekokak (7400)

nekokak
  (email not shown publicly)
http://d.hatena.ne.jp/nekokak/

Journal of nekokak (7400)

Thursday November 02, 2006
02:25 AM

Sledge::Model(EXPERIMENTAL)

[ #31485 ]

I wrote Sledge::Model.

This module is EXPERIMENTAL!

package Sledge::Model;
use strict;
use warnings;
our $VERSION = 0.01;
use Sledge::Exceptions;
 
sub new {
    my ($class, $page) = @_;
    return bless {page => $page}, $class;
}
 
1;
 
package Sledge::Model::CDBI;
use strict;
use warnings;
use base 'Sledge::Model';
use UNIVERSAL::require;
 
sub get {
    my ($self, $name) = @_;
    my $cdbi = $self->_get_class;
    my $cdbi_class = "$cdbi\::$name";
    $cdbi_class->use or die $@;
    return $cdbi_class;
}
 
sub _get_class { shift->{page}->config->{cdbi_class} }
 
1;
 
package Sledge::Model::DBIC::Schema;
use strict;
use base qw/Sledge::Model/;
use UNIVERSAL::require;
our $VERSION = '0.01';
 
sub new {
    my $class = shift;
    my $self  = $class->SUPER::new(@_);
    $self->connect;
}
 
sub connect {
    my $self = shift;
    my $schema_class = $self->{page}->config->{schema_class};
    $schema_class->use or die $@;
    return $schema_class->connect($self->{page}->config->datasource);
}
 
1;
 
package Sledge::Plugin::Model;
use strict;
use warnings;
our $VERSION = '0.01';
use Sledge::Exceptions;
 
sub import {
    my $self = shift;
    my $pkg  = caller;
 
    $pkg->mk_accessors('model');
    $pkg->add_trigger(AFTER_INIT => sub {
        my $self = shift;
        $self->model($self->create_model);
    });
 
    {
        no strict 'refs'; ## no critic
        *{"$pkg\::create_model"} = sub {
            Sledge::Exception::AbstractMethod->throw
        };
    }
}
 
1;

For CDBI.
in your config.yaml:

  cdbi_class: Proj::Data
  datasource:
    - dbi:mysql:proj
    - user
    - passwd

in your controller:

  sub create_model { Sledge::Model::CDBI->new(shift) }
 
  sub dispatch_index {
    my $self = shift;
    my $memo = $self->model->get('Memo')->retrieve_all;
  }

For DBIC::Schema.
in your config.yaml:

  schema_class: Proj::Schema
  datasource:
    - dbi:mysql:proj
    - user
    - passwd

in your controller:

  sub create_model { Sledge::Model::DBIC::Schema->new(shift) }
 
  sub dispatch_index {
    my $self = shift;
    my $memo = $self->model->resultset('Hoge')->search({});
  }

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.