From: Cory Watson Date: Sat, 23 Jan 2010 20:22:41 +0000 (-0600) Subject: Make default work properly. X-Git-Tag: 0.05~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=779ca481dfc39471fbcbe33772436cf72daac821;p=gitmo%2FMooseX-UndefTolerant.git Make default work properly. --- diff --git a/Changes b/Changes index e00ea87..3bb443e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for MooseX-UndefTolerant +0.04 January 23, 2010 + * Rather than skipping initialization all together, just pop off the undef + and let the init happen normally. This makes default values work + properly. (Test provided by (and thanks to) Tom Heady) + 0.03 December 3, 2009 * Fiddle with dependencies. diff --git a/MANIFEST b/MANIFEST index 2fcfb13..c712eda 100644 --- a/MANIFEST +++ b/MANIFEST @@ -16,3 +16,4 @@ META.yml README t/00-load.t t/attribute.t +t/defaults.t diff --git a/lib/MooseX/UndefTolerant.pm b/lib/MooseX/UndefTolerant.pm index 86e115c..b801bdf 100644 --- a/lib/MooseX/UndefTolerant.pm +++ b/lib/MooseX/UndefTolerant.pm @@ -5,7 +5,7 @@ use Moose::Exporter; use MooseX::UndefTolerant::Attribute; -our $VERSION = '0.03'; +our $VERSION = '0.04'; Moose::Exporter->setup_import_methods( attribute_metaclass_roles => [ 'MooseX::UndefTolerant::Attribute' ] diff --git a/lib/MooseX/UndefTolerant/Attribute.pm b/lib/MooseX/UndefTolerant/Attribute.pm index c1065aa..c5c3579 100644 --- a/lib/MooseX/UndefTolerant/Attribute.pm +++ b/lib/MooseX/UndefTolerant/Attribute.pm @@ -8,10 +8,10 @@ around('initialize_instance_slot', sub { 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}); + # parameter passed in was undef, pop it off the args... + pop unless (exists($_[2]->{$ia}) && defined($_[2]->{$ia})); - # If it was defined, call the real init slot method + # Invoke the real init, as the above line cleared the unef $self->$orig(@_) }); @@ -19,7 +19,7 @@ around('initialize_instance_slot', sub { =head1 NAME -MooseX::UndefTolerant::Attribute - Make your attribute tolerant to undef intitialization +MooseX::UndefTolerant::Attribute - Make your attribute(s) tolerant to undef intitialization =head1 SYNOPSIS @@ -45,8 +45,8 @@ MooseX::UndefTolerant::Attribute - Make your attribute tolerant to undef intitia 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. +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 diff --git a/t/defaults.t b/t/defaults.t new file mode 100644 index 0000000..aa8680a --- /dev/null +++ b/t/defaults.t @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +use Test::More; + +package Foo; + +use Moose; +use MooseX::UndefTolerant::Attribute; + +has bar => ( + is => 'rw', + traits => ['MooseX::UndefTolerant::Attribute'], + default => 'baz' +); + +1; + +package main; + +my $foo = Foo->new( bar => undef ); +is ( $foo->bar, 'baz', 'does the default value get set when passing undef in the constructor' ); + +done_testing;