NAME
Data::Type::Guard - inspects members of foreign objects
SYNOPSIS
my $dtg = Data::Type::Guard->new
(
allow => [ 'Human', 'Others' ], # blessed objects of that type
tests =>
{
email => STD::EMAIL( 1 ), # mxcheck ON ! see Email::Valid
firstname => STD::WORD,
social_id => [ STD::NUM, STD::VARCHAR( 10 ) ],
contacts => sub { my %args = @_; exists $args{lucy} },
}
);
die "object is rejected" unless $dtg->inspect( $h );
# compact version
valid_object { email => STD::EMAIL( 1 ), firstname => STD::WORD }, $object_a, $object_b;
INTRODUCTION
This class inspects others objects member return-values for a specific datatype.
API
CONSTRUCTOR
my $dtg = Data::Type::Guard->new( allow => $aref, tests => $href )
allow => $aref
If is set then the inspect
function below will return 0
if the object is not a reference of the requested type. If empty, isn't selective for special references ( HASH, ARRAY, "CUSTOM", .. ).
tests => $href
Keys are the members names (anything that can be called via the $o->member syntax) and the type(s) as value. When a member should match multple types, they should be contained in an array reference ( i.e. 'fon' => [ qw(NUM TELEPHONE) ] ). Instead of types a reference to a sub is allowed, while it must return true if it matches (see valid_object() TYPE, .. }, @objects )">).
METHODS
$dtg->inspect( $blessed )
Accepts a blessed reference as a parameter. It returns 0
if a guard test or type constrain will fail, otherwise 1
.
[Note] A more appropriate report on failure is planned.
FUNCTIONS
valid_object( { member => TYPE, .. }, @objects )
Valids members of objects against multiple 'types' or code reference. Any $object
must have an accessor function to its method (same as the key given in the member
$href
). See Data::Type::Guard for oo-interface for that.
my $car = Car->new( speed => 300, year => '2000', owner_firstname => 'micheal' );
valid_object( { year => DATE( 'YEAR' ), owner_firstname => VARCHAR(20), speed => INT }, $car ) or die;
AUTO DETECTION
$Data::Type::Guard::AUTO_DETECT controls if information from the reflection of an object is superseeding the tests => parameter. Visit Class::Maker reflection manual ( for the types =
{}> field ). Example:
use Data::Type qw(:all);
use Data::Type::Guard;
use Class::Maker qw(class);
class 'My::Car',
{
public =>
{
string => [qw( name manufactorer )],
int => [qw( serial )],
},
types =>
{
name => STD::WORD,
serial => STD::NUM,
},
};
my $dtg = Data::Type::Guard->new();
die "My::Car isnt correctly initialized" unless $dtg->inspect( My::Car->new( serial => 'aaaaaa', name => '111' ) );
[Note] This feature is available after Class::Maker '0.5.18', but this is still an undocumented yet.
EXPORT
None per default.
valid_object
.
':all' loads qw(valid_object)
CONTACT
Sourceforge http://sf.net/projects/datatype is hosting a project dedicated to this module. And I enjoy receiving your comments/suggestion/reports also via http://rt.cpan.org or http://testers.cpan.org.
AUTHOR
Murat Uenalan, <muenalan@cpan.org>