From: Dave Rolsky Date: Sun, 27 Feb 2011 17:20:55 +0000 (-0600) Subject: Code for forward compat with Moose 1.99+ X-Git-Tag: 0.09~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d6ce838b149668ac59c654aa996c3915c311507c;p=gitmo%2FMooseX-UndefTolerant.git Code for forward compat with Moose 1.99+ --- diff --git a/lib/MooseX/UndefTolerant.pm b/lib/MooseX/UndefTolerant.pm index f25b684..f76117e 100644 --- a/lib/MooseX/UndefTolerant.pm +++ b/lib/MooseX/UndefTolerant.pm @@ -4,15 +4,22 @@ use Moose qw(); use Moose::Exporter; use MooseX::UndefTolerant::Attribute; +use MooseX::UndefTolerant::Class; use MooseX::UndefTolerant::Constructor; our $VERSION = '0.07'; +my %metaroles = ( attribute => [ 'MooseX::UndefTolerant::Attribute' ] ); +if ( $Moose::VERSION < 1.9900 ) { + $metaroles{constructor} = [ 'MooseX::UndefTolerant::Constructor' ]; +} +else { + $metaroles{class} = [ 'MooseX::UndefTolerant::Class' ]; +} + + Moose::Exporter->setup_import_methods( - class_metaroles => { - attribute => [ 'MooseX::UndefTolerant::Attribute' ], - constructor => [ 'MooseX::UndefTolerant::Constructor' ], - } + class_metaroles => \%metaroles, ); 1; diff --git a/lib/MooseX/UndefTolerant/Class.pm b/lib/MooseX/UndefTolerant/Class.pm new file mode 100644 index 0000000..c8bd25c --- /dev/null +++ b/lib/MooseX/UndefTolerant/Class.pm @@ -0,0 +1,25 @@ +package MooseX::UndefTolerant::Class; +use Moose::Role; + +if ( $Moose::VERSION >= 1.9900 ) { + around('_inline_init_attr_from_constructor', sub { + my $orig = shift; + my $self = shift; + my ($attr, $idx) = @_; + + my @source = $self->$orig(@_); + + my $init_arg = $attr->init_arg; + + return + "if ( exists \$params->{$init_arg} && defined \$params->{$init_arg} ) {", + @source, + '} else {', + "delete \$params->{$init_arg};", + '}'; + }); +} + +no Moose::Role; + +1; diff --git a/lib/MooseX/UndefTolerant/Constructor.pm b/lib/MooseX/UndefTolerant/Constructor.pm index 841811d..b9ccc0b 100644 --- a/lib/MooseX/UndefTolerant/Constructor.pm +++ b/lib/MooseX/UndefTolerant/Constructor.pm @@ -1,27 +1,29 @@ package MooseX::UndefTolerant::Constructor; use Moose::Role; -around('_generate_slot_initializer', sub { - my $orig = shift; - my $self = shift; +if ( $Moose::VERSION < 1.9900 ) { + around('_generate_slot_initializer', sub { + my $orig = shift; + my $self = shift; - # note the key in the params may not match the attr name. - my $key_name = $self->_attributes->[$_[0]]->init_arg; + # note the key in the params may not match the attr name. + my $key_name = $self->_attributes->[$_[0]]->init_arg; - # insert a line of code at the start of the initializer, - # clearing the param if it's undefined. + # insert a line of code at the start of the initializer, + # clearing the param if it's undefined. - if (defined $key_name) { - my $tolerant_code = - qq# delete \$params->{'$key_name'} unless # . - qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#; + if (defined $key_name) { + my $tolerant_code = + qq# delete \$params->{'$key_name'} unless # . + qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#; - return $tolerant_code . $self->$orig(@_); - } - else { - return $self->$orig(@_); - } -}); + return $tolerant_code . $self->$orig(@_); + } + else { + return $self->$orig(@_); + } + }); +} no Moose::Role;