Class-Param
view release on metacpan or search on metacpan
lib/Class/Param/Callback.pm view on Meta::CPAN
return bless( $self, $class );
};
}
1;
__END__
=head1 NAME
Class::Param::Callback - Param instance with callbacks
=head1 SYNOPSIS
%store = ();
$param = Class::Param::Callback->new(
get => sub { return $store{ $_[1] } },
set => sub { return $store{ $_[1] } = $_[2] },
has => sub { return exists $store{ $_[1] } },
names => sub { return keys %store },
remove => sub { return delete $store{ $_[1] } }
);
=head1 DESCRIPTION
Construct a params instance using callbacks.
=head1 METHODS
=over 4
=item new
This method takes a hash of parameters. The following options are
valid:
lib/Class/Param/Encoding.pm view on Meta::CPAN
spec => [
{
type => Params::Validate::OBJECT,
isa => 'Class::Param::Base',
optional => 0
},
{
type => Params::Validate::SCALAR,
default => 'UTF-8',
optional => 1,
callbacks => {
'valid Encode encoding' => sub {
return Encode::find_encoding( $_[0] );
}
}
}
],
called => "$class\::new"
);
return bless( [ $decorated, Encode::find_encoding($encoding) ], $class );
t/08-callback.t view on Meta::CPAN
can_ok( 'Class::Param::Callback', 'set' );
can_ok( 'Class::Param::Callback', 'add' );
can_ok( 'Class::Param::Callback', 'has' );
can_ok( 'Class::Param::Callback', 'clear' );
can_ok( 'Class::Param::Callback', 'names' );
can_ok( 'Class::Param::Callback', 'new' );
can_ok( 'Class::Param::Callback', 'param' );
can_ok( 'Class::Param::Callback', 'remove' );
my %store = ();
my %callbacks = (
get => sub { return $store{ $_[1] } },
set => sub { return $store{ $_[1] } = $_[2]; },
names => sub { return keys %store },
remove => sub { return delete $store{ $_[1] } }
);
isa_ok my $p = Class::Param::Callback->new(%callbacks), 'Class::Param::Callback', 'Class::Param::Callback->new constructs a new instance';
is_deeply [ $p->names ], [], '->names returns an emplty list';
is_deeply [ $p->param ], [], '->param returns an emplty list';
is $p->param('bogus'), undef, '->param on non existent name returns undef in scalar context';
is_deeply [ $p->param('bogus') ], [], '->param on non existent name returns an empty list in list context';
is $p->get('bogus'), undef, '->get on non existent name returns undef';
is $p->remove('bogus'), undef, '->remove on non existent name returns undef';
is $p->param( 'bogus' => undef ), undef, '->param( name => undef ) on non existent name returns undef';
is $p->param(undef), undef, '->param with a undefined name returns undef in scalar context';
is_deeply [ $p->param(undef) ], [], '->param with a undefined name returns an emply list in list context';
is $p->count, 0, '->count on with no params';
( run in 0.667 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )