... | ... |
@@ -59,6 +59,33 @@ __PACKAGE__->register_filter( |
59 | 59 |
decode_utf8 => sub { decode_utf8($_[0]) } |
60 | 60 |
); |
61 | 61 |
|
62 |
+our $AUTOLOAD; |
|
63 |
+ |
|
64 |
+sub AUTOLOAD { |
|
65 |
+ my $self = shift; |
|
66 |
+ |
|
67 |
+ # Method |
|
68 |
+ my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
69 |
+ |
|
70 |
+ # Helper |
|
71 |
+ $self->{_helpers} ||= {}; |
|
72 |
+ croak qq/Can't locate object method "$method" via "$package"/ |
|
73 |
+ unless my $helper = $self->{_helpers}->{$method}; |
|
74 |
+ |
|
75 |
+ # Run |
|
76 |
+ return $self->$helper(@_); |
|
77 |
+} |
|
78 |
+ |
|
79 |
+sub helper { |
|
80 |
+ my $self = shift; |
|
81 |
+ |
|
82 |
+ # Merge |
|
83 |
+ my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
84 |
+ $self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers}; |
|
85 |
+ |
|
86 |
+ return $self; |
|
87 |
+} |
|
88 |
+ |
|
62 | 89 |
sub connect { |
63 | 90 |
my $proto = shift; |
64 | 91 |
|
... | ... |
@@ -183,6 +210,8 @@ sub delete { |
183 | 210 |
|
184 | 211 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
185 | 212 |
|
213 |
+sub DESTROY { } |
|
214 |
+ |
|
186 | 215 |
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/; |
187 | 216 |
|
188 | 217 |
sub execute{ |
... | ... |
@@ -1331,6 +1360,24 @@ B<Example:> |
1331 | 1360 |
|
1332 | 1361 |
$dbi->delete_all(table => 'books'); |
1333 | 1362 |
|
1363 |
+=head2 C<(experimental) helper> |
|
1364 |
+ |
|
1365 |
+ $dbi->helper( |
|
1366 |
+ update_or_insert => sub { |
|
1367 |
+ my $self = shift; |
|
1368 |
+ # do something |
|
1369 |
+ }, |
|
1370 |
+ find_or_create => sub { |
|
1371 |
+ my $self = shift; |
|
1372 |
+ # do something |
|
1373 |
+ } |
|
1374 |
+ ); |
|
1375 |
+ |
|
1376 |
+Register helper methods. These method is called from L<DBIx::Custom> object directory. |
|
1377 |
+ |
|
1378 |
+ $dbi->update_or_insert; |
|
1379 |
+ $dbi->find_or_create; |
|
1380 |
+ |
|
1334 | 1381 |
=head2 C<insert> |
1335 | 1382 |
|
1336 | 1383 |
$dbi->insert(table => $table, |
... | ... |
@@ -608,3 +608,26 @@ eval{ $dbi->begin_work }; |
608 | 608 |
ok($@, "$test : exception"); |
609 | 609 |
$dbi->dbh->{AutoCommit} = 1; |
610 | 610 |
|
611 |
+ |
|
612 |
+test 'helper'; |
|
613 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
614 |
+$dbi->helper( |
|
615 |
+ one => sub { 1 } |
|
616 |
+); |
|
617 |
+$dbi->helper( |
|
618 |
+ two => sub { 2 } |
|
619 |
+); |
|
620 |
+$dbi->helper({ |
|
621 |
+ twice => sub { |
|
622 |
+ my $self = shift; |
|
623 |
+ return $_[0] * 2; |
|
624 |
+ } |
|
625 |
+}); |
|
626 |
+ |
|
627 |
+is($dbi->one, 1, "$test : first"); |
|
628 |
+is($dbi->two, 2, "$test : second"); |
|
629 |
+is($dbi->twice(5), 10 , "$test : second"); |
|
630 |
+ |
|
631 |
+eval {$dbi->XXXXXX}; |
|
632 |
+like($@, qr/\QCan't locate object method "XXXXXX" via "DBIx::Custom"/, "$test : not exists"); |
|
633 |
+ |