| 1 | # -*- mode: perl; coding: utf-8 -*- | ||||||||||||||||||||
| 2 | 3 | ▶ | package YATT::ArgTypes; use YATT::Inc; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 3 | ▶ | use base qw(YATT::Class::Configurable); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use YATT::Fields qw(cf_callpack | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | cf_base cf_type_map cf_type_fmt | ||||||||||||||||||||
| 9 | cf_type_name | ||||||||||||||||||||
| 10 | cf_debug | ||||||||||||||||||||
| 11 | 3 | ); | |||||||||||||||||||
| 12 | 3 | ▶ | use YATT::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 3 | ▶ | use YATT::Util::Symbol; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 3 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 17 | 3 | my $pack = shift; | |||||||||||||||||||
| 18 | 3 | my ($callpack) = caller; | |||||||||||||||||||
| 19 | 3 | my @types; | |||||||||||||||||||
| 20 | 3 | my $opts = $pack->new(callpack => $callpack | |||||||||||||||||||
| 21 | , $pack->parse_args(\@_, \@types)); | ||||||||||||||||||||
| 22 | 3 | $opts->add_type($_) for @types; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 3 | ▶ | sub parse_args { | ||||||||||||||||||
|
Subroutine
parse_args: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 26 | 3 | my ($pack, $arglist, $taskqueue) = @_; | |||||||||||||||||||
| 27 | 3 | my @confs; | |||||||||||||||||||
| 28 | 3 | ▶ | while (@$arglist) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 33 | ▶ | if (ref $arglist->[0]) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 30 | 21 | push @$taskqueue, shift @$arglist; | |||||||||||||||||||
| 31 | } elsif (my ($flag, $key) = $arglist->[0] =~ /^([\-:])(\w+)/) { | ||||||||||||||||||||
| 32 | 12 | shift @$arglist; | |||||||||||||||||||
| 33 | 12 | ▶ | my $value = $flag eq ':' ? 1 : shift @$arglist; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 12 | push @confs, $key, $value; | |||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||
| 36 | 0 | croak "Invalid option '$arglist->[0]'"; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | 3 | @confs; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 21 | ▶ | sub add_type { | ||||||||||||||||||
|
Subroutine
add_type: called CC=13 SCAR=27.0
Pod
undocumented
| |||||||||||||||||||||
| 43 | 21 | (my MY $self, my ($desc)) = @_; | |||||||||||||||||||
| 44 | 21 | my $type = shift @$desc; | |||||||||||||||||||
| 45 | 21 | my $fullclass = sprintf $self->{cf_type_fmt}, $type; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 21 | $self->{cf_type_map}{$type} = $fullclass; | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | define_const(globref($fullclass, "type_name"), $type) | ||||||||||||||||||||
| 50 | 21 | ▶ | if $self->{cf_type_name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # t_zzz typealias. | ||||||||||||||||||||
| 53 | 21 | define_const(globref($self->{cf_callpack}, "t_$type"), $fullclass); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 21 | my $fields = fields_hash($self); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 21 | my (@symbols, @tasks, %config); | |||||||||||||||||||
| 58 | 21 | ▶ | while (@$desc) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 27 | ▶ | if (ref $desc->[0] eq 'SCALAR') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 60 | 9 | my ($nameref, $value) = splice @$desc, 0, 2; | |||||||||||||||||||
| 61 | 9 | my $code = do { | |||||||||||||||||||
| 62 | 9 | ▶ | unless (ref $value) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 63 | 0 | ▶ | sub () { $value }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 64 | } elsif (ref $value eq 'CODE') { | ||||||||||||||||||||
| 65 | 0 | $value; | |||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||
| 67 | 0 | die "Unknown ArgType desc for $$nameref : '$value'"; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||
| 70 | 9 | push @symbols, [$$nameref, $code]; | |||||||||||||||||||
| 71 | # *{globref($fullclass, $$nameref)} = $code; | ||||||||||||||||||||
| 72 | } elsif (my ($flag, $key) = $desc->[0] =~ /^([\-:])(\w+)/) { | ||||||||||||||||||||
| 73 | 18 | shift @$desc; | |||||||||||||||||||
| 74 | 18 | ▶ | my $value = $flag eq ':' ? 1 : shift @$desc; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 18 | ▶ | if ($fields->{"cf_$key"}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 3 | $config{"cf_$key"} = $value; | |||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||
| 78 | 15 | ▶ | my $sub = $self->can("option_$key") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | or die "Unknown ArgType option $key"; | ||||||||||||||||||||
| 80 | 15 | push @tasks, [$sub, $value]; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | # $sub->($self, $fullclass, $value); | ||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||
| 84 | 0 | die "Unknown desc type $desc" | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # base だけは eval を使う。 さもないと %FIELDS が作られない。 | ||||||||||||||||||||
| 89 | # *{globref($fullclass, 'ISA')} = [$self->{cf_base}]; | ||||||||||||||||||||
| 90 | $self->checked_eval | ||||||||||||||||||||
| 91 | (sprintf qq{package %s; use base qw(%s)} | ||||||||||||||||||||
| 92 | , $fullclass | ||||||||||||||||||||
| 93 | , $self->lookup_in($self->{cf_type_map}, $config{cf_base}) | ||||||||||||||||||||
| 94 | 21 | ▶ | || $$self{cf_base}); | ||||||||||||||||||
|
Condition66%2/3
$self->lookup_in($self->{'cf_type_map'}, $config{'cf_base'}) || $self->{'cf_base'}
MC/DC0%0/2
$self->lookup_in($self->{'cf_type_map'}, $config{'cf_base'}) || $self->{'cf_base'}
$self->lookup_in($self->{'cf_type_map'}, $config{'cf_base'})
$self->{'cf_base'}
Truth table66%2/3
A || B
A= $self->lookup_in($self->{'cf_type_map'}, $config{'cf_base'})B= $self->{'cf_base'}
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 21 | foreach my $rec (@symbols) { | |||||||||||||||||||
| 97 | 9 | my ($sym, $code) = @$rec; | |||||||||||||||||||
| 98 | 9 | *{globref($fullclass, $sym)} = $code; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 21 | foreach my $rec (@tasks) { | |||||||||||||||||||
| 102 | 15 | my ($sub, $value) = @$rec; | |||||||||||||||||||
| 103 | 15 | $sub->($self, $fullclass, $value, \%config); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 21 | ▶ | sub lookup_in { | ||||||||||||||||||
|
Subroutine
lookup_in: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 108 | 21 | my ($self, $hash, $key) = @_; | |||||||||||||||||||
| 109 | 21 | ▶ | return unless defined $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 3 | $hash->{$key}; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 9 | ▶ | sub option_alias { | ||||||||||||||||||
|
Subroutine
option_alias: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 114 | 9 | (my MY $self, my ($class, $value)) = @_; | |||||||||||||||||||
| 115 | 9 | ▶ | foreach my $alias (ref $value ? @$value : $value) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 12 | $self->{cf_type_map}{$alias} = $class; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 6 | ▶ | sub option_fields { | ||||||||||||||||||
|
Subroutine
option_fields: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 121 | 6 | (my MY $self, my ($class, $value)) = @_; | |||||||||||||||||||
| 122 | 6 | my $fields = terse_dump(@$value); | |||||||||||||||||||
| 123 | 6 | $self->checked_eval(<<END); | |||||||||||||||||||
| 124 | package $class; use YATT::Fields $fields; | ||||||||||||||||||||
| 125 | END | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 3 | 1; | |||||||||||||||||||