Skip to content

Commit

Permalink
Enable to create a kura of your own choice.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfly8 committed Aug 18, 2024
1 parent d8e7715 commit c08ea59
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/kura.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ use Carp ();
use Sub::Util ();
use Scalar::Util ();

my %forbidden_kura_name = map { $_ => 1 } qw{
my %FORBIDDEN_NAME = map { $_ => 1 } qw{
BEGIN CHECK DESTROY END INIT UNITCHECK
AUTOLOAD STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG
};

# This is a default Exporter class.
# 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) = @_;

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

sub import {
my $pkg = shift;
my $caller = caller;
Expand All @@ -28,10 +43,10 @@ sub import_into {
state $validate_name = sub {
my ($name) = @_;

if (!$name) {
if (!defined $name) {
return 'name is required';
}
elsif ($forbidden_kura_name{$name}) {
elsif ($FORBIDDEN_NAME{$name}) {
return "'$name' is forbidden.";
}
return;
Expand All @@ -54,13 +69,10 @@ sub import_into {
};

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

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

sub { $checker };
Expand All @@ -73,7 +85,7 @@ sub import_into {
return "'$name' is already defined";
}

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

{
no strict "refs";
Expand All @@ -87,7 +99,7 @@ sub import_into {
state $setup_exporter = sub {
my ($caller) = @_;

my $exporter_class = 'Exporter';
my $exporter_class = $EXPORTER_CLASS;

unless ($caller->isa($exporter_class)) {
no strict "refs";
Expand Down
24 changes: 24 additions & 0 deletions t/02-import_into.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use Test2::V0;
use lib 't/lib';
use MyChecker;

subtest 'Test `import_into` method' => sub {
subtest 'Checker is imported into $target_package' => sub {
package Foo {}
my $target_package = 'Foo';

use kura ();
kura->import_into($target_package, Hello => MyChecker->new);

isa_ok Foo::Hello(), 'MyChecker';
};

subtest 'So, you can customize the import method to your taste' => sub {
use MyKura Foo => MyChecker->new;

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

done_testing;
18 changes: 18 additions & 0 deletions t/lib/MyKura.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package MyKura;
use strict;
use warnings;

use kura ();

sub import {
my $class = shift;
my $caller = caller;

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

$name = 'My' . $name;

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

1;

0 comments on commit c08ea59

Please sign in to comment.