Permalink
Browse files

Merge pull request #80 from yappo/master

fixed #78, #79 And column method accept setter values
  • Loading branch information...
2 parents cebd6c9 + b56f466 commit 51868b2ea0de2552a99122efb325dfaab403f294 @nekokak committed Oct 5, 2012
Showing with 58 additions and 8 deletions.
  1. +5 −0 Changes
  2. +13 −1 lib/Teng.pm
  3. +10 −6 lib/Teng/Row.pm
  4. +1 −1 lib/Teng/Schema/Table.pm
  5. +13 −0 t/001_basic/015_row_class.t
  6. +16 −0 t/002_common/002_update.t
View
5 Changes
@@ -1,5 +1,10 @@
Revision history for Perl extension Teng
+0.16
+ - row column method accept setter values. (thaks yappo)
+ - rename Row class method _lazy_get_data to generate_column_accessor. (thaks yappo)
+ - FIXED: AUTOLOAD row column method's uncached bug. (thaks yappo)
+
0.15
- stop mode:fixup support. (thanks nihen)
- refactoring load_plugin. (thanks ktat)
View
14 lib/Teng.pm
@@ -661,7 +661,7 @@ in your script.
id => 1,
}
);
- $row->update({name => 'nekokak'});
+ $row->update({name => 'nekokak'}); # same do { $row->name('nekokak'); $row->update; }
$row = $teng->single_by_sql(q{SELECT id, name FROM user WHERE id = ?}, [ 1 ]);
$row->delete();
@@ -852,6 +852,18 @@ You can also call update on a row object:
my $row = $teng->single('user',{id => 1});
$row->update({name => 'nomaneko'});
+You can use the set_column method:
+
+ my $row = $teng->single('user', {id => 1});
+ $row->set_column( name => 'yappo' );
+ $row->update;
+
+you can column update by using column method:
+
+ my $row = $teng->single('user', {id => 1});
+ $row->name('yappo');
+ $row->update;
+
=item $delete_row_count = $teng->delete($table, \%delete_condition)
Deletes the specified record(s) from C<$table> and returns the number of rows deleted. You may optionally specify C<%delete_condition> to create a conditional delete query.
View
16 lib/Teng/Row.pm
@@ -8,8 +8,9 @@ sub new {
my ($class, $args) = @_;
my $self = bless {
- _get_column_cached => {},
- _dirty_columns => {},
+ _get_column_cached => {},
+ _dirty_columns => {},
+ _autoload_column_cache => {},
%$args,
}, $class;
@@ -19,12 +20,14 @@ sub new {
$self;
}
-sub _lazy_get_data {
+sub generate_column_accessor {
my ($x, $col) = @_;
return sub {
my $self = shift;
+ return $self->set_column( $col => @_ ) if @_;
+
# "Untrusted" means the row is set_column by scalarref.
# e.g.
# $row->set_column("date" => \"DATE()");
@@ -33,7 +36,7 @@ sub _lazy_get_data {
}
my $cache = $self->{_get_column_cached};
my $data = $cache->{$col};
- if (! $data) {
+ if (! $data) {
$data = $cache->{$col} = $self->{table} ? $self->{table}->call_inflate($col, $self->get_column($col)) : $self->get_column($col);
}
return $data;
@@ -174,10 +177,11 @@ sub _where_cond {
}
}
-sub AUTOLOAD{
+# for +columns option by some search methods
+sub AUTOLOAD {
my $self = shift;
my($method) = ($AUTOLOAD =~ /([^:']+$)/);
- $self->_lazy_get_data($method)->($self);
+ ($self->{_autoload_column_cache}{$method} ||= $self->generate_column_accessor($method))->($self);
}
### don't autoload this
View
2 lib/Teng/Schema/Table.pm
@@ -32,7 +32,7 @@ sub new {
for my $col (@{$self->columns}) {
no strict 'refs';
unless ($row_class->can($col)) {
- *{"$row_class\::$col"} = $row_class->_lazy_get_data($col);
+ *{"$row_class\::$col"} = $row_class->generate_column_accessor($col);
}
}
$self->row_class($row_class);
View
13 t/001_basic/015_row_class.t
@@ -93,10 +93,23 @@ subtest 'handle' => sub {
can_ok $row->handle, 'single';
};
+subtest 'your row class AUTOLOAD' => sub {
+ my $row = $db_basic_row->single('mock_basic_row',{id => 1},{'+columns' => [\'id+10 as id_plus_ten']});
+ isa_ok $row, 'Mock::BasicRow::Row::MockBasicRow';
+ is $row->foo, 'foo';
+ is $row->id, 1;
+ is $row->name, 'perl';
+ is $row->id_plus_ten, 11;
+
+ ok $row->can('id');
+ ok ! $row->can('mock_basic_id');
+};
+
subtest 'AUTOLOAD' => sub {
my $row = $db_basic->search_by_sql(q{select id as mock_basic_id from mock_basic where id = 1})->next;
isa_ok $row, 'Teng::Row';
is $row->mock_basic_id, 1;
+ ok ! $row->can('mock_basic_id');
};
subtest 'can not use (update|delete) method' => sub {
View
16 t/002_common/002_update.t
@@ -101,5 +101,21 @@ subtest 'empty update' => sub {
is $row->name, 'perl';
};
+subtest 'update by setter column' => sub {
+ my $row = $db->single('mock_basic',{
+ id => 1,
+ });
+ is $row->name, 'perl';
+
+ $row->name('tora');
+ is $row->update, 1;
+ is $row->name, 'tora';
+
+ my $row2 = $db->single('mock_basic',{
+ id => 1,
+ });
+ is $row2->name, 'tora';
+};
+
done_testing;

0 comments on commit 51868b2

Please sign in to comment.