MooseX::Types coercions and $self
Is there anyway to get $self
into a MooseX::Types
coercion? I have other data in the object that I want to use to seed my coercion from a String
to an Object
. Alternatively, is there anything like C开发者_开发知识库lass::MOP
's initializer
that will permit me to do this -- it would have to fire before the type checks.
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
.
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:
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
.
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".)
People usually solve this problem with around
and/or some combination of BUILD
and BUILDARGS
.
精彩评论