package Foo::Types::Library;
use Data::Dumper;
use MooseX::Types -declare => [
qw/
GooglePrimaryEmail
WWWGoogleContactsTypeEmail
/
];
use MooseX::Types::Email qw/EmailAddress/;
# WWWGoogleContactsTypeEmail
class_type WWWGoogleContactsTypeEmail, {
class => 'WWW::Google::Contacts::Type::Email',
};
# GooglePrimaryEmail
subtype GooglePrimaryEmail,
as EmailAddress,
message {
'validation failed for: '. Dumper $_;
};
coerce GooglePrimaryEmail,
from ArrayRef[WWWGoogleContactsTypeEmail],
via {
my $p = (grep { $_->{primary} } @$_)[0];
$p = $p ? $p->{value} : @$_->[0]{value};
return $p;
};
This works for something like:
use MooseX::Declare;
class Foo {
use Foo::Types::Library qw /GooglePrimaryEmail/;
has 'email' => (
is => 'rw',
isa => GooglePrimaryEmail,
coerce => 1
);
}
Works fine unless email is initialized undef. In this case the attribute check fails.
Now maybe solves this but I don't know how I can create my own Maybe. I use MooseX::Types for creating my own types. As I can't see any syntax to create my own Maybe type I used the one in Moose::Util::TypeConstraints:
maybe_type (GooglePrimaryEmail);
I added this line to my type library, exported it with declare as Maybe[GooglePrimaryEmail and tested it with:
use MooseX::Declare;
class Foo {
use Foo::Types::Library qw /Maybe[GooglePrimaryEmail]/;
has 'email' => (
is => 'rw',
isa => 'Maybe[GooglePrimaryEmail]',
coerce => 1
);
}
Now every attribute type check fails. :(
Any help? |