Sue D. Nymme has asked for the wisdom of the Perl Monks concerning the following question:
I have a type constraint I've set up, called 'MyApp::DateOnly'. It's for a DateTime object with certain restrictions.
type 'MyApp::DateOnly' => as 'DateTime' => where { blessed($_) && $_->isa('DateTime') && $_->hour == 0 && $_->min == 0 && $_->sec == 0 };
I have also included a couple of simple coercions, so that the user can pass a properly-formatted string as a convenience, and so that DateTime objects are made to follow the limitations of the type:
coerce 'Search::DateOnly' => from 'DateTime' => via { $_->truncate(to => 'day') }; coerce 'Search::DateOnly' => from 'Str' => via { state $parser = DateTime::Format::Strptime->new( pattern => '%Y/%m/%d'); $parser->parse_datetime($_); };
This all works fine.
Now I need to allow undef as a null value for certain attributes. I thought I could set up such an attribute like this:
has foo => ( is => 'ro', isa => 'Maybe[MyApp::DateOnly]', coerce => 1, );
but it doesn't work -- not only does it fail on undef arguments, it won't even accept and coerce the Str or DateTime arguments it did before! Is Maybe[] processing not done automatically? How do I enable it for this type constraint?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose type question
by ikegami (Patriarch) on Jun 07, 2010 at 18:27 UTC | |
by Sue D. Nymme (Monk) on Jun 07, 2010 at 19:12 UTC | |
by ikegami (Patriarch) on Jun 07, 2010 at 20:28 UTC | |
|
Re: Moose type question
by stvn (Monsignor) on Jun 14, 2010 at 20:37 UTC | |
by ikegami (Patriarch) on Jun 14, 2010 at 20:52 UTC | |
by stvn (Monsignor) on Jun 14, 2010 at 21:26 UTC | |
by ikegami (Patriarch) on Jun 14, 2010 at 22:13 UTC | |
by ikegami (Patriarch) on Jun 15, 2010 at 06:12 UTC | |
by stvn (Monsignor) on Jun 15, 2010 at 14:49 UTC | |
by ikegami (Patriarch) on Jun 15, 2010 at 15:38 UTC | |
by stvn (Monsignor) on Jun 15, 2010 at 17:01 UTC | |
| |
by ikegami (Patriarch) on Jun 14, 2010 at 22:06 UTC |