Data-Type
view release on metacpan or search on metacpan
lib/Data/Type/Facet.pm view on Meta::CPAN
sub desc : method { 'scalar is numerically not exceeding x' }
sub test : method
{
my $this = shift;
throw Data::Type::Facet::Exception() if $Data::Type::value > $this->[0];
}
sub info : method
{
my $this = shift;
return sprintf 'maximum of %d', $this->[0];
}
package Data::Type::Facet::min;
our @ISA = qw(Data::Type::Facet::Interface);
our $VERSION = '0.01.01';
sub desc : method { 'scalar is numerically more than x' }
sub test : method
{
my $this = shift;
throw Data::Type::Facet::Exception() if $Data::Type::value < $this->[0];
}
sub info : method
{
my $this = shift;
return sprintf 'minimum of %d', $this->[0];
}
package Data::Type::Facet::match;
our @ISA = qw(Data::Type::Facet::Interface);
our $VERSION = '0.01.01';
sub desc : method { 'matches regexp (registered within $Data::Type::rebox. Read Data::Type::Docs::RFC.)' }
sub usage : method
{
return 'match( REGEXP_BOX_ID ) REGEXP_BOX_ID is a key from Data::Type::rebox'
}
sub test : method
{
my $this = shift;
Data::Type::Facet::defined->test;
if( $Data::Type::DEBUG )
{
warn sprintf "FACET match %s value '%s' with $this->[0] (regexp '%s')",
defined( $Data::Type::value ) ? 'defined' : 'undefined',
$Data::Type::value,
$Data::Type::rebox->request( $this->[0], 'regexp', @$this );
}
unless( $Data::Type::value =~ $Data::Type::rebox->request( $this->[0], 'regexp', @$this ) )
{
throw Data::Type::Facet::Exception( text => $Data::Type::rebox->request( $this->[0], 'desc', @$this ) ) ;
}
}
sub info : method
{
my $this = shift;
return sprintf 'matching a regular expression for %s', $Data::Type::rebox->request( $this->[0], 'desc', @$this );
}
package Data::Type::Facet::is;
our @ISA = qw(Data::Type::Facet::Interface);
our $VERSION = '0.01.01';
sub desc : method { 'is == x' }
sub test : method
{
my $this = shift;
throw Data::Type::Facet::Exception( text => "is not exact $this->[0]" ) unless $Data::Type::value == $this->[0];
}
sub info : method
{
my $this = shift;
return sprintf 'exact %s', $this->[0];
}
package Data::Type::Facet::defined;
our @ISA = qw(Data::Type::Facet::Interface);
our $VERSION = '0.01.04';
sub desc : method { 'defined() returns true' }
sub test : method
{
my $this = shift;
throw Data::Type::Facet::Exception( text => 'not defined value' ) unless defined $Data::Type::value;
}
sub info : method
{
lib/Data/Type/Facet.pm view on Meta::CPAN
#
package Data::Type::Facet::file;
our @ISA = qw(Data::Type::Facet::Interface);
our $VERSION = '0.01.01';
sub desc : method { 'whether file is existent' }
sub usage : method { '( FILENAME )' }
sub info : method { 'tests characteristics of file' }
sub test : method
{
my $this = shift;
throw Data::Type::Facet::Exception( text => 'undefined value' ) unless defined $Data::Type::value;
throw Data::Type::Facet::Exception(
text => 'supplied filename does not exist',
value => $Data::Type::value,
type => __PACKAGE__
) unless -e $Data::Type::value;
}
1;
__END__
=head1 NAME
Data::Type::Facet - a subelement of a type
=head1 SYNOPSIS
package Data::Type::Object::std_real;
...
sub _test
{
my $this = shift;
Data::Type::ok( 1, Data::Type::Facet::match( 'std/real' ) );
}
=head1 DESCRIPTION
Facets are bric's for L<Data::Type::Object>'s. They are partially almost trivial (more or less), but have some advantages. They are modularising the testing procedure of any datatype (and therefore giving the magic to the L<Data::Type/summary()> func...
=head1 EXCEPTIONS
L<Data::Type::Facet::Exception> is thrown by any facet to indicate L<Data::Type> that it failed to pass.
=head1 FACETS
=head2 Data::Type::Facet::ref( I<type> )
Data::Type::Facet::ref();
Data::Type::Facet::ref( 'ARRAY' ); # 'HASH' | 'CODE' | ..
Whether the value is a reference. If I<type> is given, this explicit reference is required. So if C<$Data::Type::value = [ 1, 2 ]> then
ok( 1, Data::Type::Facet::ref( 'ARRAY' ) );
would pass. While
ok( 1, Data::Type::Facet::ref( 'HASH' ) );
would of course not.
=head2 Data::Type::Facet::range( I<x>, I<y> )
Data::Type::Facet::range( 1, 10 )
Value is numerically between the lower value I<x> and upper limit value I<y> (including them).
=head2 Data::Type::Facet::lines( I<min> )
Counts the newlines C<\n> in a textblock. Expects more then I<min> lines (newlines).
=head2 Data::Type::Facet::less( I<min> )
Counts the C<length()> of a string and expects less than C<min>.
=head2 Data::Type::Facet::max( I<limit> )
Expects numbers under the I<limit> (E<lt> I<limit>).
=head2 Data::Type::Facet::min( I<limit> )
Expects numbers above I<limit> (E<gt> I<limit>).
=head2 Data::Type::Facet::match( I<boxid> )
Data::Type::Facet::match( 'std/word' );
Please visit L<Data::Type::Docs::RFC/CONVENTIONS> and L<Regexp::Box> for details registering regexps. All regexps used by L<Data::Type> are stored within the central registry C<$Data::Type::rebox> (L<Regexp::Box>). The I<boxid> must be therefore e pr...
=head2 Data::Type::Facet::is()
Expects an exact match (C<==>).
=head2 Data::Type::Facet::defined()
Expects a defined value (as perl's C<defined()>).
=head2 Data::Type::Facet::null()
Expects not literally 'NULL'. Its test C<eq 'NULL'>.
=head2 Data::Type::Facet::bool( 'true' | 'false' )
Data::Type::Facet::bool( 'true' );
( run in 2.206 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )