in reply to Re: MooseX::Declare and undef attributes - MooseX::UndefTolerant not working
in thread MooseX::Declare and undef attributes - MooseX::UndefTolerant not working
I think maybe does the job. For the standard Moose types it works well like isa => 'Maybe[Str]'. The problem is I created some subtypes for my needs.
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:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: MooseX::Declare and undef attributes - MooseX::UndefTolerant not working
by ikegami (Patriarch) on Sep 26, 2010 at 16:40 UTC | |
by err (Novice) on Sep 26, 2010 at 22:23 UTC | |
by ikegami (Patriarch) on Sep 26, 2010 at 22:39 UTC |