my $date_constraint = Moose::Util::TypeConstraints::find_type_constrai +nt('MyApp::DateOnly'); around BUILDARGS => sub { my $orig = shift; my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); # Maybe[] doesn't coerce $args{$foo} = $date_constraint->coerce($args{foo}) if defined($args{$foo}); return $class->$orig(%args); }; has foo => ( reader => 'get_foo', writer => '_set_foo', isa => 'Maybe[MyApp::DateOnly]', coerce => 1, handles => { set_foo => sub { my ($self, $arg) = @_; # Maybe[] doesn't coerce $arg = $date_constraint->coerce($arg) if defined($arg); $self->_set_foo($arg); }, }, );
I use MooseX::FollowPBP which creates get_foo and set_foo accessors for is => 'rw'. If you use the default accessor style, adjust appropriately.
As an aside, it seems to me that
type 'MyApp::DateOnly' => where { blessed($_) && $_->isa('DateTime') && $_->hour == 0 && $_->min == 0 && $_->sec == 0 };
would be more clear when written as
subtype 'MyApp::DateOnly' => as 'DateTime' => where { $_->hour == 0 && $_->min == 0 && $_->sec == 0 };
In reply to Re: Moose type question
by ikegami
in thread Moose type question
by Sue D. Nymme
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |