Subject: | delete_params for Workflow::Base |
Date: | Mon, 21 Aug 2006 11:10:32 +0200 |
To: | Bugs in Workflow via RT <bug-Workflow [...] rt.cpan.org> |
From: | Michael Bell <mbell [...] informatik.hu-berlin.de> |
Hi,
Workflow 0.22 and 0.22+
Perl 5.8.8
Actually (0.22) it is only possible to delete all parameters at once. It
is not possible to delete a single parameter or to delete a list of
parameters. This functionality is added with the function delete_param.
It use a similar interface design like param. I added documentation and
some test cases too.
Base.pm.22.diff - full diff against v0.22
Base.pm.new.diff - diff which only includes the changes for this fix
Best regards
Michael
--
Show quoted text
_______________________________________________________________
Michael Bell Humboldt-Universitaet zu Berlin
Tel.: +49 (0)30-2093 2482 ZE Computer- und Medienservice
Fax: +49 (0)30-2093 2704 Unter den Linden 6
michael.bell@cms.hu-berlin.de D-10099 Berlin
_______________________________________________________________
--- t/base.t.org 2006-08-21 10:47:34.000000000 +0200
+++ t/base.t 2006-08-21 10:56:41.000000000 +0200
@@ -5,7 +5,7 @@
use strict;
use lib 't';
use TestUtil;
-use Test::More tests => 22;
+use Test::More tests => 26;
require_ok( 'Workflow::Base' );
@@ -36,6 +36,15 @@
'Multiple parameter set value 1 ok' );
is( $b->param( 'blah' ), 'blech',
'Multiple parameter set value 2 ok' );
+is( $b->delete_param('baz'), 'quux',
+ 'Delete one parameter' );
+is( $b->param( 'baz' ), undef,
+ 'Parameter call to nonexistent key returns undef' );
+my $list = $b->delete_param(['blah']);
+is ( $list->{'blah'}, 'blech',
+ 'Deleted several parameters' );
+is( $b->param( 'blah' ), undef,
+ 'Parameter call to nonexistent key returns undef' );
ok( $b->clear_params,
'Cleared param call executed ok' );
is( scalar keys %{ $b->param }, 0,
--- Base.pm.org_new 2006-08-21 10:39:38.000000000 +0200
+++ Base.pm 2006-08-21 11:06:19.000000000 +0200
@@ -50,6 +50,35 @@
return $self->{PARAMS}{ $name } = $value;
}
+sub delete_param {
+ my ( $self, $name ) = @_;
+ unless ( defined $name ) {
+ ## this is an error - perhaps an exception is too radical
+ return undef;
+ }
+
+ # Allow multiple parameters to be deleted at once...
+
+ if ( ref $name eq 'ARRAY' ) {
+ my %list = ();
+ foreach my $param_name ( @{ $name } ) {
+ next if (not exists $self->{PARAMS}{ $param_name });
+ $list{$param_name} = $self->{PARAMS}{ $param_name };
+ delete $self->{PARAMS}{ $param_name }
+ }
+ return { %list };
+ }
+
+ if ( exists $self->{PARAMS}{ $name } ) {
+ my $value = $self->{PARAMS}{ $name };
+ delete $self->{PARAMS}{ $name };
+ return $value;
+ }
+
+ ## this is an error - perhaps an exception is too radical
+ return undef;
+}
+
sub clear_params {
my ( $self ) = @_;
$self->{PARAMS} = {};
@@ -130,6 +159,29 @@
$object->param( foo => 'blurney' );
+=head3 delete_param( [ $name ] )
+
+Delete parameters from this object.
+
+If C<$name> given and it is an array reference, then delete all
+parameters from this object. All deleted parameters will be returned
+as a hash reference together with their values.
+
+ my $deleted = $object->delete_param(['foo','baz']);
+ foreach my $key (keys %{$deleted})
+ {
+ print $key."::=".$deleted->{$key}."\n";
+ }
+
+If C<$name> given and it is not an array reference, delete the
+parameter and return the value of the parameter.
+
+ my $value = $object->delete_param( 'foo' );
+ print "Value of 'foo' was '$value'\n";
+
+If C<$name> is not defined or C<$name> does not exists the
+undef is returned.
+
=head3 clear_params()
Clears out all parameters associated with this object.
--- Base.pm.org 2006-08-21 10:08:24.000000000 +0200
+++ Base.pm 2006-08-21 11:06:19.000000000 +0200
@@ -28,7 +28,7 @@
sub param {
my ( $self, $name, $value ) = @_;
- unless ( $name ) {
+ unless ( defined $name ) {
return { %{ $self->{PARAMS} } };
}
@@ -41,7 +41,7 @@
return { %{ $self->{PARAMS} } };
}
- unless ( $value ) {
+ unless ( defined $value ) {
if ( exists $self->{PARAMS}{ $name } ) {
return $self->{PARAMS}{ $name };
}
@@ -50,6 +50,35 @@
return $self->{PARAMS}{ $name } = $value;
}
+sub delete_param {
+ my ( $self, $name ) = @_;
+ unless ( defined $name ) {
+ ## this is an error - perhaps an exception is too radical
+ return undef;
+ }
+
+ # Allow multiple parameters to be deleted at once...
+
+ if ( ref $name eq 'ARRAY' ) {
+ my %list = ();
+ foreach my $param_name ( @{ $name } ) {
+ next if (not exists $self->{PARAMS}{ $param_name });
+ $list{$param_name} = $self->{PARAMS}{ $param_name };
+ delete $self->{PARAMS}{ $param_name }
+ }
+ return { %list };
+ }
+
+ if ( exists $self->{PARAMS}{ $name } ) {
+ my $value = $self->{PARAMS}{ $name };
+ delete $self->{PARAMS}{ $name };
+ return $value;
+ }
+
+ ## this is an error - perhaps an exception is too radical
+ return undef;
+}
+
sub clear_params {
my ( $self ) = @_;
$self->{PARAMS} = {};
@@ -130,6 +159,29 @@
$object->param( foo => 'blurney' );
+=head3 delete_param( [ $name ] )
+
+Delete parameters from this object.
+
+If C<$name> given and it is an array reference, then delete all
+parameters from this object. All deleted parameters will be returned
+as a hash reference together with their values.
+
+ my $deleted = $object->delete_param(['foo','baz']);
+ foreach my $key (keys %{$deleted})
+ {
+ print $key."::=".$deleted->{$key}."\n";
+ }
+
+If C<$name> given and it is not an array reference, delete the
+parameter and return the value of the parameter.
+
+ my $value = $object->delete_param( 'foo' );
+ print "Value of 'foo' was '$value'\n";
+
+If C<$name> is not defined or C<$name> does not exists the
+undef is returned.
+
=head3 clear_params()
Clears out all parameters associated with this object.