lib/Data/TagDB/Base.pm

filter: Statementstmt 17.7% 17 / 96 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 45.4% 5 / 11 Podpod 100% 4 / 4 totaltotal 13.3% 26 / 195 CC 35 SCAR 68.0
CC
35
Cov
10%
_run_plugin
1190.0
clear_cache
6.0
db
2.0
CRAP 899.7
↑ Summary lib/Data/TagDB/Factory.pm »
1 # Copyright (c) 2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: Work with Tag databases
6  
7 package Data::TagDB::Base;
8  
9 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $VERSION = v0.16;
17  
18 4 my %_plugin_attr_pkg_map = (
19 'Data::TagDB' => 'db_attributes',
20 'Data::TagDB::Tag' => 'tag_attributes',
21 'Data::TagDB::Metadata' => 'metadata_attributes',
22 'Data::TagDB::Relation' => 'relation_attributes',
23 );
24  
25  
26  
27 #@returns Data::TagDB
28 0 ▶sub db {
Subroutine db: not called CC=1 SCAR=6.9
Pod documented
29 0 my ($self) = @_;
30 0 return $self->{db};
31 }
32  
33  
34 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
35 0 my ($self, @args) = @_;
36 0 return $self->_run_plugin('get', @args);
37 }
38  
39  
40 0 ▶sub execute {
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
41 0 my ($self, @args) = @_;
42 0 return $self->_run_plugin('execute', @args);
43 }
44  
45  
46 0 ▶sub clear_cache {
Subroutine clear_cache: not called CC=2 SCAR=17.9
Pod documented
47 0 my ($self, @opts) = @_;
48  
49 0 ▶ croak 'Stray options passed' if scalar @opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @opts 0 0 0
50  
51 0 delete $self->{attribute_cache};
52  
53 0 return $self;
54 }
55  
56 # ---- Private helpers ----
57  
58 0 ▶sub _run_plugin {
Subroutine _run_plugin: not called CC=34 SCAR=70.8
59 0 my ($self, $op, $key, %opts) = @_;
60 0 ▶ my __PACKAGE__ $db = $self->isa('Data::TagDB') ? $self : $self->db;
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('Data::TagDB') ? : 0 0 0
61 0 ▶ my $value = ($self->{attribute_cache} //= {})->{$key};
Condition0%0/2
$self->{"attribute_cache"} //= {}
l!l
00
MC/DC0%0/1
$self->{"attribute_cache"} //= {}
$self->{"attribute_cache"}
Truth table0%0/2
A
A= $self->{"attribute_cache"}
Aresult
11
00
62 0 my $has_default = exists $opts{default};
63 0 my $default = delete $opts{default};
64 0 my $listas = delete $opts{listas};
65 0 ▶ my $list = delete($opts{list}) // defined($listas);
Condition0%0/2
delete $opts{"list"} // defined $listas
l!l
00
MC/DC0%0/1
delete $opts{"list"} // defined $listas
delete $opts{"list"}
Truth table0%0/2
A
A= delete $opts{"list"}
Aresult
11
00
66 0 my $as = delete $opts{as};
67 0 my $data;
68  
69 0 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
70  
71 0 ▶ if (!defined($value) || $op eq 'execute') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or $op eq "execute") 0 0 0
Condition0%0/3
not defined $value or $op eq "execute"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or $op eq "execute"
not defined $value $op eq "execute"
Truth table0%0/3
not(A) or B
A= not defined $valueB= $op eq "execute"
ABresult
1X1
011
000
72 0 my ($ns, $subkey) = $key =~ /^([^#]+)#(.+)\z/;
73 0 my $info = $db->_get_plugin_info($ns);
74  
75 0 ▶ croak 'No such plugin loaded' unless defined $info;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info 0 0 0
76  
77 0 ▶ $info = $info->{$_plugin_attr_pkg_map{ref($self)} // '<BAD_PACKAGE>'}{$subkey};
Condition0%0/2
$_plugin_attr_pkg_map{ref $self} // "<BAD_PACKAGE>"
l!l
00
MC/DC0%0/1
$_plugin_attr_pkg_map{ref $self} // "<BAD_PACKAGE>"
$_plugin_attr_pkg_map{ref $self}
Truth table0%0/2
A
A= $_plugin_attr_pkg_map{ref $self}
Aresult
11
00
78  
79 0 ▶ croak 'No such attribute' unless defined $info;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info 0 0 0
80 0 ▶ croak 'Attribute not '.$op.'-able' unless defined $info->{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{$op} 0 0 0
81  
82 {
83 0 my ($v, %extra) = $info->{$op}->($self, $key);
84 0 ▶ unless ($extra{no_value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extra{"no_value"}) 0 0 0
85 0 ▶ $v = [$v] unless ref($v) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 0 0
86 0 $extra{_data} = $v;
87 }
88 0 $value = \%extra;
89  
90 0 ▶ unless ($op eq 'execute' || $extra{no_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op eq "execute" or $extra{"no_cache"}) 0 0 0
Condition0%0/3
$op eq "execute" or $extra{"no_cache"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "execute" or $extra{"no_cache"}
$op eq "execute" $extra{"no_cache"}
Truth table0%0/3
A or B
A= $op eq "execute"B= $extra{"no_cache"}
ABresult
1X1
011
000
91 # We cache the value.
92 0 my $cache_value = $value;
93  
94 0 ▶ unless ($extra{no_value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extra{"no_value"}) 0 0 0
95 # To ensure we build no cyclic references first we check our rawtype.
96 0 my %found = map {ref($_) => undef} @{$v};
97  
98 0 ▶ if (exists $found{'Data::TagDB::Tag'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $found{'Data::TagDB::Tag'}) { } 0 0 0
elsif (&any(sub { /^Data::TagDB::/; } , keys %found)) { } 0 0 0
99 # We can handle Data::TagDB::Tag but only if it's the only one
100 0 ▶ if (scalar(keys %found) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %found == 1) { } 0 0 0
101 0 $cache_value->{rawtype} = 'Data::TagDB::Tag';
102 0 $cache_value = {%{$cache_value}, _data => [map {$_->dbid} @{$v}], _restore_tags => 1}; # build our own object
103 } else {
104 # Sadly we need to refuse to cache
105 0 $cache_value = undef;
106 }
107 0 ▶ } elsif (any {/^Data::TagDB::/} keys %found) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 $cache_value = undef;
109 }
110 }
111  
112 0 ▶ $self->{attribute_cache}{$key} = $cache_value if $cache_value;
Branch: 1 branch
BranchTrueFalseTotal
if $cache_value 0 0 0
113 }
114 }
115 }
116  
117 0 $data = $value->{_data};
118 0 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
119 0 ▶ croak 'No such value' unless $has_default;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_default 0 0 0
120 0 ▶ return @{$default} if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 0 0 0
121 0 return $default;
122 }
123  
124 0 ▶ unless ($list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list) 0 0 0
125 0 ▶ if (scalar(@{$data}) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$data;} != 1) 0 0 0
126 0 croak 'Not a single-value';
127 }
128 }
129  
130 0 ▶ if ($value->{_restore_tags}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"_restore_tags"}) 0 0 0
131 0 $data = [map {$db->tag_by_dbid($_)} @{$data}];
132 }
133  
134 0 ▶ if (defined($as) && $as ne 'raw' && !(defined($value->{rawtype}) && $as eq $value->{rawtype})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $as and $as ne "raw" and not defined $value->{"rawtype"} && $as eq $value->{"rawtype"}) 0 0 0
Condition0%0/9
defined $as and $as ne "raw" and not defined $value->{"rawtype"} && $as eq $value->{"rawtype"}
!ll&&!rl&&r
000
defined $as and $as ne "raw"
!ll&&!rl&&r
000
defined $value->{'rawtype'} && $as eq $value->{'rawtype'}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $as and $as ne "raw" and not defined $value->{"rawtype"} && $as eq $value->{"rawtype"}
defined $as $as ne "raw" defined $value->{'rawtype'} $as eq $value->{'rawtype'}
Truth table0%0/5
A and B and not(C && D)
A= defined $asB= $as ne "raw"C= defined $value->{'rawtype'}D= $as eq $value->{'rawtype'}
ABCDresult
0XXX0
10XX0
11110
110X1
11101
135 0 $data = [map {Data::Identifier::as($_, $as, db => $db, rawtype => $value->{rawtype})} @{$data}];
136 }
137  
138 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 0 0
139 0 ▶ if (defined($listas)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listas) 0 0 0
140 0 ▶ if ($listas eq 'Data::TagDB::Iterator') {
Branch: 2 branches
BranchTrueFalseTotal
if ($listas eq 'Data::TagDB::Iterator') { } 0 0 0
elsif ($listas eq 'Data::TagDB::Cloudlet') { } 0 0 0
141 0 require Data::TagDB::Iterator;
142 0 return Data::TagDB::Iterator->from_array($data, db => $db);
143 } elsif ($listas eq 'Data::TagDB::Cloudlet') {
144 0 require Data::TagDB::Cloudlet;
145 0 return Data::TagDB::Cloudlet->new(root => $data, db => $db);
146 } else {
147 ...
148 0 }
149 }
150  
151 0 return @{$data};
152 } else {
153 0 return $data->[0];
154 }
155 }
156  
157 4 1;
158  
159 __END__
↑ Summary lib/Data/TagDB/Factory.pm »