From: Chris Andrews Date: Fri, 8 Oct 2010 19:11:38 +0000 (+0100) Subject: Add support for immutable classes, by adjusting the generated constructor. X-Git-Tag: 0.06~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2d1c57bd726cc5ecf2aba441912a9163a74ceb1a;p=gitmo%2FMooseX-UndefTolerant.git Add support for immutable classes, by adjusting the generated constructor. --- diff --git a/lib/MooseX/UndefTolerant.pm b/lib/MooseX/UndefTolerant.pm index b026bc1..3707141 100644 --- a/lib/MooseX/UndefTolerant.pm +++ b/lib/MooseX/UndefTolerant.pm @@ -4,11 +4,15 @@ use Moose qw(); use Moose::Exporter; use MooseX::UndefTolerant::Attribute; +use MooseX::UndefTolerant::Constructor; our $VERSION = '0.04'; Moose::Exporter->setup_import_methods( - class_metaroles => { attribute => [ 'MooseX::UndefTolerant::Attribute' ] } + class_metaroles => { + attribute => [ 'MooseX::UndefTolerant::Attribute' ], + constructor => [ 'MooseX::UndefTolerant::Constructor' ], + } ); 1; diff --git a/lib/MooseX/UndefTolerant/Constructor.pm b/lib/MooseX/UndefTolerant/Constructor.pm new file mode 100644 index 0000000..e89b609 --- /dev/null +++ b/lib/MooseX/UndefTolerant/Constructor.pm @@ -0,0 +1,18 @@ +package MooseX::UndefTolerant::Constructor; +use Moose::Role; + +around('_generate_slot_initializer', sub { + my $orig = shift; + my $self = shift; + my $attr = $self->_attributes->[$_[0]]->init_arg; + + my $tolerant_code = + qq# delete \$params->{'$attr'} unless # . + qq# exists \$params->{'$attr'} && defined \$params->{'$attr'};\n#; + + return $tolerant_code . $self->$orig(@_); +}); + +no Moose::Role; + +1;