Skip to content

Commit

Permalink
fixed issue #79, cached AUTOLOAD column method code-refs
Browse files Browse the repository at this point in the history
  • Loading branch information
yappo committed Oct 2, 2012
1 parent 0051bd3 commit a7b640e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/Teng/Row.pm
Expand Up @@ -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;

Expand Down Expand Up @@ -176,10 +177,11 @@ sub _where_cond {
}
}

sub AUTOLOAD{
# for +columns option by some search methods
sub AUTOLOAD {
my $self = shift;
my($method) = ($AUTOLOAD =~ /([^:']+$)/);
$self->generate_column_accessor($method)->($self);
($self->{_autoload_column_cache}{$method} ||= $self->generate_column_accessor($method))->($self);
}

### don't autoload this
Expand Down
13 changes: 13 additions & 0 deletions t/001_basic/015_row_class.t
Expand Up @@ -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 {
Expand Down

0 comments on commit a7b640e

Please sign in to comment.