Aion
view release on metacpan or search on metacpan
lib/Aion/Type.pm view on Meta::CPAN
sub make_arg {
my ($self, $pkg, $is_arg) = @_;
my $hash = "%$self->{name}";
my $proto = $is_arg? '$': '';
if($is_arg) {
my $init = $self->{init}? '->init': '';
my $code = "package $pkg {
my $hash = %\$self;
sub $self->{name} (\$) { Aion::Type->new($hash, args => \$_[0])$init }
}";
eval $code;
die if $@;
return $self;
}
my $code = "package $pkg {
my $hash = %\$self;
sub $self->{name} () { Aion::Type->new($hash)->init }
}";
eval $code;
die if $@;
$self
}
# СоздаÑÑ ÑÑнкÑÐ¸Ñ Ð´Ð»Ñ Ñипа c аÑгÑменÑом или без.
# init вÑзÑваеÑÑÑ ÑолÑко Ð´Ð»Ñ Ñипа Ñ Ð°ÑгÑменÑами. Ðез аÑгÑменÑов возвÑаÑаеÑÑÑ Ð¾Ð´Ð¸Ð½ и ÑÐ¾Ñ Ð¶Ðµ Ñип
sub make_maybe_arg {
my ($self, $pkg) = @_;
my $var = "\$$self->{name}";
my $hash = "%$self->{name}";
my $init = $self->{init}? '->init': '';
my $code = "package $pkg;
my $var = \$self;
my $hash = %\$self;
sub $self->{name} (;\$) {
\@_==0? $var:
Aion::Type->new(
$hash,
args => \$_[0],
test => ${var}->{a_test},
)$init
}
";
eval $code or die;
$self
}
1;
__END__
=encoding utf-8
=head1 NAME
Aion::Type - class of validators
=head1 SYNOPSIS
use Aion::Type;
use Aion::Types qw//;
my $Int = Aion::Type->new(name => "Int", test => sub { /^-?\d+$/ });
12 ~~ $Int # => 1
12.1 ~~ $Int # -> ""
my $Char = Aion::Type->new(name => "Char", test => sub { /^.\z/ });
$Char->include("a") # => 1
$Char->exclude("ab") # => 1
my $IntOrChar = $Int | $Char;
77 ~~ $IntOrChar # => 1
"a" ~~ $IntOrChar # => 1
"ab" ~~ $IntOrChar # -> ""
my $Digit = $Int & $Char;
7 ~~ $Digit # => 1
77 ~~ $Digit # -> ""
"a" ~~ ~$Int; # => 1
5 ~~ ~$Int; # -> ""
eval { $Int->validate("a", "..Eval..") }; $@ # ~> ..Eval.. must have the type Int. The it is 'a'
=head1 DESCRIPTION
Spawns validators. Used in C<Aion::Types::subtype>.
=head1 METHODS
=head2 new (%ARGUMENTS)
Constructor.
=head3 ARGUMENTS
=over
=item * name (Str) â Type name.
=item * args (ArrayRef) â List of type arguments.
=item * init (CodeRef) â Type initializer.
=item * test (CodeRef) - Checker.
=item * a_test (CodeRef) â Value checker for types with optional arguments.
=item * coerce (ArrayRef[Tuple[Aion::Type, CodeRef]]) - Array of pairs: type and transition.
=back
( run in 0.442 second using v1.01-cache-2.11-cpan-b9db842bd85 )