added experimental DBIx::Cus...
|
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...
|
11 |
__PACKAGE__->attr(table_class => 'DBIx::Custom::Table'); |
12 |
__PACKAGE__->attr(tables => sub { {} }); |
|
added experimental DBIx::Cus...
|
13 | |
14 |
sub table { |
|
simplified DBIx::Custom::Mod...
|
15 |
my ($self, $name) = @_; |
added experimental DBIx::Cus...
|
16 |
|
simplified DBIx::Custom::Mod...
|
17 |
# Table class |
18 |
my $table_class = $self->table_class; |
|
19 |
croak qq{Invalid table class name "$table_class"} |
|
20 |
unless $table_class =~ /^[\w:]+$/; |
|
21 |
eval "use $table_class"; |
|
22 |
croak $@ if $@; |
|
23 |
|
|
24 |
# Create table |
|
25 |
$self->tables->{$name} |
|
26 |
= $table_class->new(name => $name, dbi => $self->dbi) |
|
27 |
unless defined $self->tables->{$name}; |
|
28 |
|
|
29 |
return $self->{tables}{$name}; |
|
added experimental DBIx::Cus...
|
30 |
} |
31 | ||
32 |
1; |
|
33 | ||
34 |
=head1 NAME |
|
35 | ||
36 |
DBIx::Custom::Model - Table class(experimental) |
|
37 | ||
38 |
=head1 SYNOPSIS |
|
39 | ||
40 |
use MyModel; |
|
41 | ||
42 |
use base 'DBIx::Custom::Model'; |
|
43 | ||
44 |
sub new { |
|
45 |
my $self = shift->SUPER::new(@_); |
|
46 |
|
|
simplified DBIx::Custom::Mod...
|
47 |
my $dbi = DBIx::Custom->connect(...); |
48 |
|
|
49 |
$self->dbi($dbi); |
|
50 |
|
|
51 |
$self->table('book')->helper( |
|
52 |
insert => sub { |
|
added experimental DBIx::Cus...
|
53 |
my $self = shift; |
54 |
|
|
simplified DBIx::Custom::Mod...
|
55 |
return $self->dbi->insert(table => $self->name, @_); |
added experimental DBIx::Cus...
|
56 |
} |
57 |
); |
|
58 |
|
|
59 |
return $self; |
|
60 |
} |
|
61 | ||
62 |
=head1 METHODS |
|
63 | ||
64 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
|
65 |
and implements the following new ones. |
|
66 | ||
67 |
=head2 C<table> |
|
68 | ||
simplified DBIx::Custom::Mod...
|
69 |
my $table = $model->table('book'); |
added experimental DBIx::Cus...
|
70 | |
simplified DBIx::Custom::Mod...
|
71 |
Get a L<DBIx::Custom::Table>, or create a L<DBIx::Custom::Table> object if not exists. |
added experimental DBIx::Cus...
|
72 |