Newer Older
145 lines | 2.935kb
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 experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
10
# Carp trust relationship
11
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
12

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

            
15
our $AUTOLOAD;
16

            
17
sub AUTOLOAD {
18
    my $self = shift;
19

            
20
    # Method
21
    my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
22

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

            
28
    # Run
29
    return $self->$helper(@_);
30
}
31

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

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

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

            
59
1;
60

            
61
=head1 NAME
62

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

            
65
=head1 SYNOPSIS
66

            
67
use DBIx::Custom::Table;
68

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

            
71
=head1 METHODS
72

            
73
L<DBIx::Custom> inherits all methods from L<Object::Simple>
74
and implements the following new ones.
75

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

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

            
83
=head2 C<delete_all>
84

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

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

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

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

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

            
107
=head2 C<method>
108

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

            
118
Define method.
119

            
120
=head2 C<new>
121

            
122
    my $table = DBIx::Custom::Table->new;
123

            
124
Create a L<DBIx::Custom::Table> object.
125

            
126
=head2 C<select>
127

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

            
133
=head2 C<update>
134

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

            
140
=head2 C<update_all>
141

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