| 1 | # -*- mode: perl; coding: utf-8 -*- | ||||||||||
| 2 | package YATT::Class::Configurable; | ||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 12 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 12 | our %FIELDS; | |||||||||
| 7 | 12 | ▶ | use fields; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | sub MY () {__PACKAGE__} | ||||||||||
| 9 | 12 | ▶ | use YATT::Util::Symbol qw(fields_hash globref); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 12 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 6304 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 13 | 6304 | my MY $self = fields::new(shift); | |||||||||
| 14 | 6304 | $self->before_configure; | |||||||||
| 15 | 6304 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 5266 | $self->init(@_); | |||||||||
| 17 | } else { | ||||||||||
| 18 | 1038 | $self->after_configure; | |||||||||
| 19 | } | ||||||||||
| 20 | 6304 | $self | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 3899 | ▶ | sub initargs {return} | ||||||||
|
Subroutine
initargs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | |||||||||||
| 25 | 5266 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 26 | 5266 | my MY $self = shift; | |||||||||
| 27 | 5266 | ▶ | if (my @member = $self->initargs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 1909 | @{$self}{@member} = splice @_, 0, scalar @member; | |||||||||
| 29 | } | ||||||||||
| 30 | 5266 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 5249 | $self->configure(@_); | |||||||||
| 32 | } else { | ||||||||||
| 33 | 17 | $self->after_configure; | |||||||||
| 34 | } | ||||||||||
| 35 | 5266 | $self; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 29 | ▶ | sub refid { | ||||||||
|
Subroutine
refid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | 29 | $_[0] + 0; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | sub stringify { | ||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 43 | 0 | my MY $self = shift; | |||||||||
| 44 | 0 | require Data::Dumper; | |||||||||
| 45 | sprintf '%s->new(%s)', ref $self | ||||||||||
| 46 | , join ", ", Data::Dumper->new | ||||||||||
| 47 | 0 | ([map($self->{$_}, $self->initargs) | |||||||||
| 48 | , $self->configure])->Terse(1)->Indent(0)->Dump; | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 542 | ▶ | sub clone { | ||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 52 | 542 | my MY $ref = shift; | |||||||||
| 53 | 542 | ref($ref)->new(map($ref->{$_}, $ref->initargs) | |||||||||
| 54 | , $ref->configure | ||||||||||
| 55 | , @_); | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 2092 | ▶ | sub cget { | ||||||||
|
Subroutine
cget: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 59 | 2092 | (my MY $self, my ($cf)) = @_; | |||||||||
| 60 | 2092 | $cf =~ s/^-//; # For Tcl/Tk co-operatability. | |||||||||
| 61 | 2092 | my $fields = fields_hash($self); | |||||||||
| 62 | 2092 | ▶ | croak "Can't cget $cf" unless exists $fields->{"cf_$cf"}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 2092 | $self->{"cf_$cf"}; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub cgetlist { | ||||||||
|
Subroutine
cgetlist: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 67 | 0 | (my MY $self) = shift; | |||||||||
| 68 | map { | ||||||||||
| 69 | 0 | ▶ | if (exists $self->{"cf_$_"}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | ($_ => $self->{"cf_$_"}) | |||||||||
| 71 | } else { | ||||||||||
| 72 | () | ||||||||||
| 73 | 0 | } | |||||||||
| 74 | } @_; | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | 6304 | ▶ | sub before_configure {} | ||||||||
|
Subroutine
before_configure: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub configkeys { | ||||||||
|
Subroutine
configkeys: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 81 | 0 | my MY $self = shift; | |||||||||
| 82 | return map { | ||||||||||
| 83 | 0 | ▶ | if (m/^cf_(.*)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | $1 | |||||||||
| 85 | } else { | ||||||||||
| 86 | () | ||||||||||
| 87 | 0 | } | |||||||||
| 88 | } keys %$self; | ||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 35 | ▶ | sub can_configure { | ||||||||
|
Subroutine
can_configure: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 92 | 35 | (my MY $self, my ($name)) = @_; | |||||||||
| 93 | 35 | my $fields = fields_hash($self); | |||||||||
| 94 | 35 | ▶ | exists $fields->{"cf_$name"} || $self->can("configure_$name"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 7142 | ▶ | sub configure { | ||||||||
|
Subroutine
configure: called CC=11 SCAR=25.1
Pod
undocumented
| |||||||||||
| 98 | 7142 | my MY $self = shift; | |||||||||
| 99 | 7142 | my $fields = fields_hash($self); | |||||||||
| 100 | 7142 | ▶ | unless (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | # list all configurable options. | ||||||||||
| 102 | return map { | ||||||||||
| 103 | 775 | ▶ | if (m/^cf_(.*)/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 4841 | ($1 => $self->{$_}) | |||||||||
| 105 | } else { | ||||||||||
| 106 | () | ||||||||||
| 107 | 1417 | } | |||||||||
| 108 | } keys %$fields; | ||||||||||
| 109 | } | ||||||||||
| 110 | 6367 | ▶ | if (@_ == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | ▶ | croak "No such config item: $_[0]" unless exists $fields->{"cf_$_[0]"}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | return $self->{"cf_$_[0]"}; | |||||||||
| 113 | } | ||||||||||
| 114 | 6367 | ▶ | if (@_ % 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | croak "Odd number of arguments"; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 6367 | my @task; | |||||||||
| 119 | 6367 | ▶ | while (my ($name, $value) = splice @_, 0, 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 20923 | ▶ | croak "undefined name for configure" unless defined $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 20923 | ▶ | if (my $sub = $self->can("configure_$name")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 139 | push @task, [$sub, $value]; | |||||||||
| 123 | } else { | ||||||||||
| 124 | 20784 | ▶ | croak "No such config item: $name" unless exists $fields->{"cf_$name"}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 20784 | $self->{"cf_$name"} = $value; | |||||||||
| 126 | } | ||||||||||
| 127 | } | ||||||||||
| 128 | 6367 | foreach my $task (@task) { | |||||||||
| 129 | 139 | $task->[0]->($self, $task->[1]); | |||||||||
| 130 | } | ||||||||||
| 131 | 6367 | $self->after_configure; | |||||||||
| 132 | 6367 | $self; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 7290 | ▶ | sub after_configure { | ||||||||
|
Subroutine
after_configure: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 136 | 7290 | my MY $self = shift; | |||||||||
| 137 | # $self->SUPER::after_configure; | ||||||||||
| 138 | 7290 | foreach my $cf (grep {/^cf_/} keys %{fields_hash($self)}) { | |||||||||
| 139 | 34669 | ▶ | next if defined $self->{$cf}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | # XXX: should be: | ||||||||||
| 141 | # (my $name = $cf) =~ s/^cf_//; | ||||||||||
| 142 | # my $sub = $self->can("default_$name") or next; | ||||||||||
| 143 | 16646 | ▶ | my $sub = $self->can("default_$cf") or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 676 | $self->{$cf} = $sub->(); | |||||||||
| 145 | } | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 62 | ▶ | sub define { | ||||||||
|
Subroutine
define: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 149 | 62 | my ($class, $method, $sub) = @_; | |||||||||
| 150 | # XXX: API 以外の関数は弾くべきかもしれない。 | ||||||||||
| 151 | 62 | *{globref($class, $method)} = $sub; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 12 | 1; | |||||||||