Aion

 view release on metacpan or  search on metacpan

lib/Aion/Type.md  view on Meta::CPAN

my $Num = Aion::Type->new(name => "Num", detail => sub {
    my ($val, $name) = @_;
    "Error: $val is'nt $name!"
});

$Num->detail("x", "car")  # => Error: x is'nt car!
```

## validate ($element, $feature)

It tested `$element` and throw `detail` if element is exclude from class.

```perl
my $PositiveInt = Aion::Type->new(
    name => "PositiveInt",
    test => sub { /^\d+$/ },
);

eval {
    $PositiveInt->validate(-1, "Neg")
};

lib/Aion/Type.pm  view on Meta::CPAN

	
	my $Num = Aion::Type->new(name => "Num", detail => sub {
	    my ($val, $name) = @_;
	    "Error: $val is'nt $name!"
	});
	
	$Num->detail("x", "car")  # => Error: x is'nt car!

=head2 validate ($element, $feature)

It tested C<$element> and throw C<detail> if element is exclude from class.

	my $PositiveInt = Aion::Type->new(
	    name => "PositiveInt",
	    test => sub { /^\d+$/ },
	);
	
	eval {
	    $PositiveInt->validate(-1, "Neg")
	};
	$@   # ~> Neg must have the type PositiveInt. The it is -1

lib/Aion/Types.md  view on Meta::CPAN


Four->coerce("4a")	# -> 4

coerce Four, from ArrayRef, via { scalar @$_ };

Four->coerce([1,2,3])	# -> 3
Four->coerce([1,2,3]) ~~ Four	# -> ""
Four->coerce([1,2,3,4]) ~~ Four	# -> 1
```

`coerce` throws exeptions:

```perl
eval {coerce Int, via1 => 1}; $@  # ~> coerce Int unused keys left: via1
eval {coerce "x"}; $@  # ~> coerce x not Aion::Type!
eval {coerce Int}; $@  # ~> coerce Int: from is'nt Aion::Type!
eval {coerce Int, from "x"}; $@  # ~> coerce Int: from is'nt Aion::Type!
eval {coerce Int, from Num}; $@  # ~> coerce Int: via is not subroutine!
eval {coerce Int, (from=>Num, via=>"x")}; $@  # ~> coerce Int: via is not subroutine!
```

lib/Aion/Types.pm  view on Meta::CPAN

	coerce Four, from Str, via { 0+$_ };
	
	Four->coerce("4a")	# -> 4
	
	coerce Four, from ArrayRef, via { scalar @$_ };
	
	Four->coerce([1,2,3])	# -> 3
	Four->coerce([1,2,3]) ~~ Four	# -> ""
	Four->coerce([1,2,3,4]) ~~ Four	# -> 1

C<coerce> throws exeptions:

	eval {coerce Int, via1 => 1}; $@  # ~> coerce Int unused keys left: via1
	eval {coerce "x"}; $@  # ~> coerce x not Aion::Type!
	eval {coerce Int}; $@  # ~> coerce Int: from is'nt Aion::Type!
	eval {coerce Int, from "x"}; $@  # ~> coerce Int: from is'nt Aion::Type!
	eval {coerce Int, from Num}; $@  # ~> coerce Int: via is not subroutine!
	eval {coerce Int, (from=>Num, via=>"x")}; $@  # ~> coerce Int: via is not subroutine!

Standart coerces:

t/aion/type.t  view on Meta::CPAN

my $Num = Aion::Type->new(name => "Num", detail => sub {
    my ($val, $name) = @_;
    "Error: $val is'nt $name!"
});

::is scalar do {$Num->detail("x", "car")}, "Error: x is'nt car!", '$Num->detail("x", "car")  # => Error: x is\'nt car!';

# 
# ## validate ($element, $feature)
# 
# It tested `$element` and throw `detail` if element is exclude from class.
# 
done_testing; }; subtest 'validate ($element, $feature)' => sub { 
my $PositiveInt = Aion::Type->new(
    name => "PositiveInt",
    test => sub { /^\d+$/ },
);

eval {
    $PositiveInt->validate(-1, "Neg")
};

t/aion/types.t  view on Meta::CPAN


::is scalar do {Four->coerce("4a")}, scalar do{4}, 'Four->coerce("4a")	# -> 4';

coerce Four, from ArrayRef, via { scalar @$_ };

::is scalar do {Four->coerce([1,2,3])}, scalar do{3}, 'Four->coerce([1,2,3])	# -> 3';
::is scalar do {Four->coerce([1,2,3]) ~~ Four}, scalar do{""}, 'Four->coerce([1,2,3]) ~~ Four	# -> ""';
::is scalar do {Four->coerce([1,2,3,4]) ~~ Four}, scalar do{1}, 'Four->coerce([1,2,3,4]) ~~ Four	# -> 1';

# 
# `coerce` throws exeptions:
# 

::like scalar do {eval {coerce Int, via1 => 1}; $@}, qr!coerce Int unused keys left: via1!, 'eval {coerce Int, via1 => 1}; $@  # ~> coerce Int unused keys left: via1';
::like scalar do {eval {coerce "x"}; $@}, qr!coerce x not Aion::Type\!!, 'eval {coerce "x"}; $@  # ~> coerce x not Aion::Type!';
::like scalar do {eval {coerce Int}; $@}, qr!coerce Int: from is'nt Aion::Type\!!, 'eval {coerce Int}; $@  # ~> coerce Int: from is\'nt Aion::Type!';
::like scalar do {eval {coerce Int, from "x"}; $@}, qr!coerce Int: from is'nt Aion::Type\!!, 'eval {coerce Int, from "x"}; $@  # ~> coerce Int: from is\'nt Aion::Type!';
::like scalar do {eval {coerce Int, from Num}; $@}, qr!coerce Int: via is not subroutine\!!, 'eval {coerce Int, from Num}; $@  # ~> coerce Int: via is not subroutine!';
::like scalar do {eval {coerce Int, (from=>Num, via=>"x")}; $@}, qr!coerce Int: via is not subroutine\!!, 'eval {coerce Int, (from=>Num, via=>"x")}; $@  # ~> coerce Int: via is not subroutine!';

# 



( run in 0.310 second using v1.01-cache-2.11-cpan-496ff517765 )