X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-UndefTolerant.git;a=blobdiff_plain;f=lib%2FMooseX%2FUndefTolerant%2FAttribute.pm;h=644611edcfd8c7379f2930f48209855511dbb816;hp=970b70be5122b4a3363da9133d1b6ca49b3e9628;hb=0601096cb90ce43aa10165078a330de93c71e4ca;hpb=d10a4d7f63850b562f9c5267707b9072b84d33f3 diff --git a/lib/MooseX/UndefTolerant/Attribute.pm b/lib/MooseX/UndefTolerant/Attribute.pm index 970b70b..644611e 100644 --- a/lib/MooseX/UndefTolerant/Attribute.pm +++ b/lib/MooseX/UndefTolerant/Attribute.pm @@ -4,22 +4,31 @@ use Moose::Role; around('initialize_instance_slot', sub { my $orig = shift; my $self = shift; - - my $ia = $self->init_arg; - - # $_[2] is the hashref of options passed to the constructor. If our - # parameter passed in was undef, pop it off the args... - pop unless (defined $ia && exists($_[2]->{$ia}) && defined($_[2]->{$ia})); - - # Invoke the real init, as the above line cleared the unef + my ($meta_instance, $instance, $params) = @_; + + my $key_name = $self->init_arg; + + # If our parameter passed in was undef, remove it from the parameter list... + # but leave the value unscathed if the attribute's type constraint can + # handle undef (or doesn't have one, which implicitly means it can) + if (defined $key_name and not defined($params->{$key_name})) + { + my $type_constraint = $self->type_constraint; + if ($type_constraint and not $type_constraint->check(undef)) + { + delete $params->{$key_name}; + } + } + + # Invoke the real init, as the above line cleared the undef param value $self->$orig(@_) }); 1; -=head1 NAME +# ABSTRACT: Make your attribute(s) tolerant to undef intitialization -MooseX::UndefTolerant::Attribute - Make your attribute(s) tolerant to undef intitialization +__END__ =head1 SYNOPSIS @@ -45,21 +54,7 @@ MooseX::UndefTolerant::Attribute - Make your attribute(s) tolerant to undef inti Applying this trait to your attribute makes it's initialization tolerant of of undef. If you specify the value of undef to any of the attributes they -will not be initialized (or will be set to the default, if applicable). +will not be initialized (or will be set to the default, if applicable). Effectively behaving as if you had not provided a value at all. -=head1 AUTHOR - -Cory G Watson, C<< >> - -=head1 COPYRIGHT & LICENSE - -Copyright 2009 Cory G Watson. - -This program is free software; you can redistribute it and/or modify it -under the terms of either: the GNU General Public License as published -by the Free Software Foundation; or the Artistic License. - -See http://dev.perl.org/licenses/ for more information. - =cut