blib/lib/True/Truth.pm

filter: Statementstmt 30.9% 17 / 55 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 6 / 12 Podpod n/a totaltotal 26.1% 23 / 88 CC 13 SCAR 31.6
CC
13
Cov
23%
merge
56.0
add_pending_truth
20.0
persist_pending_truth
20.0
CRAP 23.5
↑ Summary
1 package True::Truth;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Cache::KyotoTycoon;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Storable qw/nfreeze thaw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dump qw/dump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: merge multiple versions of truth into one
11 #
12 our $VERSION = '1.1'; # VERSION
13  
14 has 'debug' => (
15 is => 'rw',
16 isa => 'Bool',
17 default => sub { 0 },
18 lazy => 1,
19 );
20  
21 has 'kt_server' => (
22 is => 'rw',
23 isa => 'Str',
24 default => '127.0.0.1',
25 );
26  
27 has 'kt_port' => (
28 is => 'rw',
29 isa => 'Int',
30 lazy => 1,
31 default => sub { 1978 },
32 );
33  
34 has 'kt_db' => (
35 is => 'rw',
36 isa => 'Str',
37 lazy => 1,
38 default => sub { 0 },
39 );
40  
41 has 'kt_timeout' => (
42 is => 'rw',
43 isa => 'Int',
44 lazy => 1,
45 default => sub { 5 },
46 );
47  
48 has 'kt' => (
49 is => 'rw',
50 isa => 'Cache::KyotoTycoon',
51 builder => '_connect_kt',
52 lazy => 1,
53 );
54  
55 has 'expire' => (
56 is => 'rw',
57 isa => 'Int',
58 default => '3600',
59 );
60  
61  
62 0 ▶sub add_true_truth {
Subroutine add_true_truth: not called CC=1 SCAR=6.9
63 0 my ($self, $key, $truth) = @_;
64  
65 0 return int $self->_add($key, $truth);
66 }
67  
68  
69 0 ▶sub add_pending_truth {
Subroutine add_pending_truth: not called CC=4 SCAR=30.0
70 0 my ($self, $key, $truth) = @_;
71  
72 0 ▶ return unless ref $truth eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $truth eq "HASH" 0 0 0
73  
74 0 foreach my $ky (keys %$truth) {
75 0 ▶ if (ref($truth->{$ky}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $truth->{$ky} eq 'HASH') { } 0 0 0
76 0 $truth->{$ky}->{_locked} = 1;
77 }
78 else {
79 0 $truth->{_locked} = 1;
80 }
81 }
82 0 return int $self->_add($key, $truth);
83 }
84  
85  
86 0 ▶sub persist_pending_truth {
Subroutine persist_pending_truth: not called CC=4 SCAR=30.0
87 0 my ($self, $key, $index) = @_;
88  
89 0 my $truth = $self->_get($key, $index);
90  
91 0 ▶ return unless ref $truth eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $truth eq "HASH" 0 0 0
92  
93 0 foreach my $k (keys %$truth) {
94 0 ▶ if (ref($truth->{$k}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $truth->{$k} eq 'HASH') { } 0 0 0
95 0 delete $truth->{$k}->{_locked};
96 }
97 else {
98 0 delete $truth->{_locked};
99 }
100 }
101 0 $self->_add($key, $truth, $index);
102 0 return;
103 }
104  
105  
106 0 ▶sub remove_pending_truth {
Subroutine remove_pending_truth: not called CC=1 SCAR=6.9
107 0 my ($self, $key, $index) = @_;
108  
109 0 $self->_del($key, $index);
110 0 return;
111 }
112  
113  
114 0 ▶sub get_true_truth {
Subroutine get_true_truth: not called CC=1 SCAR=6.9
115 0 my ($self, $key) = @_;
116  
117 0 my $all_truth = $self->_get($key);
118 0 my $truth = merge(@$all_truth);
119 0 return $truth;
120 }
121  
122  
123 # This was stolen from Catalyst::Utils... thanks guys!
124 sub merge (@);
125  
126 0 ▶sub merge (@) {
Subroutine merge: not called CC=7 SCAR=40.3
127 shift
128 0 ▶ unless ref $_[0]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
129 ; # Take care of the case we're called like Hash::Merge::Simple->merge(...)
130 0 my ($left, @right) = @_;
131  
132 0 ▶ return $left unless @right;
Branch: 1 branch
BranchTrueFalseTotal
unless @right 0 0 0
133  
134 0 ▶ return merge($left, merge(@right)) if @right > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @right > 1 0 0 0
135  
136 0 my ($right) = @right;
137  
138 0 my %merge = %$left;
139  
140 0 for my $key (keys %$right) {
141  
142 0 my ($hr, $hl) = map { ref $_->{$key} eq 'HASH' } $right, $left;
143  
144 0 ▶ if ($hr and $hl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr and $hl) { } 0 0 0
Condition0%0/3
$hr and $hl
!ll&&!rl&&r
000
MC/DC0%0/2
$hr and $hl
$hr $hl
Truth table0%0/3
A and B
A= $hrB= $hl
ABresult
0X0
100
111
145 0 $merge{$key} = merge($left->{$key}, $right->{$key});
146 }
147 else {
148 0 $merge{$key} = $right->{$key};
149 }
150 }
151  
152 0 return \%merge;
153 }
154  
155 #### internal stuff ####
156  
157 sub _add {
158 my ($self, $key, $val, $index) = @_;
159  
160 my $idx;
161 if ($index) {
162 $idx = $index;
163 }
164 else {
165 $idx = scalar keys $self->kt->match_prefix("$key.");
166 }
167 $self->kt->set("$key.$idx", encode_base64(nfreeze($val)), $self->expire);
168 return $idx;
169 }
170  
171 sub _get {
172 my ($self, $key, $index) = @_;
173  
174 if ($index) {
175 my $val = $self->kt->get("$key.$index");
176 return thaw(decode_base64($val))
177 if $val;
178 }
179 else {
180 my $data = $self->kt->match_prefix($key);
181 my @res;
182 foreach my $val (sort keys %{$data}) {
183 push(@res, thaw(decode_base64($self->kt->get($val))));
184 }
185 return \@res;
186 }
187 return;
188 }
189  
190 sub _del {
191 my ($self, $key, $index) = @_;
192  
193 if ($index) {
194 $self->kt->remove("$key.$index");
195 }
196 else {
197 my $data = $self->kt->match_prefix($key);
198 foreach my $val (sort keys %{$data}) {
199 $self->kt->remove($val);
200 }
201 }
202 return;
203 }
204  
205 sub _connect_kt {
206 my ($self) = @_;
207 return Cache::KyotoTycoon->new(
208 host => $self->kt_server,
209 port => $self->kt_port,
210 timeout => $self->kt_timeout,
211 db => $self->kt_db,
212 );
213 }
214  
215  
216 1; # This is the end of True::Truth
217  
218 __END__
↑ Summary