From: Yuval Kogman Date: Mon, 19 May 2008 08:04:34 +0000 (+0000) Subject: add the splice method, a generalization of insert X-Git-Tag: 0.18_01~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=331e1af071868ee1c56f59091da141822c7542b0;p=gitmo%2FMooseX-AttributeHelpers.git add the splice method, a generalization of insert --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm index 7366081..3ea71b4 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm @@ -115,7 +115,26 @@ sub insert : method { }; } } - + +sub splice : method { + my ($attr, $reader, $writer) = @_; + if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { + my $container_type_constraint = $attr->type_constraint->type_parameter; + return sub { + my ( $self, $i, $j, @elems ) = @_; + ($container_type_constraint->check($_)) + || confess "Value " . (defined($_) ? $_ : 'undef') . " did not pass container type constraint" for @elems; + CORE::splice @{$self->$reader()}, $i, $j, @elems; + }; + } + else { + return sub { + my ( $self, $i, $j, @elems ) = @_; + CORE::splice @{$self->$reader()}, $i, $j, @elems; + }; + } +} + 1; __END__ @@ -164,6 +183,8 @@ see those provied methods, refer to that documentation. =item B +=item B + =back =head1 BUGS