my $date_constraint = Moose::Util::TypeConstraints::find_type_constraint('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);
},
},
);
####
type 'MyApp::DateOnly'
=> where { blessed($_)
&& $_->isa('DateTime')
&& $_->hour == 0 && $_->min == 0 && $_->sec == 0
};
####
subtype 'MyApp::DateOnly'
=> as 'DateTime'
=> where { $_->hour == 0 && $_->min == 0 && $_->sec == 0 };