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 => EMAIL( 1 ), # mxcheck ON ! see Email::Valid
firstname => WORD,
social_id => [ NUM, VARCHAR( 10 ) ],
contacts => sub { my %args = @_; exists $args{lucy} },
}
);
die "object is rejected" unless $dtg->inspect( $h );
# compact version
valid_object { email => EMAIL( 1 ), firstname => WORD }, $object_a, $object_b;
DOCUMENATION
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 return0
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).
- METHODS
-
- $dtg->inspect( $blessed )
-
Accepts a blessed reference as a parameter. It returns
0
if a guard test or type constrain will fail, otherwise1
.[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 themember
$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 ).
Exampe:
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.
- FUNCTIONS
-
valid_object
.':all' loads qw(valid_object)
CONTACT
Also http://sf.net/projects/datatype is hosting a projects 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>
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 204:
You forgot a '=back' before '=head1'
You forgot a '=back' before '=head1'