我可以在基类中使用 Moose 中的属性修饰符来处理来自子类的多个属性吗? [英] Can I use an attribute modifer in Moose in a base class to handle multiple attributes from sub classes?

查看:46
本文介绍了我可以在基类中使用 Moose 中的属性修饰符来处理来自子类的多个属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个模块具有许多接受日期时间相关的属性日期戳并返回一个人类可读的字符串( dd Mon yyyy hh:mm:ss ).

I've got several Modules that have a number of date time related attributes that accept a datestamp and return a human readable string ( dd Mon yyyy hh:mm:ss ).

package ModuleOne;

use Moose;

extends 'ModuleBase';

has date_attr_one => ( ... );

然后……

package ModuleTwo;

use Moose;

extends 'ModuleBase';

has date_attr_mouse => ( ... );

由于它目前正在工作,我正在使用周围"属性修饰符,以便如果有没有参数返回格式为03 May 2012 12:33:42"的日期.如果有一个'03 May 2012 12:33:42' 格式的参数,我想将其设置为日期戳.

As it's currently working I'm using an 'around' attribute modifier so that if there's no parameter return the a date in the format of '03 May 2012 12:33:42'. If there is a parameter in the format of '03 May 2012 12:33:42' the I want to set that as a datestamp.

因此,在 ModuleOne 中,将有:

So, in ModuleOne, there would be:

has date_attr_one => ( ... );

紧随其后:

around 'date_attr_one' => sub { ... }

相同类型的事情发生在第二个模块中.

Same type of thing occurs in the second module.

我遇到的问题是有多个模块在执行此操作,并且每个模块都是使用around"修饰符和相同的重复代码.我想将它的修饰符移到基类中,以便扩展基类的每个模块都可以使用该修饰符.我尝试使用正则表达式将其放入基类,例如:(在基类中)

The problem I'm having is that there are multiple modules doing this and each of them is using the 'around' modifier and the same duplicated code. I'd like to move that around modifier into the base class so every Module that extends the base class can use that modifier. I tried putting it into the base class using a regex like: (in the base class)

around qr/date_attr_one/ => sub { ... }

我在那里放了一些从未执行过的打印语句.角色不允许这样的事情.

I put some print statements in there that never executed. Roles don't allow such a thing.

有没有办法将修饰符移动到基类中,以便每个扩展基类的模块都可以使用该修饰符,其中每个模型中的属性具有不同的名称?

Is there a way to move that around modifier into the base class so every Module that extends the base class can use that modifier where the attributes would have different names in each model?

因此,如上例所示,基类的 around 属性修饰符需要处理 $self->date_attr_one 和 $self->date_attr_mouse 等.

So, as in the example above, the base class' around attribute modifier would need to handle $self->date_attr_one and $self->date_attr_mouse, among others.

推荐答案

如果你创建了一个像下面这样的包,你在你的类中只需要:

If you create a package like the one below, all you need in your classes is:

has date => (
   is     => 'rw',
   isa    => 'My::Date',
   coerce => 1,
);

示例代码:

my $o = My::Class->new(date => '03 May 2012 12:33:42');
say $o->date();
say 0+$o->date();

$o->date(1336062822);
say $o->date();
say 0+$o->date();

上述模块:

package My::Date;

use strict;
use warnings;

use Moose;
use Moose::Util::TypeConstraints;

use DateTime::Format::Strptime qw( );
use POSIX                      qw( strftime );

my $format = DateTime::Format::Strptime->new(
   pattern   => '%d %b %Y %H:%M:%S',
   locale    => 'en_US',
   time_zone => 'local',
   on_error  => 'croak',
);

has epoch => (
   is       => 'rw',
   isa      => 'Num',
   required => 1,
);

sub as_string {
   return strftime('%d %b %Y %H:%M:%S', localtime( $_[0]->epoch ));
}

coerce __PACKAGE__,
   from 'Num',
      via { __PACKAGE__->new( epoch => $_ ) },
   from 'Str',
      via { __PACKAGE__->new( epoch => $format->parse_datetime($_)->epoch ) };

use overload (
   '""' => sub { $_[0]->as_string },
   '0+' => sub { $_[0]->epoch },
   fallback => 1,
);

1;

这篇关于我可以在基类中使用 Moose 中的属性修饰符来处理来自子类的多个属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆