Skip to content

Commit

Permalink
Update docs: checker -> constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Aug 18, 2024
1 parent 2fe2e84 commit 7c51af3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Type::Tiny -------------------> | |
| Kura | ---> Named Value Constraints!
Moose::Meta::TypeConstraint --> | |
| |
YourFavoriteChecker ----------> +--------+
YourFavoriteConstraint -------> +--------+
```

If your project uses multiple constraint libraries, kura allows you to simplify your codebase 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.
Expand Down
50 changes: 26 additions & 24 deletions lib/kura.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ my %FORBIDDEN_NAME = map { $_ => 1 } qw{
# You can change this class by setting $kura::EXPORTER_CLASS.
our $EXPORTER_CLASS = 'Exporter';

# This is a default checker code to object.
# You can change this code by setting $kura::CHECKER_CODE_TO_OBJECT.
our $CHECKER_CODE_TO_OBJECT = sub {
my ($name, $checker, $caller) = @_;
# This is a default constraint code to object.
# You can change this code by setting $kura::CALLABLE_TO_OBJECT.
#
# NOTE: This variable will probably change. Use caution when overriding it.
our $CALLABLE_TO_OBJECT = sub {
my ($name, $constraint, $caller) = @_;

require Type::Tiny;
Type::Tiny->new(
constraint => $checker,
constraint => $constraint,
);
};

Expand All @@ -38,7 +40,7 @@ sub import {

sub import_into {
my $pkg = shift;
my ($caller, $name, $checker) = @_;
my ($caller, $name, $constraint) = @_;

state $validate_name = sub {
my ($name) = @_;
Expand All @@ -52,40 +54,40 @@ sub import_into {
return;
};

state $validate_checker = sub {
my ($checker) = @_;
state $validate_constraint = sub {
my ($constraint) = @_;

unless (defined $checker) {
return 'checker is required';
unless (defined $constraint) {
return 'constraint is required';
}

return if Scalar::Util::blessed($checker) && $checker->can('check');
return if Scalar::Util::blessed($constraint) && $constraint->can('check');

my $ref = Scalar::Util::reftype($checker) // '';
my $ref = Scalar::Util::reftype($constraint) // '';

return if $ref eq 'CODE';

return 'Not a valid checker';
return "Invalid constraint. It must be an object that has a 'check' method or a code reference.";
};

state $checker_to_code = sub {
my ($name, $checker, $caller) = @_;
state $constraint_to_code = sub {
my ($name, $constraint, $caller) = @_;

if (Scalar::Util::reftype($checker) eq 'CODE') {
$checker = $CHECKER_CODE_TO_OBJECT->($name, $checker, $caller);
if (Scalar::Util::reftype($constraint) eq 'CODE') {
$constraint = $CALLABLE_TO_OBJECT->($name, $constraint, $caller);
}

sub { $checker };
sub { $constraint };
};

state $install_checker = sub {
my ($name, $checker, $caller) = @_;
state $install_constraint = sub {
my ($name, $constraint, $caller) = @_;

if ($caller->can($name)) {
return "'$name' is already defined";
}

my $code = $checker_to_code->(@_);
my $code = $constraint_to_code->(@_);

{
no strict "refs";
Expand Down Expand Up @@ -116,10 +118,10 @@ sub import_into {
$err = $validate_name->($name);
Carp::croak $err if $err;

$err = $validate_checker->($checker);
$err = $validate_constraint->($constraint);
Carp::croak $err if $err;

$err = $install_checker->($name, $checker, $caller);
$err = $install_constraint->($name, $constraint, $caller);
Carp::croak $err if $err;

$err = $setup_exporter->($caller);
Expand Down Expand Up @@ -176,7 +178,7 @@ Kura - means "Traditional Japanese storehouse" - stores constraints, such as L<D
| Kura | ---> Named Value Constraints!
Moose::Meta::TypeConstraint --> | |
| |
YourFavoriteChecker ----------> +--------+
YourFavoriteConstraint -------> +--------+
If your project uses multiple constraint libraries, kura allows you to simplify your codebase 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.
Expand Down
26 changes: 13 additions & 13 deletions t/01-kura.t
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use Test2::V0;

use lib './t/lib';
use MyChecker;
use MyConstraint;

subtest 'Test `kura` features' => sub {
subtest '`kura` import checker into caller' => sub {
use kura X => MyChecker->new;
isa_ok X, 'MyChecker';
subtest '`kura` import constraint into caller' => sub {
use kura X => MyConstraint->new;
isa_ok X, 'MyConstraint';
};

subtest '`kura` with constarint and other function.' => sub {
use MyFoo qw(Foo hello);
isa_ok Foo, 'MyChecker';
isa_ok Foo, 'MyConstraint';
is hello(), 'Hello, Foo!';
};
};

subtest 'Test `kura` exceptions' => sub {
subtest 'Checker already defined' => sub {
eval "use kura Foo => MyChecker->new";
subtest 'Constraint already defined' => sub {
eval "use kura Foo => MyConstraint->new";
like $@, qr/^'Foo' is already defined/;
};

Expand All @@ -28,24 +28,24 @@ subtest 'Test `kura` exceptions' => sub {
};

subtest 'Forbidden name' => sub {
eval "use kura BEGIN => MyChecker->new";
eval "use kura BEGIN => MyConstraint->new";
like $@, qr/^'BEGIN' is forbidden/;
};

subtest 'Not given checker' => sub {
subtest 'Not given constraint' => sub {
eval "use kura Foo";
like $@, qr/^checker is required/;
like $@, qr/^constraint is required/;
};

subtest 'Invalid checker' => sub {
subtest 'Invalid constraint' => sub {
eval "use kura Bar => 1";
like $@, qr/^Not a valid checker/;
like $@, qr/^Invalid constraint/;
};

subtest 'Invalid orders' => sub {
eval "
use kura B => A;
use kura A => MyChecker->new;
use kura A => MyConstraint->new;
";
like $@, qr/^Bareword "A" not allowed/;
};
Expand Down
8 changes: 4 additions & 4 deletions t/02-import_into.t
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use Test2::V0;

use lib './t/lib';
use MyChecker;
use MyConstraint;

subtest 'Test `import_into` method' => sub {
subtest 'Customize the import method to your taste' => sub {
use mykura Foo => MyChecker->new;
use mykura Foo => MyConstraint->new;

# MyKura customize the name of the checker
isa_ok MyFoo, 'MyChecker';
# MyKura customize the name of the constraint
isa_ok MyFoo, 'MyConstraint';
}
};

Expand Down
2 changes: 1 addition & 1 deletion t/lib/MyChecker.pm → t/lib/MyConstraint.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package MyChecker;
package MyConstraint;

sub new { bless {}, shift }
sub check { 1 }
Expand Down
4 changes: 2 additions & 2 deletions t/lib/MyFoo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ our @EXPORT_OK;
push @EXPORT_OK, qw(hello);

use lib 't/lib';
use MyChecker;
use MyConstraint;

use kura Foo => MyChecker->new;
use kura Foo => MyConstraint->new;

sub hello { 'Hello, Foo!' }

Expand Down
4 changes: 2 additions & 2 deletions t/lib/mykura.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ sub import {
my $class = shift;
my $caller = caller;

my ($name, $checker) = @_;
my ($name, $constraint) = @_;

$name = 'My' . $name;

kura->import_into($caller, $name, $checker);
kura->import_into($caller, $name, $constraint);
}

1;

0 comments on commit 7c51af3

Please sign in to comment.