X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FUndefTolerant%2FAttribute.pm;h=c1065aabcd870280d958965c4e0c242c4fedace9;hb=efcfddbdcfac9bcc3b9a8190f9fe3089199d65ec;hp=e1aee785532aee70daa72c6aefed89c485667324;hpb=5447ee457d5fd503926799cb8efb8552bdb4764d;p=gitmo%2FMooseX-UndefTolerant.git diff --git a/lib/MooseX/UndefTolerant/Attribute.pm b/lib/MooseX/UndefTolerant/Attribute.pm index e1aee78..c1065aa 100644 --- a/lib/MooseX/UndefTolerant/Attribute.pm +++ b/lib/MooseX/UndefTolerant/Attribute.pm @@ -1,15 +1,65 @@ package MooseX::UndefTolerant::Attribute; use Moose::Role; -around('initialize_instance_slot', sub{ +around('initialize_instance_slot', sub { my $orig = shift; my $self = shift; - # If the parameter passed in was undef, quietly do nothing but return - return unless defined($_->[2]); + my $ia = $self->init_arg; + + # $_[2] is the hashref of options passed to the constructor. If our + # parameter passed in was undef, quietly do nothing but return. + return unless exists($_[2]->{$ia}) && defined($_[2]->{$ia}); # If it was defined, call the real init slot method $self->$orig(@_) }); -1; \ No newline at end of file +1; + +=head1 NAME + +MooseX::UndefTolerant::Attribute - Make your attribute tolerant to undef intitialization + +=head1 SYNOPSIS + + package My:Class; + use Moose; + + use MooseX::UndefTolerant::Attribute; + + has 'bar' => ( + traits => [ qw(MooseX::UndefTolerant::Attribute)], + is => 'ro', + isa => 'Num', + predicate => 'has_bar' + ); + + # Meanwhile, under the city... + + # Doesn't explode + my $class = My::Class->new(bar => undef); + $class->has_bar # False! + +=head1 DESCRIPTION + +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. 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