| 1 | package kura; | ||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 6 | our $VERSION = "0.10"; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use Sub::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | ▶ | use Scalar::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | my %FORBIDDEN_NAME = map { $_ => 1 } qw{ | |||||||||||||||||||
| 12 | BEGIN CHECK DESTROY END INIT UNITCHECK | ||||||||||||||||||||
| 13 | AUTOLOAD STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG | ||||||||||||||||||||
| 14 | }; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 6 | my @ALLOWED_CONSTRAINT_CLASSES = qw( | |||||||||||||||||||
| 17 | Data::Validator | ||||||||||||||||||||
| 18 | Poz::Types | ||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 19 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 19 | my $pkg = shift; | |||||||||||||||||||
| 23 | 19 | my $caller = caller; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 19 | $pkg->import_into($caller, @_); | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Import into the caller package. | ||||||||||||||||||||
| 29 | 21 | ▶ | sub import_into { | ||||||||||||||||||
|
Subroutine
import_into: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 30 | 21 | my $pkg = shift; | |||||||||||||||||||
| 31 | 21 | my ($caller, $name, $constraint) = @_; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 21 | my ($kura_item, $err) = _new_kura_item($caller, $name, $constraint); | |||||||||||||||||||
| 34 | 21 | ▶ | Carp::croak $err if $err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 14 | _save_kura_item($kura_item, $caller); | |||||||||||||||||||
| 37 | 14 | _save_inc($caller); | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # Create a constraint object. | ||||||||||||||||||||
| 41 | # | ||||||||||||||||||||
| 42 | # @param $constraint Defined. Following `create_constraint` function allows these types: Object, CodeRef, HashRef. | ||||||||||||||||||||
| 43 | # @param $opts Dict[name => Str, caller => Str] | ||||||||||||||||||||
| 44 | # @return ($constraint, undef) | (undef, $error_message) | ||||||||||||||||||||
| 45 | # | ||||||||||||||||||||
| 46 | # NOTE: This function is a hook point. If you want to customize the constraint object, you can override this function. | ||||||||||||||||||||
| 47 | 15 | ▶ | sub create_constraint { | ||||||||||||||||||
|
Subroutine
create_constraint: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||
| 48 | 15 | my ($constraint, $opts) = @_; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 15 | ▶ | if (my $blessed = Scalar::Util::blessed($constraint)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 51 | 9 | ▶ | return _create_constraint_from_typetiny($constraint, $opts) if $constraint->isa('Type::Tiny'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 7 | ▶ | return ($constraint, undef) if $constraint->can('check'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 1 | ▶ | return ($constraint, undef) if grep { $constraint->isa($_) } @ALLOWED_CONSTRAINT_CLASSES; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 1 | return (undef, "Invalid constraint. Object must have a `check` method or allowed constraint class: $blessed"); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | elsif (my $reftype = Scalar::Util::reftype($constraint)) { | ||||||||||||||||||||
| 57 | 5 | ▶ | if ($reftype eq 'CODE') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 58 | 4 | return _create_constraint_from_coderef($constraint, $opts); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | elsif ($reftype eq 'HASH') { | ||||||||||||||||||||
| 61 | 1 | return _create_constraint_from_hashref($constraint, $opts); | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | return (undef, 'Invalid constraint'); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # Create a constraint object from a Type::Tiny object. | ||||||||||||||||||||
| 69 | 2 | ▶ | sub _create_constraint_from_typetiny { | ||||||||||||||||||
|
Subroutine
_create_constraint_from_typetiny: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 70 | 2 | my ($type, $opts) = @_; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 2 | ▶ | $type->{name} = $opts->{name} if $type->is_anon; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 2 | return ($type, undef); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # Create a constraint object from a code reference. | ||||||||||||||||||||
| 78 | 4 | ▶ | sub _create_constraint_from_coderef { | ||||||||||||||||||
|
Subroutine
_create_constraint_from_coderef: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 4 | my ($coderef, $opts) = @_; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 4 | require Type::Tiny; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 4 | my $args = {}; | |||||||||||||||||||
| 84 | 4 | $args->{name} = $opts->{name}; | |||||||||||||||||||
| 85 | 4 | ▶ | $args->{constraint} = sub { !!eval { $coderef->($_[0]) } }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 0 | ▶ | $args->{message} = sub { sprintf('%s did not pass the constraint "%s"', Type::Tiny::_dd($_[0]), $args->{name}) }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 4 | return (Type::Tiny->new(%$args), undef); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # Create a constraint object from a hash reference. | ||||||||||||||||||||
| 92 | 1 | ▶ | sub _create_constraint_from_hashref { | ||||||||||||||||||
|
Subroutine
_create_constraint_from_hashref: called CC=4 SCAR=15.2
| |||||||||||||||||||||
| 93 | 1 | my ($args, $opts) = @_; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | ▶ | my $blessed = delete $args->{blessed} || 'Type::Tiny'; | ||||||||||||||||||
|
Condition50%1/2
delete $args->{'blessed'} || 'Type::Tiny'
MC/DC0%0/1
delete $args->{'blessed'} || 'Type::Tiny'
delete $args->{'blessed'}
Truth table50%1/2
A
A= delete $args->{'blessed'}
| |||||||||||||||||||||
| 96 | 1 | ▶ | eval "require $blessed" or die $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 1 | ▶ | $args->{name} //= $opts->{name}; | ||||||||||||||||||
|
Condition33%1/3
$args->{"name"} //= $opts->{"name"}
MC/DC0%0/2
$args->{"name"} //= $opts->{"name"}
$args->{"name"}
$opts->{"name"}
Truth table33%1/3
A //= B
A= $args->{"name"}B= $opts->{"name"}
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | return ($blessed->new(%$args), undef); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # Create a new kura item which is Dict[name => Str, code => CodeRef]. | ||||||||||||||||||||
| 104 | # If the name or constraint is invalid, it returns (undef, $error_message). | ||||||||||||||||||||
| 105 | # Otherwise, it returns ($kura_item, undef). | ||||||||||||||||||||
| 106 | 21 | ▶ | sub _new_kura_item { | ||||||||||||||||||
|
Subroutine
_new_kura_item: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 107 | 21 | my ($caller, $name, $constraint) = @_; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | { | ||||||||||||||||||||
| 110 | 21 | ▶ | return (undef, 'name is required') if !defined $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 20 | ▶ | return (undef, "'$name' is forbidden.") if $FORBIDDEN_NAME{$name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 19 | ▶ | return (undef, "'$name' is already defined") if $caller->can($name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 18 | ▶ | return (undef, 'constraint is required') if !defined $constraint; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 17 | ($constraint, my $err) = create_constraint($constraint, { name => $name, caller => $caller }); | |||||||||||||||||||
| 117 | 17 | ▶ | return (undef, $err) if $err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | # Prefix '_' means private, so it is not exported. | ||||||||||||||||||||
| 120 | 14 | ▶ | my $is_private = $name =~ /^_/ ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 14 | ▶ | my $kura_item = { name => $name, code => sub { $constraint }, is_private => $is_private }; | ||||||||||||||||||
|
Subroutine
_PrivateFoo: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 123 | 14 | return ($kura_item, undef); | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # Save the kura item to the caller package | ||||||||||||||||||||
| 127 | 14 | ▶ | sub _save_kura_item { | ||||||||||||||||||
|
Subroutine
_save_kura_item: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 128 | 14 | my ($kura_item, $caller) = @_; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 14 | my $name = $kura_item->{name}; | |||||||||||||||||||
| 131 | 14 | my $code = Sub::Util::set_subname("$caller\::$name", $kura_item->{code}); | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 6 | ▶ | no strict "refs"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 134 | 6 | ▶ | no warnings "once"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 135 | 14 | *{"$caller\::$name"} = $code; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 14 | ▶ | if (!$kura_item->{is_private}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 13 | push @{"$caller\::EXPORT_OK"}, $name; | |||||||||||||||||||
| 139 | 13 | push @{"$caller\::KURA"}, $name; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 14 | return; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # Hack to make the caller package already loaded. Useful for multi-packages in a single file. | ||||||||||||||||||||
| 146 | 14 | ▶ | sub _save_inc { | ||||||||||||||||||
|
Subroutine
_save_inc: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 147 | 14 | my ($caller) = @_; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 14 | ( my $file = $caller ) =~ s{::}{/}g; | |||||||||||||||||||
| 150 | 14 | ▶ | $INC{"$file.pm"} ||= __FILE__; | ||||||||||||||||||
|
Condition100%2/2
$INC{"$file.pm"} ||= __FILE__
MC/DC100%1/1
$INC{"$file.pm"} ||= __FILE__
$INC{"$file.pm"}
Truth table100%2/2
A
A= $INC{"$file.pm"}
| |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 14 | return; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 6 | 1; | |||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||