【发布时间】:2011-05-24 09:50:43
【问题描述】:
让我们看看SO问题进入机器人预测,显然是根据问题标题发布的,是否会成真:
您提出的问题看起来很主观,很可能已经结束。
使用 Perl/Moose,我想弥补商家文章的两种表示方式之间的不匹配。让一篇文章有name、quantity和price。第一种表示方式是将数量设置为任何数值,包括十进制值,因此您可以拥有 3.5 米长的绳索或电缆。我必须与之交互的第二个是,唉,不灵活,并且要求quantity 是一个整数。因此,我必须重写我的对象以将 quantity 设置为 1,并将实际数量包含在 name 中。 (是的,这是一个 hack,但我想让示例保持简单。)
所以这里的故事是一个属性的值会影响其他属性的值。
这是工作代码:
#!perl
package Article;
use Moose;
has name => is => 'rw', isa => 'Str', required => 1;
has quantity => is => 'rw', isa => 'Num', required => 1;
has price => is => 'rw', isa => 'Num', required => 1;
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my %args = @_ == 1 ? %{$_[0]} : @_;
my $q = $args{quantity};
if ( $q != int $q ) {
$args{name} .= " ($q)";
$args{price} *= $q;
$args{quantity} = 1;
}
return $class->$orig( %args );
};
sub itemprice { $_[0]->quantity * $_[0]->price }
sub as_string {
return sprintf '%2u * %-40s (%7.2f) %8.2f', map $_[0]->$_,
qw/quantity name price itemprice/;
}
package main;
use Test::More;
my $table = Article->new({ name => 'Table', quantity => 1, price => 199 });
is $table->itemprice, 199, $table->as_string;
my $chairs = Article->new( name => 'Chair', quantity => 4, price => 45.50 );
is $chairs->itemprice, 182, $chairs->as_string;
my $rope = Article->new( name => 'Rope', quantity => 3.5, price => 2.80 );
is $rope->itemprice, 9.80, $rope->as_string;
is $rope->quantity, 1, 'quantity set to 1';
is $rope->name, 'Rope (3.5)', 'name includes original quantity';
done_testing;
不过,我想知道在 Moose 中是否有更好的习惯用法。但也许我的问题都是主观的,应该迅速结束。 :-)
根据 perigrin 的回答更新
我已经修改了 perigrin 的代码示例(小错误和 5.10 语法)并将我的测试标记到它的末尾:
package Article::Interface;
use Moose::Role;
requires qw(name quantity price);
sub itemprice { $_[0]->quantity * $_[0]->price }
sub as_string {
return sprintf '%2u * %-40s (%7.2f) %8.2f', map $_[0]->$_,
qw/quantity name price itemprice/;
}
package Article::Types;
use Moose::Util::TypeConstraints;
class_type 'Article::Internal';
class_type 'Article::External';
coerce 'Article::External' =>
from 'Article::Internal' => via
{
Article::External->new(
name => sprintf( '%s (%s)', $_->name, $_->quantity ),
quantity => 1,
price => $_->quantity * $_->price
);
};
package Article::Internal;
use Moose;
use Moose::Util::TypeConstraints;
has name => isa => 'Str', is => 'rw', required => 1;
has quantity => isa => 'Num', is => 'rw', required => 1;
has price => isa => 'Num', is => 'rw', required => 1;
my $constraint = find_type_constraint('Article::External');
=useless for this case
# Moose::Manual::Construction - "You should never call $self->SUPER::BUILD,
# nor"should you ever apply a method modifier to BUILD."
sub BUILD {
my $self = shift;
my $q = $self->quantity;
# BUILD does not return the object to the caller,
# so it CANNOT BE USED to trigger the coercion.
return $q == int $q ? $self : $constraint->coerce( $self );
}
=cut
with qw(Article::Interface); # need to put this at the end
package Article::External;
use Moose;
has name => isa => 'Str', is => 'ro', required => 1;
has quantity => isa => 'Int', is => 'ro', required => 1;
has price => isa => 'Num', is => 'ro', required => 1;
sub itemprice { $_[0]->price } # override
with qw(Article::Interface); # need to put this at the end
package main;
use Test::More;
my $table = Article::Internal->new(
{ name => 'Table', quantity => 1, price => 199 });
is $table->itemprice, 199, $table->as_string;
is $table->quantity, 1;
is $table->name, 'Table';
my $chairs = Article::Internal->new(
name => 'Chair', quantity => 4, price => 45.50 );
is $chairs->itemprice, 182, $chairs->as_string;
is $chairs->quantity, 4;
is $chairs->name, 'Chair';
my $rope = Article::Internal->new(
name => 'Rope', quantity => 3.5, price => 2.80 );
# I can trigger the conversion manually.
$rope = $constraint->coerce( $rope );
# I'd like the conversion to be automatic, though.
# But I cannot use BUILD for doing that. - XXX
# Looks like I'd have to add a factory method that inspects the
# parameters and does the conversion if needed, and it is always
# needed when the `quantity` isn't an integer.
isa_ok $rope, 'Article::External';
is $rope->itemprice, 9.80, $rope->as_string;
is $rope->quantity, 1, 'quantity set to 1';
is $rope->name, 'Rope (3.5)', 'name includes original quantity';
done_testing;
我同意它提供了更好的关注点分离。另一方面,我不相信这对我的目的来说是一个更好的解决方案,因为它增加了复杂性并且不提供自动转换(为此我必须添加更多代码)。
【问题讨论】:
-
我很困惑,为什么你不能总是将数量存储为一个数字,而只是有一个在必要时将数字作为一个整数返回的方法?
-
@perigrin - 类的目的是符合始终要求
quantity为整数且从不的服务允许它是其他任何东西。另一方面,我想让它使用起来尽可能方便,这意味着我应该能够将它与它应该工作的系统中的数据一起使用。所以它是从我们的文章数据到他们的文章数据的桥梁(或转换器)。这种转变只需要以一种方式起作用,从我们的代表到他们的。 -
@perigrin - 关于你的问题,这里的重点是像
quantity这样的十进制数会影响多个属性,或者,正如我上面写的,“一个属性的值会影响其他属性”价值观。”因此,如果我存储了原始值,那么我必须有一个转换方法输出(到外部服务),但为了简单起见,我的想法是有一个转换 关于输入,我不在乎我是否会以这种方式丢失信息,因为该类的目的是单向数据适应。