From 7c51af35ce2ed9c5dd9508132b626b25e3fd1395 Mon Sep 17 00:00:00 2001 From: kobaken Date: Sun, 18 Aug 2024 21:56:00 +0900 Subject: [PATCH] Update docs: checker -> constraint --- README.md | 2 +- lib/kura.pm | 50 +++++++++++++------------ t/01-kura.t | 26 ++++++------- t/02-import_into.t | 8 ++-- t/lib/{MyChecker.pm => MyConstraint.pm} | 2 +- t/lib/MyFoo.pm | 4 +- t/lib/mykura.pm | 4 +- 7 files changed, 49 insertions(+), 47 deletions(-) rename t/lib/{MyChecker.pm => MyConstraint.pm} (69%) diff --git a/README.md b/README.md index 8b60d04..ba24cd6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/kura.pm b/lib/kura.pm index 1835b59..fff2069 100644 --- a/lib/kura.pm +++ b/lib/kura.pm @@ -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, ); }; @@ -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) = @_; @@ -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"; @@ -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); @@ -176,7 +178,7 @@ Kura - means "Traditional Japanese storehouse" - stores constraints, such as L 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. diff --git a/t/01-kura.t b/t/01-kura.t index b989832..822584f 100644 --- a/t/01-kura.t +++ b/t/01-kura.t @@ -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/; }; @@ -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/; }; diff --git a/t/02-import_into.t b/t/02-import_into.t index 3a761ac..4c10973 100644 --- a/t/02-import_into.t +++ b/t/02-import_into.t @@ -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'; } }; diff --git a/t/lib/MyChecker.pm b/t/lib/MyConstraint.pm similarity index 69% rename from t/lib/MyChecker.pm rename to t/lib/MyConstraint.pm index 0113ac0..9e9601e 100644 --- a/t/lib/MyChecker.pm +++ b/t/lib/MyConstraint.pm @@ -1,4 +1,4 @@ -package MyChecker; +package MyConstraint; sub new { bless {}, shift } sub check { 1 } diff --git a/t/lib/MyFoo.pm b/t/lib/MyFoo.pm index 90d1cd5..f887dd9 100644 --- a/t/lib/MyFoo.pm +++ b/t/lib/MyFoo.pm @@ -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!' } diff --git a/t/lib/mykura.pm b/t/lib/mykura.pm index 9961c83..ac28b5b 100644 --- a/t/lib/mykura.pm +++ b/t/lib/mykura.pm @@ -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;