Perl/Moose 对象模型和属性检查

标签 perl moose

再次遇到 Moose 对象模型的问题。我不确定是在这里发帖还是发到“codereview”更好 - 先在这里尝试...;)

  • 有一个Region
  • 该地区有一些 Spot(s)。
  • 每个 Spot 都有 name 和一些 Entries
  • 每个条目都有:idcontentflag

API 应该像下一个一样工作:

my $region = Region->new;    #create a Region
my $sx = $region->spot('x'); #return the Spot with a name 'x'
                             #if the spot with a name 'x' doesn't exists, create it

$sx->add(id => 'id1', content => 'content1', flag => 'f1');   #add a new Entry to Spot 'x'
$sx->add(id => 'id2', content => sub { return "string" }, flag => 'f2');#add another Entry

$sx->render; # render the "spot" - do some operations on array of Entries

我已经拥有的 - 对冗长的资源感到抱歉 - 我将它们缩短为最小的工作包。

首先 - 为“条目”创建一个简单的类。

package Region::Entry;
use namespace::sweep;
use Modern::Perl;
use Moose;

has 'id'      => (is => 'rw', isa => 'Str');
has 'content' => (is => 'rw', isa => 'Str|CodeRef');
has 'flag'    => (is => 'rw', isa => 'Str');

__PACKAGE__->meta->make_immutable;
1;

Region 类 - 只有一个 HashRef 用于按 spotname(作为键)存储 Spot

package Region;
use namespace::sweep;
use Modern::Perl;
use Moose;
use Method::Signatures::Simple;
use Region::Spot;

has 'spots' => (
    traits    => ['Hash'],
    is        => 'rw',
    isa       => 'HashRef[Region::Spot]',
    default   => sub { {} },
    handles   => {
        set_spot     => 'set',
        get_spot     => 'get',
        spot_exists  => 'exists',
    },
);

method spot {
    my $name = shift;
    return undef unless $name;
    $self->set_spot($name => Region::Spot->new()) unless $self->spot_exists($name);
    return $self->get_spot($name);
}
__PACKAGE__->meta->make_immutable;
1;

终于为 Spots 上课了

package Region::Spot;
use Modern::Perl;
use namespace::sweep;
use Moose;
use Method::Signatures::Simple;
use Region::Entry;

has 'entries' => (
    traits  => ['Array'],
    is => 'rw',
    isa => 'ArrayRef[Region::Entry]',
    default => sub { [] },
    handles => {
        add_entry => 'push',
    },
);

#PROBLEM HERE - HOW TO IMPLEMENT THIS for allow attributes as in the above API
#so add(id=>'id', content => 'xxxx', flag => 'f');
#with correct attribute checking????
method add {
    #$self->add_entry....
}

method render {
    #...
    return "rendered entries";
}
__PACKAGE__->meta->make_immutable;
1;

因此,在实现 add API 时遇到问题。看上面的评论...

$sx->add(id => 'id1', content => 'content1', flag => 'f1');   #add a new Entry to Spot 'x'

我的对象模型或其实现可能是错误的,因为我在没有定义类型的类上有 add 方法......或者不知道......

每次创建新内容时,这都是我的问题。我知道“如何使用 Moose”(我不是专家,但了解基础知识)- 但在定义正确的类时遇到问题...:(

我希望上面的内容是有道理的...很抱歉这篇文章太长了。

最佳答案

我可能遗漏了您问题的一些细微细节,但为什么不解压缩您的 add 方法中的参数并创建一个新的 Entry

method add {
    $self->add_entry( Entry->new( @_ ) );
}

关于Perl/Moose 对象模型和属性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22416833/

相关文章:

perl - 如何在不中继perd中的stdout的情况下做system()

perl - 作为智能匹配操作的结果获取数组的索引?

perl - MooseX::Declare 如何从属性默认方法返回 ArrayRef?

perl - Moose 深度强制 - 用户定义类型的 ArrayRef

perl - 为什么先销毁包含的对象?

perl - 我可以在我的主目录中安装预先配置的 Perl 二进制包吗?

linux - 不一致的 CPAN 模块行为——该怪谁?

perl - 寻求避免 "action at a distance"的模式

perl - 如何在角色中使用 MooseX::ClassAttribute?

perl - 有关于迁移到 Moose 的非常好的网络资源吗?