Newer Older
142 lines | 2.865kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
1
package DBIx::Custom::Table;
2

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use Carp 'croak';
9

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
10
__PACKAGE__->attr(['dbi', 'name', 'model']);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
11

            
12
our $AUTOLOAD;
13

            
14
sub AUTOLOAD {
15
    my $self = shift;
16

            
17
    # Method
18
    my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
19

            
20
    # Helper
21
    $self->{_helpers} ||= {};
22
    croak qq/Can't locate object method "$method" via "$package"/
23
      unless my $helper = $self->{_helpers}->{$method};
24

            
25
    # Run
26
    return $self->$helper(@_);
27
}
28

            
29
sub helper {
30
    my $self = shift;
31
    
32
    # Merge
33
    my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
34
    $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers};
35
    
36
    return $self;
37
}
38

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
39
sub new {
40
    my $self = shift->SUPER::new(@_);
41
    
42
    my @methods = qw/insert update update_all delete delete_all select/;
43
    foreach my $method (@methods) {
44
        $self->helper(
45
            $method => sub {
46
                my $self = shift;
47
                return $self->dbi->$method(table => $self->name, @_);
48
            }
49
        );
50
    }
51
    return $self;
52
}
53

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
54
sub DESTROY { }
55

            
56
1;
57

            
58
=head1 NAME
59

            
add examples
Yuki Kimoto authored on 2011-01-07
60
DBIx::Custom::Table - Table base class(experimental)
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
61

            
62
=head1 SYNOPSIS
63

            
64
use DBIx::Custom::Table;
65

            
66
my $table = DBIx::Custom::Table->new(name => 'books');
67

            
68
=head1 METHODS
69

            
70
L<DBIx::Custom> inherits all methods from L<Object::Simple>
71
and implements the following new ones.
72

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
73
=head2 C<delete>
74

            
75
    $table->delete(where => \%where);
76
    
77
Same as C<delete()> of L<DBIx::Custom> except that
78
you don't have to specify table name.
79

            
80
=head2 C<delete_all>
81

            
82
    $table->delete_all(param => $param);
83
    
84
Same as C<delete_all()> of L<DBIx::Custom> except that
85
you don't have to specify table name.
86

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
87
=head2 C<helper>
88

            
89
    $table->helper(insert => sub {
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
90
        my $self = shift;
91
        
92
        return $self->dbi->insert(table => $self->name, @_);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
93
    });
94
    
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
95
Add helper method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
96

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
97
=head2 C<insert>
98

            
99
    $table->insert(param => \%param);
100
    
101
Same as C<insert()> of L<DBIx::Custom> except that
102
you don't have to specify table name.
103

            
104
=head2 C<method>
105

            
106
    $table->method(
107
        select_complex => sub {
108
            my $self = shift;
109
            
110
            return $self->dbi->select($self->name, ...);
111
        },
112
        some_method => sub { ... }
113
    );
114

            
115
Define method.
116

            
117
=head2 C<new>
118

            
119
    my $table = DBIx::Custom::Table->new;
120

            
121
Create a L<DBIx::Custom::Table> object.
122

            
123
=head2 C<select>
124

            
125
    $table->select(param => $param);
126
    
127
Same as C<select()> of L<DBIx::Custom> except that
128
you don't have to specify table name.
129

            
130
=head2 C<update>
131

            
132
    $table->update(param => \%param, where => \%where);
133
    
134
Same as C<update()> of L<DBIx::Custom> except that
135
you don't have to specify table name.
136

            
137
=head2 C<update_all>
138

            
139
    $table->update_all(param => \%param);
140
    
141
Same as C<update_all()> of L<DBIx::Custom> except that
142
you don't have to specify table name.