kura - Store constraints for Data::Checks, Type::Tiny, Moose, and more.
package MyFoo {
use Exporter 'import';
use Data::Checks qw(StrEq);
use kura Foo => StrEq('foo');
}
package MyBar {
use Exporter 'import';
use Types::Standard -types;
use kura Bar => Str & sub { $_[0] eq 'bar' };
}
package MyBaz {
use Exporter 'import';
use Moose::Util::TypeConstraints;
use kura Baz => subtype as 'Str' => where { $_[0] eq 'baz' };
}
package MyQux {
use Exporter 'import';
use kura Qux => sub { $_[0] eq 'qux' };
}
use MyFoo qw(Foo);
use MyBar qw(Bar);
use MyBaz qw(Baz);
use MyQux qw(Qux); # CodeRef converted to Type::Tiny
ok Foo->check('foo') && !Foo->check('bar') && !Foo->check('baz') && !Foo->check('qux');
ok !Bar->check('foo') && Bar->check('bar') && !Bar->check('baz') && !Bar->check('qux');
ok !Baz->check('foo') && !Baz->check('bar') && Baz->check('baz') && !Baz->check('qux');
ok !Qux->check('foo') && !Qux->check('bar') && !Qux->check('baz') && Qux->check('qux');
Kura - means "Traditional Japanese storehouse" - stores constraints, such as Data::Checks, Type::Tiny, Moose::Meta::TypeConstraint, Mouse::Meta::TypeConstraint, Specio, and more. It can even be used with Moo when combined with Type::Tiny constraints.
Data::Checks -----------------> +--------+
| |
Type::Tiny -------------------> | |
| Kura | ---> Named Value Constraints!
Moose::Meta::TypeConstraint --> | |
| |
YourFavoriteConstraint -------> +--------+
If your project uses multiple constraint libraries, kura allows you to simplify your codes and making it easier to manage different constraint systems. This is especially useful in large projects or when migrating from one constraint system to another.
It's easy to use to store constraints in a package:
use kura NAME => CONSTRAINT;
This constraint must be a any object that has a check
method or a code reference that returns true or false.
The following is an example of a constraint declaration:
use Exporter 'import';
use Types::Standard -types;
use kura Name => Str & sub { qr/^[A-Z][a-z]+$/ };
use kura Level => Int & sub { $_[0] >= 1 && $_[0] <= 100 };
use kura Character => Dict[
name => Name,
level => Level,
];
When declaring constraints, it is important to define child constraints before their parent constraints to avoid errors. For example:
# Bad order
use kura Parent => Dict[ name => Child ]; # => Bareword "Child" not allowed
use kura Child => Str;
# Good order
use kura Child => Str;
use kura Parent => Dict[ name => Child ];
If constraints are declared in the wrong order, you might encounter errors like “Bareword not allowed.” Ensure that all dependencies are declared beforehand to prevent such issues.
You can export the declared constraints by your favorite Exporter package such as Exporter, Exporter::Tiny, and more.
Internally, Kura automatically adds the declared constraint to @EXPORT_OK
, so you just put use Exporter 'import';
in your package:
package MyFoo {
use Exporter 'import';
use Data::Checks qw(StrEq);
use kura Foo => StrEq('foo');
}
use MyFoo qw(Foo);
Foo->check('foo'); # true
Foo->check('bar'); # false
If you forget to put use Exporter 'import';
, you get an error like this:
package MyFoo {
# use Exporter 'import'; # Forgot to load Exporter!!
use Data::Checks qw(StrEq);
use kura Foo => StrEq('foo');
}
use MyFoo qw(Foo);
# => ERROR!
Attempt to call undefined import method with arguments ("Foo" ...) via package "MyFoo"
(Perhaps you forgot to load the package?)
Copyright (C) kobaken.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
kobaken [email protected]