NAME
Fukurama::Class::DataTypes - Helper class to register and check datatypes
VERSION
Version 0.03 (beta)
SYNOPSIS
package MyClass;
BEGIN {
use Fukurama::Class::DataTypes();
Fukurama::Class::DataTypes->set_type_checker('MyOwnClass', sub {
my $parameter = $_[0];
my $data_type_checker_name = $_[1];
my $error = undef;
my $is_ok = 0;
if(ref($parameter) && UNIVERSAL::isa($parameter, 'MyOwnClass')) {
if($parameter->get('name') eq 'MyOwnName') {
$is_ok = 1;
} else {
$error = 'nameIsNotCorrect';
}
} else {
$error = 'notAnObject';
}
return ($is_ok, $parameter, $error);
});
}
use Fukurama::Class;
# Croak, if parameter one is not an instance of 'MyOwnClass'
# and doesn't have the name 'MyOwnName'
sub set_my_own_class : Method(public|MyOwnClass|boolean) {
...
}
DESCRIPTION
This helper-class provides functions to register and handle check-methods for several data types.
EXPORT
-
METHODS
- set_type_checker( name:STRING, checker:CODE ) return:BOOLEAN
-
Set a check-method for a new or an existing datatype. Name is the identifier string for the data type, checker is a code reference to check the data type.
Examples for name: string , boolean, MySpecial::Class Native data types have to be in lowercase. Object data types have to start with an uppercase letter. You should be careful when you define the identifier because object data types would be handeled different as native data types.
An example for a type checker for a simple hash reference:
$class->set_type_checker('hashref', sub { my $parameter = $_[0]; my $data_type_checker_name = $_[1]; my $error = undef; my $is_ok = 0; if(ref($parameter) eq 'HASH') { $is_ok = 1; } else { $error = 'notARef'; } return ($is_ok, $parameter, $error); });
- set_ref_checker( identifier:STRING, checker:CODE ) return:BOOLEAN
-
Set a check-method for a new or an existing reference type. identifier is the string which identifies this reference, checker is a code referende to check the reference type.
Examples for identifier: [] (for array reference), {} (for hash reference)
An example for a reference checker for array references:
$class->set_ref_checker('[]' => sub { my $data_type_checker = $_[0]; my $parameter = $_[1]; my $data_type_checker_name = $_[2]; my $actual_subroutine_parameter = $_[3]; # \INT my $full_subroutine_parameter_list = $_[4]; # \ARRAY return 0 if(ref($parameter) ne 'ARRAY'); my $i = 0; my $error = undef; # Check all entries of this array reference foreach my $parameter_entry (@{$parameter}) { my ($is_ok, $returned_parameter, $returned_error) = &{$data_type_checker}($parameter_entry, $data_type_checker_name); if(!$is_ok) { $parameter->[$i] = $returned_parameter; $error = [ $is_ok, $parameter, $returned_error ]; } ++$i; } return @$error if($error) { 1; });
- is_ref_allowed( identifier:STRING ) return:BOOLEAN
-
Method to check if the given identifier has a defined reference checker.
- get_check_definition ( type_name:STRING, ref_identifier:STRING ) return:HASHREF
-
For internal usage in attribute helper classes. Get the defined checker methods for data type and the reference identifier as a hash reference.
{ is_class => data_type_is_a_class:BOOLEAN, check => reference_checker:CODE, param_0 => data_type_checker:CODE, }
- check_parameter_definition ( type_name:STRING, parameter_definition:HASHREF ) return:BOOLEAN
-
For internal usage in attribute helper classes. Check the given parameter definition.
AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE
see perldoc of Fukurama::Class