| 1 | package Aion::Meta::FeatureConstruct; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 6 | ▶ | use common::sense; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 6 | ▶ | use Aion::Meta::Util qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | Aion::Meta::Util::create_getters(qw/ | |||||||||||||||||||
| 8 | pkg name | ||||||||||||||||||||
| 9 | write read | ||||||||||||||||||||
| 10 | getvar ret | ||||||||||||||||||||
| 11 | /); | ||||||||||||||||||||
| 12 | 6 | Aion::Meta::Util::create_accessors(qw/ | |||||||||||||||||||
| 13 | init_arg | ||||||||||||||||||||
| 14 | set get has clear weaken | ||||||||||||||||||||
| 15 | accessor_name reader_name writer_name predicate_name clearer_name | ||||||||||||||||||||
| 16 | initer not_specified | ||||||||||||||||||||
| 17 | getter setter selfret | ||||||||||||||||||||
| 18 | /); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Конструктор | ||||||||||||||||||||
| 21 | 58 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 22 | 58 | my ($cls, $pkg, $name) = @_; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 58 | ▶ | bless { | ||||||||||||||||||
|
Condition33%1/3
ref $cls || $cls
MC/DC0%0/2
ref $cls || $cls
ref $cls
$cls
Truth table33%1/3
A || B
A= ref $clsB= $cls
| |||||||||||||||||||||
| 25 | pkg => $pkg, | ||||||||||||||||||||
| 26 | name => $name, | ||||||||||||||||||||
| 27 | initializer => <<'END', | ||||||||||||||||||||
| 28 | if (exists $value{%(init_arg)s}) { | ||||||||||||||||||||
| 29 | %(initer)s | ||||||||||||||||||||
| 30 | }%(not_specified)s | ||||||||||||||||||||
| 31 | END | ||||||||||||||||||||
| 32 | destroyer => <<'END', | ||||||||||||||||||||
| 33 | if (%(has)s) { | ||||||||||||||||||||
| 34 | eval { | ||||||||||||||||||||
| 35 | %(cleaner)s | ||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||
| 37 | warn $@ if $@; | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | END | ||||||||||||||||||||
| 40 | accessor => <<'END', | ||||||||||||||||||||
| 41 | package %(pkg)s { | ||||||||||||||||||||
| 42 | sub %(accessor_name)s%(attr)s { | ||||||||||||||||||||
| 43 | if (@_>1) { | ||||||||||||||||||||
| 44 | my ($self, $val) = @_; | ||||||||||||||||||||
| 45 | %(setter)s | ||||||||||||||||||||
| 46 | %(selfret)s | ||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||
| 48 | my ($self) = @_; | ||||||||||||||||||||
| 49 | %(getter)s | ||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | END | ||||||||||||||||||||
| 54 | reader => <<'END', | ||||||||||||||||||||
| 55 | package %(pkg)s { | ||||||||||||||||||||
| 56 | sub %(reader_name)s { | ||||||||||||||||||||
| 57 | my ($self) = @_; | ||||||||||||||||||||
| 58 | %(read)s | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | END | ||||||||||||||||||||
| 62 | writer => <<'END', | ||||||||||||||||||||
| 63 | package %(pkg)s { | ||||||||||||||||||||
| 64 | sub %(writer_name)s { | ||||||||||||||||||||
| 65 | my ($self, $val) = @_; | ||||||||||||||||||||
| 66 | %(write)s | ||||||||||||||||||||
| 67 | %(selfret)s | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | END | ||||||||||||||||||||
| 71 | predicate => <<'END', | ||||||||||||||||||||
| 72 | package %(pkg)s { | ||||||||||||||||||||
| 73 | sub %(predicate_name)s { | ||||||||||||||||||||
| 74 | my ($self) = @_; | ||||||||||||||||||||
| 75 | %(has)s | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | END | ||||||||||||||||||||
| 79 | clearer => <<'END', | ||||||||||||||||||||
| 80 | package %(pkg)s { | ||||||||||||||||||||
| 81 | sub %(clearer_name)s { | ||||||||||||||||||||
| 82 | my ($self) = @_; | ||||||||||||||||||||
| 83 | if (%(has)s) { | ||||||||||||||||||||
| 84 | %(cleaner)s%(clear)s | ||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | %(clearret)s | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | END | ||||||||||||||||||||
| 90 | accessor_name => '%(name)s', | ||||||||||||||||||||
| 91 | reader_name => '_get_%(name)s', | ||||||||||||||||||||
| 92 | writer_name => '_set_%(name)s', | ||||||||||||||||||||
| 93 | attr => '', | ||||||||||||||||||||
| 94 | write => '%(preset)s%(set)s%(trigger)s', | ||||||||||||||||||||
| 95 | read => '%(access)s%(getvar)s%(release)s%(ret)s', | ||||||||||||||||||||
| 96 | setter => '%(write)s', | ||||||||||||||||||||
| 97 | getter => '%(read)s', | ||||||||||||||||||||
| 98 | initer => "%(initvar)s%(write)s", | ||||||||||||||||||||
| 99 | init_arg => '%(name)s', | ||||||||||||||||||||
| 100 | initvar => 'my $val = delete $value{%(init_arg)s};', | ||||||||||||||||||||
| 101 | not_specified => '', | ||||||||||||||||||||
| 102 | preset => '', | ||||||||||||||||||||
| 103 | set => '$self->{%(name)s} = $val;', | ||||||||||||||||||||
| 104 | trigger => '', | ||||||||||||||||||||
| 105 | selfret => '$self', | ||||||||||||||||||||
| 106 | access => '', | ||||||||||||||||||||
| 107 | getvar => '%(get)s', | ||||||||||||||||||||
| 108 | get => '$self->{%(name)s}', | ||||||||||||||||||||
| 109 | release => '', | ||||||||||||||||||||
| 110 | ret => '', | ||||||||||||||||||||
| 111 | predicate_name => 'has_%(name)s', | ||||||||||||||||||||
| 112 | has => 'exists $self->{%(name)s}', | ||||||||||||||||||||
| 113 | clearer_name => 'clear_%(name)s', | ||||||||||||||||||||
| 114 | clear => 'delete $self->{%(name)s}', | ||||||||||||||||||||
| 115 | clearret => '$self', | ||||||||||||||||||||
| 116 | cleaner => '', | ||||||||||||||||||||
| 117 | weaken => 'Scalar::Util::weaken(%(get)s);', | ||||||||||||||||||||
| 118 | }, ref $cls || $cls; | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 5 | ▶ | sub add_attr { shift->_expand('attr', @_) } | ||||||||||||||||||
|
Subroutine
add_attr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 122 | 34 | ▶ | sub add_preset { shift->_expand('preset', @_) } | ||||||||||||||||||
|
Subroutine
add_preset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 123 | 3 | ▶ | sub add_trigger { shift->_expand('trigger', @_) } | ||||||||||||||||||
|
Subroutine
add_trigger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 124 | 2 | ▶ | sub add_cleaner { shift->_expand('cleaner', @_) } | ||||||||||||||||||
|
Subroutine
add_cleaner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 125 | 8 | ▶ | sub add_access { shift->_expand('access', @_) } | ||||||||||||||||||
|
Subroutine
add_access: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 126 | 30 | ▶ | sub add_release { | ||||||||||||||||||
|
Subroutine
add_release: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 127 | 30 | my $self = shift; | |||||||||||||||||||
| 128 | 30 | ▶ | @$self{qw/getvar ret/} = ('my $val = %(get)s;', '$val') if $self->{ret} eq ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 30 | $self->_expand('release', @_) | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 82 | ▶ | sub _expand(@) { | ||||||||||||||||||
|
Subroutine
_expand: called CC=5 SCAR=16.3
| |||||||||||||||||||||
| 133 | 82 | my ($self, $key, $code, $shift) = @_; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 82 | ▶ | if(ref $self->{$key}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 136 | 1 | ▶ | if($shift) { unshift @{$self->{$key}}, $code } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 0 | else { push @{$self->{$key}}, $code } | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | elsif ($self->{$key} eq '') { | ||||||||||||||||||||
| 140 | 76 | $self->{$key} = $code; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||
| 143 | 5 | ▶ | $self->{$key} = $shift? [$code, $self->{$key}]: [$self->{$key}, $code]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 82 | $self | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 6 | for my $key (qw/initializer destroyer accessor writer reader predicate clearer/) { | |||||||||||||||||||
| 150 | 134 | ▶ | *$key = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 151 | 134 | my ($self) = @_; | |||||||||||||||||||
| 152 | 134 | _idents($self->_resolv($self->{$key})) | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | 42 | } | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 1970 | ▶ | sub _resolv { | ||||||||||||||||||
|
Subroutine
_resolv: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 157 | 1970 | my ($self, $s) = @_; | |||||||||||||||||||
| 158 | 1970 | ▶ | $s = join '', @$s if ref $s; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 1970 | $s =~ s{%\((\w*)\)s}{ | |||||||||||||||||||
| 160 | 1836 | ▶ | die "has: not construct `$1`\!" unless exists $self->{$1}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 1836 | $self->_resolv($self->{$1}) | |||||||||||||||||||
| 162 | }ge; | ||||||||||||||||||||
| 163 | 1970 | $s | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 134 | ▶ | sub _idents { | ||||||||||||||||||
|
Subroutine
_idents: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 167 | 134 | local ($_) = @_; | |||||||||||||||||||
| 168 | 134 | my $indent; | |||||||||||||||||||
| 169 | 134 | s{(^\t*)|;[\t ]*(\S)}{ | |||||||||||||||||||
| 170 | 1247 | ▶ | if(defined $1) { $indent = $1 } else { ";\n$indent$2" } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | }gme; | ||||||||||||||||||||
| 172 | 134 | $_ | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 6 | 1; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||