[英]MooseX::Types coercions and $self
Is there anyway to get $self
into a MooseX::Types
coercion? 反正是为了让
$self
进入MooseX::Types
强制? I have other data in the object that I want to use to seed my coercion from a String
to an Object
. 我在对象中有其他数据,我想用它来强制从
String
到Object
。 Alternatively, is there anything like Class::MOP
's initializer
that will permit me to do this -- it would have to fire before the type checks. 或者,是否有类似
Class::MOP
的initializer
程序允许我这样做 - 它必须在类型检查之前触发。
Requested pseudo code: 请求的伪代码:
with 'DBHandle';
has 'database' => ( isa => 'Str', is => 'ro', default => 'Db' );
has 'schema' => ( isa => 'Str', is => 'ro', default => 'schema' );
has 'table' => ( isa => 'Str', is => 'ro', default => 'column );
has 'columns' => ( isa => DBCols, is => 'ro', default => sub {[qw/foo bar baz/]} );
Here, I want "columns" to coerce to a DBCols
-- an ArrayRef of DBCol's (objects) -- requiring the use of catalog
, schema
, and col
attributes found in the class, and with a dbh/singleton provided by DBHandle
. 在这里,我希望“columns”强制转换为
DBCols
- DBCol(对象)的ArrayRef - 需要使用类中找到的catalog
, schema
和col
属性,以及DBHandle
提供的dbh / singleton。
To make this less-pseudo, the actually situation is only slightly more complex. 为了减少伪,实际情况只是稍微复杂一些。 I was able to accomplish the above with
around
, now what I want I to do is create an attribute trait that would permit this syntax: 我能够完成上述与
around
,现在我想我做的就是创建一个属性特征,将允许这个语法:
has 'column_id' => (
isa => Int
, is => 'ro'
, traits => ['DBKey']
, default => 5
, column => 'foo'
);
Where the attribute trait column
provided by DBKey
, coerces to DBCol
the same way that the above columns
would: this would require the ability to access the classes database
, schema
, table
, and again the singleton for the dbh
. DBKey
提供的属性trait column
以与上述columns
相同的方式DBKey
为DBCol
:这将需要能够访问类database
, schema
, table
以及dbh
的单例。
No. It'd be nice, but coercions are really designed to be global, and no one has written a "context-sensitive coercion" yet, because no one's really sure how to. 不,这很好,但强制实际上是设计为全球性的,没有人写过“上下文敏感的强制”,因为没有人真的确定如何。 (Actually, s/coercions/type constraints/ -- it'd be useful just to say "this Str must be a valid column name, defined as an entry in this object's
columns
HashRef".) (实际上,s / coercions / type constraints / - 只是说“这个Str必须是一个有效的列名,在这个对象的
columns
HashRef中被定义为条目”。)
People usually solve this problem with around
and/or some combination of BUILD
and BUILDARGS
. 人们通常使用
BUILD
和BUILDARGS
around
和/或某些组合来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.