Newer Older
73 lines | 1.502kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
1
package DBIx::Custom::Model;
2

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use Carp 'croak';
9

            
10
__PACKAGE__->attr(dbi => sub { DBIx::Custom->new });
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
11
__PACKAGE__->attr(table_class => 'DBIx::Custom::Table');
12
__PACKAGE__->attr(tables => sub { {} });
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
13

            
14
sub table {
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
15
    my ($self, $name) = @_;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
16
    
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
17
    # Table class
18
    my $table_class = $self->table_class;
19
    croak qq{Invalid table class name "$table_class"}
20
      unless $table_class =~ /^[\w:]+$/;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
21
    unless ($table_class->can('isa')) {
22
        eval "require $table_class";
23
        croak $@ if $@;
24
    }
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
25
    # Create table
26
    $self->tables->{$name}
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
27
        = $table_class->new(name => $name, dbi => $self->dbi, model => $self)
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
28
      unless defined $self->tables->{$name};
29
    
30
    return $self->{tables}{$name};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
31
}
32

            
33
1;
34

            
35
=head1 NAME
36

            
37
DBIx::Custom::Model - Table class(experimental)
38

            
39
=head1 SYNOPSIS
40

            
41
use MyModel;
42

            
43
use base 'DBIx::Custom::Model';
44

            
45
sub new {
46
    my $self = shift->SUPER::new(@_);
47
    
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
48
    my $dbi = DBIx::Custom->connect(...);
49
    
50
    $self->dbi($dbi);
51
    
52
    $self->table('book')->helper(
53
        insert => sub {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
54
            my $self = shift;
55
            
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
56
            return $self->dbi->insert(table => $self->name, @_);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
57
        }
58
    );
59
    
60
    return $self;
61
}
62

            
63
=head1 METHODS
64

            
65
L<DBIx::Custom> inherits all methods from L<Object::Simple>
66
and implements the following new ones.
67

            
68
=head2 C<table>
69

            
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
70
    my $table = $model->table('book');
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
71

            
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
72
Get a L<DBIx::Custom::Table>, or create a L<DBIx::Custom::Table> object if not exists.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
73