Copied from http://stackoverflow.com/q/4473327/124486

The below is not as well formatted:

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
Class::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.

-- 
Evan Carroll - m...@evancarroll.com
System Lord of the Internets
web: http://www.evancarroll.com
ph: 281.901.0011

Reply via email to