|
|
| |
Data::Object::Types::Keywords(3) |
User Contributed Perl Documentation |
Data::Object::Types::Keywords(3) |
Data::Object::Types::Keywords
Data-Object Type Library Keywords
package Test::Library;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
register
{
name => 'Person',
aliases => ['Student', 'Teacher'],
validation => is_instance_of('Test::Person'),
parent => 'Object'
},
{
name => 'Principal',
validation => is_instance_of('Test::Person'),
parent => 'Object'
};
# creates person, student, and teacher constraints
package main;
my $library = Test::Library->meta;
This package provides type library keyword functions for
Data::Object::Types::Library and Type::Library libraries.
This package uses type constraints from:
Types::Standard
This package supports the following scenarios:
package Test::Library::Exports;
use base 'Data::Object::Types::Library';
use Data::Object::Types::Keywords;
# The following is a snapshot of the exported keyword functions:
# as
# class_type
# classifier
# coerce
# compile_match_on_type
# declare
# declare_coercion
# duck_type
# dwim_type
# english_list
# enum
# extends
# from
# is_all_of
# is_any_of
# is_one_of
# inline_as
# intersection
# is_capable_of
# is_consumer_of
# is_instance_of
# match_on_type
# message
# register
# role_type
# subtype
# to_type
# type
# union
# via
# where
"Test::Library::Exports"
This package supports exporting functions which help configure
Type::Library derived libraries.
This package implements the following functions:
is_all_of(CodeRef @checks) : CodeRef
The is_all_of function accepts one or more callbacks and returns
truthy if all of the callbacks return truthy.
- is_all_of example #1
-
package Test::Library::HasAllOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_all_of(
sub {
my ($value) = @_;
return 0 if !$value->isa('Test::Entity');
return 1;
},
sub {
my ($value) = @_;
return 0 if !$value->isa('Test::Person');
return 1;
},
);
register {
name => 'Person',
validation => $validation,
parent => 'Object'
};
$validation
is_any_of(CodeRef @checks) : CodeRef
The is_any_of function accepts one or more callbacks and returns
truthy if any of the callbacks return truthy.
- is_any_of example #1
-
package Test::Library::HasAnyOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_any_of(
sub {
my ($value) = @_;
return 0 if !$value->isa('App::Person');
return 1;
},
sub {
my ($value) = @_;
return 0 if !$value->isa('Test::Person');
return 1;
},
);
register {
name => 'Person',
validation => $validation,
parent => 'Object'
};
$validation
is_capable_of(Str @routines) : CodeRef
The is_capable_of function accepts one or more subroutine names
and returns a callback which returns truthy if the value passed to the
callback has implemented all of the routines specified.
- is_capable_of example #1
-
package Test::Library::IsCapableOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_capable_of(qw(create update delete));
register {
name => 'Person',
validation => $validation,
parent => 'Object'
};
$validation
is_comprised_of(Str @names) : CodeRef
The is_comprised_of function accepts one or more names and returns
a callback which returns truthy if the value passed to the callback is a
hashref or hashref based object which has keys that correspond to the names
provided.
- is_comprised_of example #1
-
package Test::Library::IsComprisedOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_comprised_of(qw(mon tues wed thurs fri sat sun));
register {
name => 'WorkHours',
validation => $validation,
parent => 'HashRef'
};
$validation
is_consumer_of(Str $name) : CodeRef
The is_consumer_of function accepts a role name and returns a
callback which returns truthy if the value passed to the callback consumes
the role specified.
- is_consumer_of example #1
-
package Test::Library::IsConsumerOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_consumer_of('Test::Role::Identifiable');
register {
name => 'Person',
validation => $validation,
parent => 'Object'
};
$validation
is_instance_of(Str $name) : CodeRef
The is_instance_of function accepts a class or package name and
returns a callback which returns truthy if the value passed to the callback
inherits from the class or package specified.
- is_instance_of example #1
-
package Test::Library::IsInstanceOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_instance_of('Test::Person');
register {
name => 'Person',
validation => $validation,
parent => 'Object'
};
$validation
is_one_of(CodeRef @checks) : CodeRef
The is_one_of function accepts one or more callbacks and returns
truthy if only one of the callbacks return truthy.
- is_one_of example #1
-
package Test::Library::HasOneOf;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
my $validation = is_one_of(
sub {
my ($value) = @_;
return 0 if !$value->isa('Test::Student');
return 1;
},
sub {
my ($value) = @_;
return 0 if !$value->isa('Test::Teacher');
return 1;
},
);
register {
name => 'Person',
validation => $validation,
parent => 'Object'
};
$validation
register(HashRef $type) : InstanceOf["Type::Tiny"]
The register function takes a simple hashref and creates and
registers a Type::Tiny type object.
- register example #1
-
package Test::Library::Standard;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
register {
name => 'Message',
coercions => [
'Str', sub {
my ($value) = @_;
{
type => 'simple',
payload => $value
}
}
],
validation => sub {
my ($value) = @_;
return 0 if !$value->{type};
return 0 if !$value->{payload};
return 1;
},
parent => 'HashRef'
};
- register example #2
-
package Test::Library::Parameterized;
use Data::Object::Types::Keywords;
use base 'Data::Object::Types::Library';
extends 'Types::Standard';
register {
name => 'People',
coercions => [
'ArrayRef', sub {
my ($value) = @_;
Test::People->new($value)
}
],
validation => sub {
my ($value) = @_;
return 0 if !$value->isa('Test::People');
return 1;
},
explaination => sub {
my ($value, $type, $name) = @_;
my $param = $type->parameters->[0];
for my $i (0 .. $#$value) {
next if $param->check($value->[$i]);
my $indx = sprintf('%s->[%d]', $name, $i);
my $desc = $param->validate_explain($value->[$i], $indx);
my $text = '"%s" constrains each value in the array object with "%s"';
return [sprintf($text, $type, $param), @{$desc}];
}
return;
},
parameterize_constraint => sub {
my ($value, $type) = @_;
$type->check($_) || return for @$value;
return !!1;
},
parameterize_coercions => sub {
my ($data, $type, $anon) = @_;
my $coercions = [];
push @$coercions, 'ArrayRef', sub {
my $value = @_ ? $_[0] : $_;
my $items = [];
for (my $i = 0; $i < @$value; $i++) {
return $value unless $anon->check($value->[$i]);
$items->[$i] = $data->coerce($value->[$i]);
}
return $type->coerce($items);
};
return $coercions;
},
parent => 'Object'
};
Al Newkirk, "awncorp@cpan.org"
Copyright (C) 2011-2019, Al Newkirk, et al.
This is free software; you can redistribute it and/or modify it
under the terms of the The Apache License, Version 2.0, as elucidated in the
"license file"
<https://github.com/iamalnewkirk/data-object-types/blob/master/LICENSE>.
Wiki <https://github.com/iamalnewkirk/data-object-types/wiki>
Project
<https://github.com/iamalnewkirk/data-object-types>
Initiatives
<https://github.com/iamalnewkirk/data-object-types/projects>
Milestones
<https://github.com/iamalnewkirk/data-object-types/milestones>
Contributing
<https://github.com/iamalnewkirk/data-object-types/blob/master/CONTRIBUTE.md>
Issues
<https://github.com/iamalnewkirk/data-object-types/issues>
Visit the GSP FreeBSD Man Page Interface. Output converted with ManDoc. |