我想将我的 Moose 类中的属性的 Str 强制转换为 DBIx::Class::Row 对象。为此,我需要对 DBIC 架构执行查找以查找该行。如果查找失败,我想将错误推送到 ArrayRef 属性上。
我目前将架构作为属性传递给我的类。
通过强制,我似乎无法访问该对象,因此无法推送错误 arrayref 属性或使用架构对象来执行查找。
我尝试的另一种方法是在设置时使用“around”来查找和设置属性,但是当属性值通过构造函数传递时,这当然不会被调用。
这可能吗,或者有人有替代实现来实现我想要实现的目标吗?
I want to coerce a Str into a DBIx::Class::Row object for an attribute in my Moose class. To do this I need to perform a lookup on the DBIC schema to find the row. I want to push an error onto an ArrayRef attribute if the lookup failed.
I currently pass the schema in as an attribute to my class.
With coercion it appears I cannot access the object so I cannot push onto the error arrayref attribute or use the schema object to perform my lookup.
An alternative I tried was to use 'around' to lookup and set the attribute when set, but this of course does not get called when the attribute value is passed via the constructor.
Is this possible, or does someone have an alternative implementation to do what I want to achieve?
发布评论
评论(1)
您可以捕获并改变使用属性初始值设定项传递到构造函数时存储的值。 (但是,它仅在构造函数中设置属性时运行,而不是在任何其他时间运行。)可以在 类::MOP::属性。
由于这仅捕获通过构造函数设置属性的情况,因此您仍然需要捕获设置属性的其他情况。正如您所说,这可以使用方法修饰符来完成,但是您可以将两者合并为一个方法,包裹在自动生成的访问器周围:
You can catch and mutate a value being stored when passed into a constructor with an attribute initializer. (However, it is only run when the attribute is set in the constructor, not at any other time.) Documentation for initializers can be found in Class::MOP::Attribute.
Since this only catches cases where the attribute is set via the constructor, you still need to catch the other cases where the attribute is set. This can be done with a method modifier as you said, but you can combine the two into one method, wrapped around the autogenerated accessor: