免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1481 | 回复: 0
打印 上一主题 下一主题

谁哪分析一下这段代码 MT [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-08 11:46 |只看该作者 |倒序浏览
package MT::Object;
use strict;

use MT::ObjectDriver;
use MT::ErrorHandler;
@MT::Object::ISA = qw( MT::ErrorHandler );

## Magic.

sub install_properties {
    my $class = shift;
    no strict 'refs';
    ${"${class}::__properties"} = shift;
}

sub properties {
    my $this = shift;
    my $class = ref($this) || $this;
    no strict 'refs';
    ${"${class}::__properties"};
}

## Drivers.

use vars qw( $DRIVER );
sub set_driver { $DRIVER = MT::ObjectDriver->new($_[1]) }
sub driver { $DRIVER }

## Construction/initialization.

sub new {
    my $class = shift;
    my $obj = bless {}, $class;
    $obj->init(@_);
}

sub init {
    my $obj = shift;
    my %arg = @_;
    $obj->{'column_values'} = {};
    $obj;
}

sub clone {
    my $obj = shift;
    my $clone = ref($obj)->new();
    $clone->set_values($obj->column_values);
    $clone;
}

sub column_names {
    my $obj = shift;
    my $props = $obj->properties;
    my @cols = @{ $props->{columns} };
    push @cols, qw( created_on created_by modified_on modified_by )
        if $props->{audit};
    \@cols;
}

sub datasource { $_[0]->properties->{datasource} }

sub column_values { $_[0]->{'column_values'} }

sub column {
    my $obj = shift;
    my($col, $value) = @_;
    return unless defined $col;
    $obj->{'column_values'}->{$col} = $value if defined $value;
    $obj->{'column_values'}->{$col};
}

sub set_values {
    my $obj = shift;
    my($values) = @_;
    my @cols = @{ $obj->column_names };
    for my $col (@cols) {
        next unless exists $values->{$col};
        $obj->column($col, $values->{$col});
    }
}

sub _mk_passthru {
    my($method) = @_;
    sub {
        my($obj) = $_[0];
        die "No ObjectDriver defined" unless defined $DRIVER;
        if (wantarray) {
            my @rc = $DRIVER->$method(@_);
            @rc or return $obj->error( $DRIVER->errstr );
            return @rc;
        } else {
            my $rc = $DRIVER->$method(@_);
            defined $rc or return $obj->error( $DRIVER->errstr );
            return $rc;
        }
    }
}

{
    no strict 'refs';
    *load = _mk_passthru('load');
    *load_iter = _mk_passthru('load_iter');
    *save = _mk_passthru('save');
    *remove = _mk_passthru('remove');
    *remove_all = _mk_passthru('remove_all');
    *exists = _mk_passthru('exists');
    *count = _mk_passthru('count');
}

sub DESTROY { }

use vars qw( $AUTOLOAD );
sub AUTOLOAD {
    my $obj = $_[0];
    (my $col = $AUTOLOAD) =~ s!.+::!!;
    no strict 'refs';
    my $class = ref($obj);
    *$AUTOLOAD = sub {
        shift()->column($col, @_);
    };
    goto &$AUTOLOAD;
}

1;
__END__
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP