Catalyst::Helper::Model::CDBI - Helper for CDBI Models


Catalyst-Model-CDBI documentation Contained in the Catalyst-Model-CDBI distribution.

Index


Code Index:

NAME

Top

Catalyst::Helper::Model::CDBI - Helper for CDBI Models

SYNOPSIS

Top

    script/create.pl model CDBI CDBI dsn user password

DESCRIPTION

Top

Helper for CDBI Model.

METHODS

mk_compclass

Reads the database and makes a main model class as well as placeholders for each table.

mk_comptest

Makes tests for the CDBI Model.

SEE ALSO

Top

Catalyst::Manual, Catalyst::Test, Catalyst::Request, Catalyst::Response, Catalyst::Helper

AUTHOR

Top

Sebastian Riedel, sri@oook.de

LICENSE

Top

This library is free software . You can redistribute it and/or modify it under the same terms as perl itself.

NAME

Top

[% class %] - CDBI Model Component

SYNOPSIS

Top

See [% app %]

DESCRIPTION

Top

CDBI Model Component.

AUTHOR

Top

[% author %]

LICENSE

Top

This library is free software . You can redistribute it and/or modify it under the same terms as perl itself.

NAME

Top

[% tableclass %] - CDBI Table Class

SYNOPSIS

Top

See [% app %]

DESCRIPTION

Top

CDBI Table Class.

AUTHOR

Top

[% author %]

LICENSE

Top

This library is free software . You can redistribute it and/or modify it under the same terms as perl itself.


Catalyst-Model-CDBI documentation Contained in the Catalyst-Model-CDBI distribution.
package Catalyst::Helper::Model::CDBI;

use strict;
use Class::DBI::Loader;
use Class::DBI;
use File::Spec;

sub mk_compclass {
    my ( $self, $helper, $dsn, $user, $pass ) = @_;
    $helper->{dsn}  = $dsn  || '';
    $helper->{user} = $user || '';
    $helper->{pass} = $pass || '';
    $helper->{rel} = $dsn =~ /sqlite|pg|mysql/i ? 1 : 0;
    my $file = $helper->{file};
    $helper->{classes} = [];
    $helper->render_file( 'cdbiclass', $file );
    #push( @{ $helper->{classes} }, $helper->{class} );
    return 1 unless $dsn;
    my $loader = Class::DBI::Loader->new(
        dsn       => $dsn,
        user      => $user,
        password  => $pass,
        namespace => $helper->{class}
    );

    my $path = $file;
    $path =~ s/\.pm$//;
    $helper->mk_dir($path);

    for my $c ( $loader->classes ) {
        $helper->{tableclass} = $c;
        $helper->{tableclass} =~ /\W*(\w+)$/;
        my $f = $1;
        my $p = File::Spec->catfile( $path, "$f.pm" );
        $helper->render_file( 'tableclass', $p );
        push( @{ $helper->{classes} }, $c );
    }
    return 1;
}

sub mk_comptest {
    my ( $self, $helper ) = @_;
    my $test = $helper->{test};
    my $name = $helper->{name};
    for my $c ( @{ $helper->{classes} } ) {
        $helper->{tableclass} = $c;
        $helper->{tableclass} =~ /\:\:(\w+)\:\:(\w+)$/;
        my $prefix;
        unless ( $1 eq 'M' ) { $prefix = "$name\::$2" }
        else { $prefix = $2 }
        $prefix =~ s/::/-/g;
        my $test = $helper->next_test($prefix);
        $helper->render_file( 'test', $test );
    }
}

1;
__DATA__

__cdbiclass__
package [% class %];

use strict;
use base 'Catalyst::Model::CDBI';

__PACKAGE__->config(
    dsn           => '[% dsn %]',
    user          => '[% user %]',
    password      => '[% pass %]',
    options       => {},
    relationships => [% rel %]
);

1;
__tableclass__
package [% tableclass %];

use strict;

1;
__test__
use Test::More tests => 2;
use_ok( Catalyst::Test, '[% app %]' );
use_ok('[% tableclass %]');