blib/lib/Aion/Pleroma.pm

filter: Statementstmt 94.8% 130 / 137 Branchbran 79.4% 54 / 68 Conditioncond 65.3% 32 / 49 MC/DCmcdc 35.7% 10 / 28 Subroutinesub 100% 22 / 22 Podpod 100% 3 / 3 totaltotal 81.7% 251 / 307 CC 60 SCAR 18.3
CC
60
Cov
85%
__ANON__
11.6
_build
9.8
__ANON__
8.1
CRAP 6.3
« blib/lib/Aion.pm ↑ Summary blib/lib/Aion/Types.pm »
1 package Aion::Pleroma;
2 # Контейнер для эонов (сервисов)
3  
4 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Aion::Env AION_PLEROMA_INI => (default => 'etc/annotation/eon.ann');
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Aion::Env AION_PLEROMA_AUTOWARE => (default => 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Aion::Env::Etc EON => (default => {}, key => 'aion.eon');
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Aion;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Файл с аннотациями
13 2 has ini => (is => 'ro', isa => Maybe[Str], default => AION_PLEROMA_INI);
14  
15 # Конфигурация: ключ => хеш-описание эона (class, method, arguments, calls)
16 8 ▶has pleroma => (is => 'ro?!', isa => HashRef, default => sub {
Subroutine __ANON__: called CC=10 SCAR=24.5
17 8 my ($self) = @_;
18  
19 8 $self->{pleroma} = {};
20 8 $self->autoware('Aion::Pleroma');
21 0 for my $key (keys %{&EON}) { $self->autoware(EON->{$key}, $key) }
22  
23 8 ▶ return $self->pleroma unless defined $self->ini and -e $self->ini;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ini and -e $self->ini 1 7 8
Condition66%2/3
defined $self->ini and -e $self->ini
!ll&&!rl&&r
017
MC/DC50%1/2
defined $self->ini and -e $self->ini
defined $self->ini -e $self->ini
Truth table66%2/3
A and B
A= defined $self->iniB= -e $self->ini
ABresult
0X0
100
111
24  
25 0 ▶ open my $f, '<:encoding(UTF-8)', $self->ini or die "Not open ${\$self->ini}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $f, "<:encoding(UTF-8)", $self->ini or die "Not open ${\$self->ini;}: $!" 7 0 7
26 7 ▶ while(<$f>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $f)) 21 7 28
27 0 ▶ do { close $f; die "${\$self->ini} corrupt at line $.: $_" } unless /^([\w:]+)#(\w*),\d+=(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless (/^([\w:]+)#(\w*),\d+=(.*)$/u) 0 21 21
28 21 my ($pkg, $sub, $key) = ($1, $2, $3);
29  
30 21 ▶ $key = $key ne ""? $key: ($sub? "$pkg#$sub": $pkg);
Branch: 2 branches
BranchTrueFalseTotal
$key ne '' ? : 14 7 21
$sub ? : 0 7 7
31  
32 21 ▶ $self->autoware(+{class => $pkg, method => $sub || 'new'}, $key);
Condition100%2/2
$sub || 'new'
l!l
147
MC/DC100%1/1
$sub || 'new'
$sub
Truth table100%2/2
A
A= $sub
Aresult
11
00
33 }
34 7 close $f;
35  
36 7 $self->pleroma
37 2 });
38  
39 # Совокупность порождённых эонов-сервисов
40 11 ▶has eon => (is => 'ro', isa => HashRef[Object], lazy => 0, default => sub { +{'Aion::Pleroma' => shift} });
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 # Получить эон из контейнера
43 21 ▶sub get {
Subroutine get: called CC=5 SCAR=16.4
Pod documented
44 21 my ($self, $key) = @_;
45
46 21 my $eon = $self->{eon}{$key};
47 21 ▶ return $eon if $eon;
Branch: 1 branch
BranchTrueFalseTotal
if $eon 4 17 21
48
49 17 my $config = _hash_config($self->pleroma->{$key});
50 17 ▶ if($config) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config) { } 12 5 17
elsif ($key =~ /^([\w:]+)(#\w+)?$/u and _find_pm($1)) { } 0 5 5
51 12 $self->{eon}{$key} = $self->_build($key, $config);
52 }
53 0 ▶ elsif(AION_PLEROMA_AUTOWARE and $key =~ /^([\w:]+)(#\w+)?$/ and _find_pm($1)) { $self->autoware($key)->get($key) }
Condition66%2/3
$key =~ /^([\w:]+)(#\w+)?$/u and _find_pm($1)
!ll&&!rl&&r
320
MC/DC0%0/2
$key =~ /^([\w:]+)(#\w+)?$/u and _find_pm($1)
$key =~ /^([\w:]+)(#\w+)?$/u _find_pm($1)
Truth table66%2/3
A and B
A= $key =~ /^([\w:]+)(#\w+)?$/uB= _find_pm($1)
ABresult
0X0
100
111
54 5 else { undef }
55 }
56  
57 # Получить эон из контейнера или исключение, если его там нет
58 11 ▶sub resolve {
Subroutine resolve: called CC=2 SCAR=6.9
Pod documented
59 11 my ($self, $key) = @_;
60
61 11 ▶ $self->get($key) // die "$key is'nt eon!";
Condition100%2/2
$self->get($key) // die("$key is'nt eon!")
l!l
92
MC/DC0%0/1
$self->get($key) // die("$key is'nt eon!")
$self->get($key)
Truth table50%1/2
A
A= $self->get($key)
Aresult
11
00
62 }
63  
64 # Добавить в плерому эон. $config — строка 'класс#метод' либо хеш (class, method, arguments, calls)
65 37 ▶sub autoware {
Subroutine autoware: called CC=8 SCAR=20.8
Pod documented
66 37 my ($self, $config, $key) = @_;
67  
68 37 ▶ $config = _hash_config($config) unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 13 24 37
69 37 ▶ my $pkg = $config->{class} // die "autoware: class is undef!";
Condition50%1/2
$config->{"class"} // die("autoware: class is undef!")
l!l
370
MC/DC0%0/1
$config->{"class"} // die("autoware: class is undef!")
$config->{"class"}
Truth table50%1/2
A
A= $config->{"class"}
Aresult
11
00
70 37 ▶ my $sub = $config->{method} // 'new';
Condition100%2/2
$config->{"method"} // "new"
l!l
352
MC/DC100%1/1
$config->{"method"} // "new"
$config->{"method"}
Truth table100%2/2
A
A= $config->{"method"}
Aresult
11
00
71 37 my $action = "$pkg#$sub";
72 37 ▶ $key //= $sub eq 'new'? $pkg: $action;
Branch: 1 branch
BranchTrueFalseTotal
$sub eq 'new' ? : 10 2 12
Condition66%2/3
$key //= $sub eq "new" ? $pkg : $action
l!l&&r!l&&!r
25120
MC/DC0%0/2
$key //= $sub eq "new" ? $pkg : $action
$key $sub eq "new" ? $pkg : $action
Truth table66%2/3
A //= B
A= $keyB= $sub eq "new" ? $pkg : $action
ABresult
1X1
011
000
73  
74 37 my $pleroma = $self->pleroma;
75 37 my $exists = $pleroma->{$key};
76 37 ▶ if(defined $exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exists) 3 34 37
77 3 my $str = _string_config($exists);
78 3 ▶ die "Added eon $key twice, with $action ne $str" if $str ne $action;
Branch: 1 branch
BranchTrueFalseTotal
if $str ne $action 1 2 3
79 2 return $self;
80 }
81  
82 34 $pleroma->{$key} = $config;
83  
84 # При добавлении нового ключа проверяем, не замкнулся ли цикл ссылок
85 34 $self->_check_cycle($key);
86  
87 33 $self
88 }
89  
90 # Пройти по @-ссылкам от $key и выбросить исключение, если цепочка замкнулась на себя
91 34 ▶sub _check_cycle {
Subroutine _check_cycle: called CC=1 SCAR=0.0
92 34 my ($self, $start) = @_;
93  
94 34 my %visiting;
95 my $walk;
96 37 ▶ $walk = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
97 37 my ($key, $path) = @_;
98  
99 die "Circular eon dependency: " . join(' -> ', @$path, $key)
100 37 ▶ if exists $visiting{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $visiting{$key} 1 36 37
101  
102 36 my $pleroma = $self->pleroma;
103 36 ▶ my $conf = _hash_config($pleroma->{$key}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conf = _hash_config($pleroma->{$key}) or return 35 1 36
104  
105 35 $visiting{$key} = 1;
106 35 $walk->($_, [@$path, $key]) for _conf_refs($conf);
107 33 delete $visiting{$key};
108 34 };
109  
110 34 $walk->($start, []);
111 }
112  
113 # Ключи эонов, на которые ссылается конфигурация (значения, начинающиеся с '@')
114 35 ▶sub _conf_refs {
Subroutine _conf_refs: called CC=2 SCAR=7.0
115 35 my ($conf) = @_;
116 35 ▶ return () unless $conf;
Branch: 1 branch
BranchTrueFalseTotal
unless $conf 0 35 35
117 35 my @refs;
118 my $collect;
119 120 ▶ $collect = sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
120 120 my ($v) = @_;
121 120 ▶ if(defined $v && !ref $v) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $v and not ref $v) { } 78 42 120
elsif (ref $v eq 'ARRAY') { } 6 36 42
elsif (ref $v eq 'HASH') { } 36 0 36
Condition66%2/3
defined $v and not ref $v
!ll&&!rl&&r
04278
MC/DC50%1/2
defined $v and not ref $v
defined $v not ref $v
Truth table66%2/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
122 78 ▶ push @refs, $1 if $v =~ /^@(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^@(.+)/u 3 75 78
123 }
124 6 elsif(ref $v eq 'ARRAY') { $collect->($_) for @$v }
125 36 elsif(ref $v eq 'HASH') { $collect->($_) for values %$v }
126 35 };
127 35 $collect->($conf);
128 @refs
129 35 }
130  
131 # Построить эон из хеш-описания: class, method, arguments, calls
132 12 ▶sub _build {
Subroutine _build: called CC=9 SCAR=22.9
133 12 my ($self, $key, $conf) = @_;
134  
135 12 ▶ my $pkg = $conf->{class} // ($key =~ /^[\w:]+$/a ? $key : undef)
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^[\w:]+$/a ? : 0 0 0
Condition40%2/5
($conf->{"class"} // ($key =~ /^[\w:]+$/a ? $key : undef)) // die("Eon ${key}: class is undef! Optional 'class' key or key-as-class.")
l!l
120
$conf->{"class"} // ($key =~ /^[\w:]+$/a ? $key : undef)
l!l&&r!l&&!r
1200
MC/DC0%0/2
($conf->{"class"} // ($key =~ /^[\w:]+$/a ? $key : undef)) // die("Eon ${key}: class is undef! Optional 'class' key or key-as-class.")
$conf->{"class"} ($key =~ /^[\w:]+$/a ? $key : undef)
Truth table33%1/3
A // B
A= $conf->{"class"}B= ($key =~ /^[\w:]+$/a ? $key : undef)
ABresult
1X1
011
000
136 // die "Eon $key: class is undef! Optional 'class' key or key-as-class.";
137 12 ▶ my $method = $conf->{method} // 'new';
Condition100%2/2
$conf->{"method"} // "new"
l!l
93
MC/DC100%1/1
$conf->{"method"} // "new"
$conf->{"method"}
Truth table100%2/2
A
A= $conf->{"method"}
Aresult
11
00
138  
139 12 _require($pkg);
140  
141 12 ▶ my $args = $conf->{arguments} // {};
Condition100%2/2
$conf->{"arguments"} // {}
l!l
39
MC/DC100%1/1
$conf->{"arguments"} // {}
$conf->{"arguments"}
Truth table100%2/2
A
A= $conf->{"arguments"}
Aresult
11
00
142 12 $args = $self->_resolve_args($args);
143  
144 12 my $eon = $self->_construct("$pkg#$method", $args);
145  
146 12 ▶ for my $call (@{ $conf->{calls} // [] }) {
Condition100%2/2
$conf->{"calls"} // []
l!l
111
MC/DC100%1/1
$conf->{"calls"} // []
$conf->{"calls"}
Truth table100%2/2
A
A= $conf->{"calls"}
Aresult
11
00
147 1 my ($name, @call_args);
148 1 ▶ if(ref $call eq 'ARRAY') { ($name, @call_args) = @$call }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $call eq 'ARRAY') { } 1 0 1
149 0 else { $name = $call }
150 1 @call_args = map { $self->_resolve_value($_) } @call_args;
151 1 $eon->$name(@call_args);
152 }
153  
154 $eon
155 12 }
156  
157 # Вызвать конструктор: именованные (hashref) или упорядоченные (arrayref) аргументы
158 12 ▶sub _construct {
Subroutine _construct: called CC=3 SCAR=11.0
159 12 my ($self, $action, $args) = @_;
160 12 ▶ my ($pkg, $method) = $action =~ /^([^#]+)#(.+)$/? ($1, $2): ($action, 'new');
Branch: 1 branch
BranchTrueFalseTotal
$action =~ /^([^#]+)#(.+)$/u ? : 12 0 12
161  
162 12 _require($pkg);
163  
164 12 ▶ ref($args) eq 'ARRAY'? $pkg->$method(@$args): $pkg->$method(%$args);
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 1 11 12
165 }
166  
167 # Подгрузить пакет, если он ещё не определён в памяти
168 24 ▶sub _require {
Subroutine _require: called CC=3 SCAR=11.0
169 24 my ($pkg) = @_;
170 24 ▶ return if $pkg->can('new') || $pkg->can('does');
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->can("new") or $pkg->can("does") 17 7 24
Condition66%2/3
$pkg->can("new") or $pkg->can("does")
l!l&&r!l&&!r
1707
MC/DC50%1/2
$pkg->can("new") or $pkg->can("does")
$pkg->can("new") $pkg->can("does")
Truth table66%2/3
A or B
A= $pkg->can("new")B= $pkg->can("does")
ABresult
1X1
011
000
171 7 $pkg =~ s!::!/!g;
172 7 $pkg =~ s!\z!.pm!;
173 7 require $pkg;
174 }
175  
176 # Есть ли файл такого пакета на @INC (для ветки autoware в get)
177 2 ▶sub _find_pm {
Subroutine _find_pm: called CC=6 SCAR=19.3
178 2 my ($pkg) = @_;
179 2 ▶ return 1 if $pkg->can('new') || $pkg->can('does');
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->can("new") or $pkg->can("does") 0 2 2
Condition33%1/3
$pkg->can("new") or $pkg->can("does")
l!l&&r!l&&!r
002
MC/DC0%0/2
$pkg->can("new") or $pkg->can("does")
$pkg->can("new") $pkg->can("does")
Truth table33%1/3
A or B
A= $pkg->can("new")B= $pkg->can("does")
ABresult
1X1
011
000
180 2 my $path = $pkg =~ s!::!/!gr;
181 2 $path .= '.pm';
182 2 for my $dir (@INC) {
183 20 ▶ next if ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dir 0 20 20
184 20 ▶ return 1 if -e "$dir/$path";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$dir/$path" 0 20 20
185 }
186 ''
187 2 }
188  
189 # Строковое действие -> хеш-конфигурация {class, method}
190 66 ▶sub _hash_config {
Subroutine _hash_config: called CC=5 SCAR=16.1
191 66 my ($action) = @_;
192 66 ▶ return $action if ref $action eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $action eq "HASH" 44 22 66
193 22 ▶ return undef unless defined $action && $action ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action and $action ne "" 6 16 22
Condition66%2/3
defined $action and $action ne ""
!ll&&!rl&&r
6016
MC/DC50%1/2
defined $action and $action ne ""
defined $action $action ne ""
Truth table66%2/3
A and B
A= defined $actionB= $action ne ""
ABresult
0X0
100
111
194 16 ▶ my ($pkg, $method) = $action =~ /^([^#]+)#(.+)$/? ($1, $2): ($action, 'new');
Branch: 1 branch
BranchTrueFalseTotal
$action =~ /^([^#]+)#(.+)$/u ? : 6 10 16
195 16 +{ class => $pkg, method => $method };
196 }
197  
198 # Хеш-конфигурация -> строковое действие 'класс#метод'
199 3 ▶sub _string_config {
Subroutine _string_config: called CC=3 SCAR=12.5
200 3 my ($conf) = @_;
201 3 ▶ return undef unless defined $conf && ref $conf eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf and ref $conf eq "HASH" 0 3 3
Condition33%1/3
defined $conf and ref $conf eq "HASH"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $conf and ref $conf eq "HASH"
defined $conf ref $conf eq "HASH"
Truth table33%1/3
A and B
A= defined $confB= ref $conf eq "HASH"
ABresult
0X0
100
111
202 3 join '#', @$conf{qw/class method/};
203 }
204  
205 # Заменить в аргументах ссылки "@key" на эоны
206 12 ▶sub _resolve_args {
Subroutine _resolve_args: called CC=2 SCAR=6.9
207 12 my ($self, $args) = @_;
208 12 ▶ if(ref $args eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'ARRAY') { } 1 11 12
209 1 +[ map { $self->_resolve_value($_) } @$args ]
210 }
211 else {
212 11 +{ map { $_ => $self->_resolve_value($args->{$_}) } keys %$args }
213 }
214 }
215  
216 # Значение, начинающееся с "@", воспринимается как ссылка на эон
217 8 ▶sub _resolve_value {
Subroutine _resolve_value: called CC=4 SCAR=14.6
218 8 my ($self, $val) = @_;
219 8 ▶ return $val unless defined $val && !ref $val && $val =~ /^@(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and not ref $val and $val =~ /^@(.+)/u 6 2 8
Condition50%3/6
defined $val and not ref $val and $val =~ /^@(.+)/u
!ll&&!rl&&r
062
defined $val and not ref $val
!ll&&!rl&&r
008
MC/DC33%1/3
defined $val and not ref $val and $val =~ /^@(.+)/u
defined $val not ref $val $val =~ /^@(.+)/u
Truth table50%2/4
A and not(B) and C
A= defined $valB= not ref $valC= $val =~ /^@(.+)/u
ABCresult
0XX0
10X0
1100
1111
220 2 $self->resolve($1);
221 }
222  
223 2 1;
224  
225 __END__
« blib/lib/Aion.pm ↑ Summary blib/lib/Aion/Types.pm »