Sledge::Model(EXPERIMENTAL)

nekokak on 2006-11-02T07:25:24

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({}); }