blib/lib/App/karr/Foundation/Questions.pm

filter: Statementstmt 17.8% 35 / 196 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 95 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 26.6% 8 / 30 Podpod 100% 10 / 10 totaltotal 10.1% 53 / 521 CC 110 SCAR 61.4
CC
110
Cov
8%
_validate_question
1260.0
settle
600.0
resolve
210.0
CRAP 464.1
↑ Summary blib/lib/App/karr/Foundation/ChainStore.pm »
1 # ABSTRACT: karr-foundation question mailbox under refs/karr-foundation/questions/*
2  
3 package App::karr::Foundation::Questions;
4 1 our $VERSION = '0.602';
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use App::karr::Error qw( user_error clean_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::karr::Encoding qw( yaml_dump yaml_load );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 1 has git => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 1 ▶use constant QUESTION_ROOT => 'refs/karr-foundation/questions/';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # How long an answered question is kept. Days rather than a count, unlike the
21 # run logs: what bounds this namespace is that questions are asked by people
22 # and about people, not minted by every step of every run.
23 1 ▶use constant KEEP_ANSWERED_DAYS => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # How many ids a mint may walk past before it gives up. Every attempt means
26 # another writer took that id between this one's read and its write, so a
27 # handful is already generous -- and unbounded would mean a wedged ref turns
28 # into a loop nobody can see.
29 1 ▶use constant MINT_ATTEMPTS => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 my %POLICY = map { $_ => 1 } qw( block use_default escalate_to_ai );
32  
33 1 my %QUESTION_KEY = map { $_ => 1 } qw(
34 id question context options default policy deadline step asked asked_by
35 );
36  
37 1 my %ANSWER_KEY = map { $_ => 1 } qw(
38 id question answer note answered answered_by
39 );
40  
41 0 ▶sub _now { return strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime() ) }
Subroutine _now: not called CC=1 SCAR=6.9
42  
43 # Ids are minted, never given, so this is narrow on purpose: a positive decimal
44 # with no leading zero, which is one ref component and reads back as itself.
45 0 ▶sub _valid_id { return defined $_[0] && "$_[0]" =~ /\A[1-9][0-9]*\z/ }
Subroutine _valid_id: not called CC=2 SCAR=17.9
Condition0%0/3
defined $_[0] && "$_[0]" =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] && "$_[0]" =~ /\A[1-9][0-9]*\z/
defined $_[0] "$_[0]" =~ /\A[1-9][0-9]*\z/
Truth table0%0/3
A && B
A= defined $_[0]B= "$_[0]" =~ /\A[1-9][0-9]*\z/
ABresult
0X0
100
111
46  
47 # Two leaves under the id rather than one ref per question with a sibling
48 # suffix: git cannot hold refs/.../7 and refs/.../7/answer at the same time
49 # (a ref is a file and a directory would have to be the same name), so the id
50 # is the directory and both halves are leaves under it.
51 0 ▶sub _ask_ref { return QUESTION_ROOT . $_[1] . '/ask' }
Subroutine _ask_ref: not called CC=1 SCAR=6.9
52 0 ▶sub _answer_ref { return QUESTION_ROOT . $_[1] . '/answer' }
Subroutine _answer_ref: not called CC=1 SCAR=6.9
53  
54  
55 has keep_answered_days => (
56 is => 'ro',
57 0 ▶ default => sub { KEEP_ANSWERED_DAYS },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 1 );
59  
60  
61 has auto_prune => (
62 is => 'ro',
63 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 1 );
65  
66 # ---------------------------------------------------------------------------
67 # Asking
68 # ---------------------------------------------------------------------------
69  
70 0 ▶sub _validate_question {
Subroutine _validate_question: not called CC=35 SCAR=71.4
71 0 my ( $self, $q ) = @_;
72 0 ▶ user_error('A question must be a mapping') unless ref $q eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $q eq "HASH" 0 0 0
73 0 my %s = %$q;
74  
75 user_error('A question needs a question to ask')
76 0 ▶ unless defined $s{question} && !ref $s{question} && $s{question} =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s{"question"} and not ref $s{"question"} and $s{"question"} =~ /\S/ 0 0 0
Condition0%0/6
defined $s{"question"} and not ref $s{"question"} and $s{"question"} =~ /\S/
!ll&&!rl&&r
000
defined $s{"question"} and not ref $s{"question"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $s{"question"} and not ref $s{"question"} and $s{"question"} =~ /\S/
defined $s{"question"} not ref $s{"question"} $s{"question"} =~ /\S/
Truth table0%0/4
A and not(B) and C
A= defined $s{"question"}B= not ref $s{"question"}C= $s{"question"} =~ /\S/
ABCresult
0XX0
10X0
1100
1111
77 0 $s{question} = "$s{question}";
78  
79 0 for my $key ( qw( context step ) ) {
80 0 ▶ next unless defined $s{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s{$key} 0 0 0
81 0 ▶ user_error("A question's $key must be a plain value") if ref $s{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $s{$key} 0 0 0
82 0 ▶ delete $s{$key} unless length "$s{$key}";
Branch: 1 branch
BranchTrueFalseTotal
unless length "$s{$key}" 0 0 0
83 }
84  
85 # The same latitude the board's frontmatter and a chain step's `needs` give:
86 # YAML writes a one-element list as a scalar often enough that refusing one
87 # would be pedantry.
88 0 ▶ if ( defined $s{options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s{"options"}) 0 0 0
89 0 ▶ my $options = ref $s{options} ? $s{options} : [ $s{options} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $s{'options'} ? : 0 0 0
90 0 ▶ user_error("A question's options must be a list of answers")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options eq "ARRAY" 0 0 0
91 unless ref $options eq 'ARRAY';
92 0 my @options;
93 0 for my $o ( @$options ) {
94 0 ▶ user_error("A question's options must be a list of answers")
Branch: 1 branch
BranchTrueFalseTotal
if ref $o or not defined $o or not length "$o" 0 0 0
Condition0%0/6
ref $o or not defined $o or not length "$o"
l!l&&r!l&&!r
000
ref $o or not defined $o
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $o or not defined $o or not length "$o"
ref $o not defined $o not length "$o"
Truth table0%0/4
A or not(B) or not(C)
A= ref $oB= not defined $oC= not length "$o"
ABCresult
1XX1
01X1
0011
0000
95 if ref $o || !defined $o || !length "$o";
96 0 push @options, "$o";
97 }
98 0 ▶ if ( @options ) { $s{options} = \@options }
Branch: 1 branch
BranchTrueFalseTotal
if (@options) { } 0 0 0
99 0 else { delete $s{options} }
100 }
101  
102 0 ▶ if ( defined $s{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s{"default"}) 0 0 0
103 0 ▶ user_error("A question's default must be a plain value") if ref $s{default};
Branch: 1 branch
BranchTrueFalseTotal
if ref $s{"default"} 0 0 0
104 0 $s{default} = "$s{default}";
105 0 ▶ delete $s{default} unless length $s{default};
Branch: 1 branch
BranchTrueFalseTotal
unless length $s{"default"} 0 0 0
106 }
107 0 ▶ if ( defined $s{default} && $s{options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s{"default"} and $s{"options"}) 0 0 0
Condition0%0/3
defined $s{"default"} and $s{"options"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s{"default"} and $s{"options"}
defined $s{"default"} $s{"options"}
Truth table0%0/3
A and B
A= defined $s{"default"}B= $s{"options"}
ABresult
0X0
100
111
108 user_error("The default '$s{default}' is not one of its options ("
109 0 . join( ', ', @{ $s{options} } ) . ')')
110 0 ▶ unless grep { $_ eq $s{default} } @{ $s{options} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $s{"default"};} @{$s{"options"};} 0 0 0
111 }
112  
113 0 ▶ if ( defined $s{deadline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s{"deadline"}) 0 0 0
114 user_error("A question's deadline must be a UTC stamp like "
115 . _now() . ", not '$s{deadline}'")
116 if ref $s{deadline}
117 0 ▶ || "$s{deadline}" !~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $s{"deadline"} or not qq[$s{"deadline"}] =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z/ 0 0 0
Condition0%0/3
ref $s{"deadline"} or not qq[$s{"deadline"}] =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $s{"deadline"} or not qq[$s{"deadline"}] =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z/
ref $s{"deadline"} not qq[$s{"deadline"}] =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z/
Truth table0%0/3
A or not(B)
A= ref $s{"deadline"}B= not qq[$s{"deadline"}] =~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z/
ABresult
1X1
011
000
118 0 $s{deadline} = "$s{deadline}";
119 }
120  
121 0 ▶ my $policy = $s{policy} // 'block';
Condition0%0/2
$s{"policy"} // "block"
l!l
00
MC/DC0%0/1
$s{"policy"} // "block"
$s{"policy"}
Truth table0%0/2
A
A= $s{"policy"}
Aresult
11
00
122 user_error("A question has unknown policy '$policy' (expected: "
123 . join( ', ', sort keys %POLICY ) . ')')
124 0 ▶ unless !ref $policy && $POLICY{$policy};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $policy and $POLICY{$policy} 0 0 0
Condition0%0/3
not ref $policy and $POLICY{$policy}
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $policy and $POLICY{$policy}
not ref $policy $POLICY{$policy}
Truth table0%0/3
not(A) and B
A= not ref $policyB= $POLICY{$policy}
ABresult
0X0
100
111
125 0 $s{policy} = $policy;
126  
127 # Both of these are the same mistake seen from two sides: a question that
128 # cannot be answered by the time it resolves. Refused here, where a planner
129 # still hears about it, rather than in a runner that would quietly take the
130 # fallback on the tick after it was asked.
131 0 ▶ if ( $policy ne 'block' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy ne "block") 0 0 0
132 user_error("Policy '$policy' needs a deadline: without one it would "
133 . 'resolve the moment the question is asked and nobody could answer it')
134 0 ▶ unless defined $s{deadline};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s{"deadline"} 0 0 0
135 }
136 user_error("Policy 'use_default' needs a default to fall back on")
137 0 ▶ if $policy eq 'use_default' && !defined $s{default};
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "use_default" and not defined $s{"default"} 0 0 0
Condition0%0/3
$policy eq "use_default" and not defined $s{"default"}
!ll&&!rl&&r
000
MC/DC0%0/2
$policy eq "use_default" and not defined $s{"default"}
$policy eq "use_default" not defined $s{"default"}
Truth table0%0/3
A and not(B)
A= $policy eq "use_default"B= not defined $s{"default"}
ABresult
0X0
100
111
138  
139 0 for my $key ( sort keys %s ) {
140 0 ▶ next if $QUESTION_KEY{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $QUESTION_KEY{$key} 0 0 0
141 0 warn "karr-foundation: question: unknown key '$key'\n";
142 }
143 0 return \%s;
144 }
145  
146  
147 0 ▶sub ask {
Subroutine ask: not called CC=12 SCAR=50.5
Pod documented
148 0 my ( $self, %arg ) = @_;
149  
150 0 my $wait = delete $arg{wait};
151 0 ▶ if ( defined $wait ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wait) 0 0 0
152 user_error("A question's wait and its deadline are two spellings of one "
153 0 ▶ . 'thing; pass one of them') if defined $arg{deadline};
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"deadline"} 0 0 0
154 0 ▶ user_error("A question's wait must be a whole number of seconds, not '$wait'")
Branch: 1 branch
BranchTrueFalseTotal
if ref $wait or not "$wait" =~ /\A[0-9]+\z/ 0 0 0
Condition0%0/3
ref $wait or not "$wait" =~ /\A[0-9]+\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $wait or not "$wait" =~ /\A[0-9]+\z/
ref $wait not "$wait" =~ /\A[0-9]+\z/
Truth table0%0/3
A or not(B)
A= ref $waitB= not "$wait" =~ /\A[0-9]+\z/
ABresult
1X1
011
000
155 if ref $wait || "$wait" !~ /\A[0-9]+\z/;
156 0 ▶ user_error('A question that waits no time at all cannot be answered by '
Branch: 1 branch
BranchTrueFalseTotal
unless $wait > 0 0 0 0
157 . 'anybody') unless $wait > 0;
158 0 $arg{deadline} = strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime( time + $wait ) );
159 }
160  
161 0 ▶ $arg{asked} //= _now();
Condition0%0/3
$arg{"asked"} //= _now()
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{"asked"} //= _now()
$arg{"asked"} _now()
Truth table0%0/3
A //= B
A= $arg{"asked"}B= _now()
ABresult
1X1
011
000
162 0 my $who = $self->git->git_user_identity;
163 0 ▶ $arg{asked_by} //= $who if length $who;
Branch: 1 branch
BranchTrueFalseTotal
if length $who 0 0 0
Condition0%0/3
$arg{"asked_by"} //= $who
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{"asked_by"} //= $who
$arg{"asked_by"} $who
Truth table0%0/3
A //= B
A= $arg{"asked_by"}B= $who
ABresult
1X1
011
000
164  
165 0 my $question = $self->_validate_question( \%arg );
166  
167 # Before the write, never after: the only moment this namespace grows is the
168 # one below, so a retention policy hung anywhere else bounds nothing.
169 0 ▶ $self->prune_questions if $self->auto_prune;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_prune 0 0 0
170  
171 0 my $id = $self->_next_id;
172 0 for ( 1 .. MINT_ATTEMPTS ) {
173 0 $question->{id} = $id + 0;
174 0 ▶ return $id
Branch: 1 branch
BranchTrueFalseTotal
if $self->git->write_ref_cas($self->_ask_ref($id), yaml_dump($question), undef) 0 0 0
175 if $self->git->write_ref_cas( $self->_ask_ref($id), yaml_dump($question), undef );
176 0 $id++;
177 }
178 0 user_error( 'Could not find a free question id after ' . MINT_ATTEMPTS
179 . " tries (last tried #$id)" );
180 }
181  
182 # One past the highest id the mailbox has, answer refs included: an answer left
183 # behind by an interrupted delete still holds its id, and handing that id to a
184 # new question would pair the two.
185 0 ▶sub _next_id {
Subroutine _next_id: not called CC=3 SCAR=24.8
186 0 my ( $self ) = @_;
187 0 my $next = 1;
188 0 ▶ for my $id ( $self->ids ) { $next = $id + 1 if $id >= $next }
Branch: 1 branch
BranchTrueFalseTotal
if $id >= $next 0 0 0
189 0 return $next;
190 }
191  
192  
193 0 ▶sub ids {
Subroutine ids: not called CC=3 SCAR=24.8
Pod documented
194 0 my ( $self ) = @_;
195 0 my %id;
196 0 for my $ref ( $self->git->list_refs(QUESTION_ROOT) ) {
197 0 my ( $id ) = substr( $ref, length QUESTION_ROOT ) =~ m{\A([^/]+)/(?:ask|answer)\z};
198 0 ▶ $id{$id} = 1 if _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
if _valid_id($id) 0 0 0
199 }
200 0 my @ids = sort { $a <=> $b } keys %id;
201 0 return @ids;
202 }
203  
204  
205 0 ▶sub question {
Subroutine question: not called CC=4 SCAR=30.0
Pod documented
206 0 my ( $self, $id ) = @_;
207 0 ▶ return undef unless _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
208 0 ▶ my $q = $self->_read_yaml( $self->_ask_ref($id), "question #$id" ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $q = $self->_read_yaml($self->_ask_ref($id), "question #$id") or return undef 0 0 0
209 0 ▶ $q->{id} //= $id + 0;
Condition0%0/2
$q->{"id"} //= $id + 0
l!l
00
MC/DC0%0/1
$q->{"id"} //= $id + 0
$q->{"id"}
Truth table0%0/2
A
A= $q->{"id"}
Aresult
11
00
210 0 return $q;
211 }
212  
213  
214 0 ▶sub questions {
Subroutine questions: not called CC=1 SCAR=6.9
Pod documented
215 0 my ( $self ) = @_;
216 0 return grep { defined } map { $self->question($_) } $self->ids;
217 }
218  
219  
220 0 ▶sub open_questions {
Subroutine open_questions: not called CC=2 SCAR=17.9
Pod documented
221 0 my ( $self ) = @_;
222 return grep {
223 0 my $r = $self->resolve($_);
224 0 ▶ $r && $r->{state} ne 'answered';
Condition0%0/3
$r && $r->{'state'} ne 'answered'
!ll&&!rl&&r
000
MC/DC0%0/2
$r && $r->{'state'} ne 'answered'
$r $r->{'state'} ne 'answered'
Truth table0%0/3
A && B
A= $rB= $r->{'state'} ne 'answered'
ABresult
0X0
100
111
225 } $self->questions;
226 }
227  
228 # ---------------------------------------------------------------------------
229 # Answering
230 # ---------------------------------------------------------------------------
231  
232  
233 0 ▶sub answer {
Subroutine answer: not called CC=2 SCAR=17.9
Pod documented
234 0 my ( $self, $id ) = @_;
235 0 ▶ return undef unless _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
236 0 return $self->_read_yaml( $self->_answer_ref($id), "answer to question #$id" );
237 }
238  
239  
240 0 ▶sub settle {
Subroutine settle: not called CC=24 SCAR=64.0
Pod documented
241 0 my ( $self, $id, $value, %opt ) = @_;
242 0 ▶ user_error("'" . ( defined $id ? $id : '' ) . "' is not a question id")
Branch: 2 branches
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
defined $id ? : 0 0 0
243 unless _valid_id($id);
244 0 ▶ my $q = $self->question($id)
Branch: 1 branch
BranchTrueFalseTotal
my $q = $self->question($id) or user_error("No question #$id is in the mailbox") 0 0 0
245 or user_error("No question #$id is in the mailbox");
246  
247 0 ▶ user_error("An answer cannot be empty")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length "$value" 0 0 0
Condition0%0/6
defined $value and not ref $value and length "$value"
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
defined $value and not ref $value and length "$value"
defined $value not ref $value length "$value"
Truth table0%0/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length "$value"
ABCresult
0XX0
10X0
1100
1111
248 unless defined $value && !ref $value && length "$value";
249 0 $value = "$value";
250  
251 0 ▶ if ( my $options = $q->{options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $options = $q->{"options"}) 0 0 0
252 user_error("Question #$id offers " . join( ', ', @$options )
253 . " (got '$value'); pass force to answer it with something else")
254 0 ▶ unless $opt{force} || grep { $_ eq $value } @$options;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"force"} or grep {$_ eq $value;} @$options 0 0 0
Condition0%0/3
$opt{"force"} or grep {$_ eq $value;} @$options
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"force"} or grep {$_ eq $value;} @$options
$opt{"force"} grep {$_ eq $value;} @$options
Truth table0%0/3
A or B
A= $opt{"force"}B= grep {$_ eq $value;} @$options
ABresult
1X1
011
000
255 }
256  
257 0 my $existing = $self->answer($id);
258 user_error("Question #$id was already answered '$existing->{answer}'"
259 . ( defined $existing->{answered} ? " at $existing->{answered}" : '' )
260 . '; pass force to replace that answer')
261 0 ▶ if $existing && !$opt{force} && $self->_answers( $q, $existing );
Branch: 2 branches
BranchTrueFalseTotal
if $existing and not $opt{"force"} and $self->_answers($q, $existing) 0 0 0
defined $existing->{'answered'} ? : 0 0 0
Condition0%0/6
$existing and not $opt{"force"} and $self->_answers($q, $existing)
!ll&&!rl&&r
000
$existing and not $opt{"force"}
!ll&&!rl&&r
000
MC/DC0%0/3
$existing and not $opt{"force"} and $self->_answers($q, $existing)
$existing not $opt{"force"} $self->_answers($q, $existing)
Truth table0%0/4
A and not(B) and C
A= $existingB= not $opt{"force"}C= $self->_answers($q, $existing)
ABCresult
0XX0
10X0
1100
1111
262  
263 my %a = (
264 id => $id + 0,
265 question => $q->{question},
266 0 answer => $value,
267 answered => _now(),
268 );
269 0 my $who = $self->git->git_user_identity;
270 0 ▶ $a{answered_by} = $who if length $who;
Branch: 1 branch
BranchTrueFalseTotal
if length $who 0 0 0
271 0 ▶ $a{note} = "$opt{note}" if defined $opt{note} && !ref $opt{note} && length $opt{note};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"note"} and not ref $opt{"note"} and length $opt{"note"} 0 0 0
Condition0%0/6
defined $opt{"note"} and not ref $opt{"note"} and length $opt{"note"}
!ll&&!rl&&r
000
defined $opt{"note"} and not ref $opt{"note"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $opt{"note"} and not ref $opt{"note"} and length $opt{"note"}
defined $opt{"note"} not ref $opt{"note"} length $opt{"note"}
Truth table0%0/4
A and not(B) and C
A= defined $opt{"note"}B= not ref $opt{"note"}C= length $opt{"note"}
ABCresult
0XX0
10X0
1100
1111
272  
273 0 for my $key ( sort keys %a ) {
274 0 ▶ next if $ANSWER_KEY{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $ANSWER_KEY{$key} 0 0 0
275 0 warn "karr-foundation: answer to question #$id: unknown key '$key'\n";
276 }
277  
278 0 my $ref = $self->_answer_ref($id);
279 0 ▶ if ( $existing || $opt{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing or $opt{'force'}) { } 0 0 0
Condition0%0/3
$existing or $opt{"force"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$existing or $opt{"force"}
$existing $opt{"force"}
Truth table0%0/3
A or B
A= $existingB= $opt{"force"}
ABresult
1X1
011
000
280 0 ▶ $self->git->write_ref( $ref, yaml_dump( \%a ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->git->write_ref($ref, yaml_dump(\%a)) or user_error("Could not write the answer to question #$id") 0 0 0
281 or user_error("Could not write the answer to question #$id");
282 }
283 else {
284 0 ▶ $self->git->write_ref_cas( $ref, yaml_dump( \%a ), undef )
Branch: 1 branch
BranchTrueFalseTotal
$self->git->write_ref_cas($ref, yaml_dump(\%a), undef) or user_error("Question #$id was answered by somebody else just now") 0 0 0
285 or user_error("Question #$id was answered by somebody else just now");
286 }
287 0 return \%a;
288 }
289  
290 # An answer belongs to the question standing beside it only if it says so. See
291 # "An answer names the question it answers" above: without this, two clones
292 # that minted the same id leave an answer attached to a question it was never
293 # given, and nothing about that looks wrong from the outside.
294 0 ▶sub _answers {
Subroutine _answers: not called CC=6 SCAR=37.4
295 0 my ( $self, $q, $a ) = @_;
296 0 ▶ return 0 unless ref $q eq 'HASH' && ref $a eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $q eq "HASH" and ref $a eq "HASH" 0 0 0
Condition0%0/3
ref $q eq "HASH" and ref $a eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $q eq "HASH" and ref $a eq "HASH"
ref $q eq "HASH" ref $a eq "HASH"
Truth table0%0/3
A and B
A= ref $q eq "HASH"B= ref $a eq "HASH"
ABresult
0X0
100
111
297 0 ▶ return 0 unless defined $a->{question} && defined $q->{question};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a->{"question"} and defined $q->{"question"} 0 0 0
Condition0%0/3
defined $a->{"question"} and defined $q->{"question"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a->{"question"} and defined $q->{"question"}
defined $a->{"question"} defined $q->{"question"}
Truth table0%0/3
A and B
A= defined $a->{"question"}B= defined $q->{"question"}
ABresult
0X0
100
111
298 0 ▶ return $a->{question} eq $q->{question} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a->{'question'} eq $q->{'question'} ? : 0 0 0
299 }
300  
301  
302 0 ▶sub resolve {
Subroutine resolve: not called CC=14 SCAR=53.5
Pod documented
303 0 my ( $self, $id ) = @_;
304 0 ▶ my $q = ref $id eq 'HASH' ? $id : $self->question($id) or return undef;
Branch: 2 branches
BranchTrueFalseTotal
my $q = ref $id eq "HASH" ? $id : $self->question($id) or return undef 0 0 0
ref $id eq 'HASH' ? : 0 0 0
305 0 $id = $q->{id};
306  
307 0 my $a = $self->answer($id);
308 0 ▶ if ( $a && !$self->_answers( $q, $a ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a and not $self->_answers($q, $a)) 0 0 0
Condition0%0/3
$a and not $self->_answers($q, $a)
!ll&&!rl&&r
000
MC/DC0%0/2
$a and not $self->_answers($q, $a)
$a not $self->_answers($q, $a)
Truth table0%0/3
A and not(B)
A= $aB= not $self->_answers($q, $a)
ABresult
0X0
100
111
309 warn "karr-foundation: the answer stored under question #$id answers a "
310 0 ▶ . "different question ('" . ( $a->{question} // '?' ) . "'); it is ignored\n";
Condition0%0/2
$a->{"question"} // "?"
l!l
00
MC/DC0%0/1
$a->{"question"} // "?"
$a->{"question"}
Truth table0%0/2
A
A= $a->{"question"}
Aresult
11
00
311 0 $a = undef;
312 }
313 0 ▶ if ( $a ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a) 0 0 0
314 0 my %r = ( state => 'answered', answer => $a->{answer} );
315 0 for my $key ( qw( answered answered_by note ) ) {
316 0 ▶ $r{$key} = $a->{$key} if defined $a->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->{$key} 0 0 0
317 }
318 0 return \%r;
319 }
320  
321 0 ▶ my $policy = $q->{policy} // 'block';
Condition0%0/2
$q->{"policy"} // "block"
l!l
00
MC/DC0%0/1
$q->{"policy"} // "block"
$q->{"policy"}
Truth table0%0/2
A
A= $q->{"policy"}
Aresult
11
00
322 0 ▶ my $overdue = defined $q->{deadline} && "$q->{deadline}" le _now();
Condition0%0/3
defined $q->{'deadline'} && "$q->{'deadline'}" le _now()
!ll&&!rl&&r
000
MC/DC0%0/2
defined $q->{'deadline'} && "$q->{'deadline'}" le _now()
defined $q->{'deadline'} "$q->{'deadline'}" le _now()
Truth table0%0/3
A && B
A= defined $q->{'deadline'}B= "$q->{'deadline'}" le _now()
ABresult
0X0
100
111
323 0 ▶ return { state => 'open', policy => $policy } unless $overdue;
Branch: 1 branch
BranchTrueFalseTotal
unless $overdue 0 0 0
324 return {
325 state => 'overdue',
326 policy => $policy,
327 ( $policy eq 'use_default' && defined $q->{default}
328 0 ▶ ? ( answer => $q->{default} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$policy eq 'use_default' && defined $q->{'default'} ? : 0 0 0
Condition0%0/3
$policy eq "use_default" and defined $q->{"default"}
!ll&&!rl&&r
000
MC/DC0%0/2
$policy eq "use_default" and defined $q->{"default"}
$policy eq "use_default" defined $q->{"default"}
Truth table0%0/3
A and B
A= $policy eq "use_default"B= defined $q->{"default"}
ABresult
0X0
100
111
329 };
330 }
331  
332 # ---------------------------------------------------------------------------
333 # Retention
334 # ---------------------------------------------------------------------------
335  
336  
337 0 ▶sub delete_question {
Subroutine delete_question: not called CC=4 SCAR=30.0
Pod documented
338 0 my ( $self, $id ) = @_;
339 0 ▶ return 0 unless _valid_id($id);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_id($id) 0 0 0
340 0 ▶ my $removed = $self->git->delete_ref( $self->_ask_ref($id) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->git->delete_ref($self->_ask_ref($id)) ? : 0 0 0
341 0 ▶ $removed += $self->git->delete_ref( $self->_answer_ref($id) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->git->delete_ref($self->_answer_ref($id)) ? : 0 0 0
342 0 return $removed;
343 }
344  
345  
346 0 ▶sub prune_questions {
Subroutine prune_questions: not called CC=8 SCAR=42.8
Pod documented
347 0 my ( $self, %opt ) = @_;
348 0 ▶ my $keep = defined $opt{keep_days} ? $opt{keep_days} : $self->keep_answered_days;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'keep_days'} ? : 0 0 0
349 0 ▶ return () unless $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 0 0 0
350  
351 0 my $cutoff = strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime( time - $keep * 86400 ) );
352 0 my @gone;
353 0 for my $id ( $self->ids ) {
354 0 ▶ my $a = $self->answer($id) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $a = $self->answer($id) or next 0 0 0
355 0 my $when = $a->{answered};
356 0 ▶ next unless defined $when && !ref $when && "$when" lt $cutoff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $when and not ref $when and "$when" lt $cutoff 0 0 0
Condition0%0/6
defined $when and not ref $when and "$when" lt $cutoff
!ll&&!rl&&r
000
defined $when and not ref $when
!ll&&!rl&&r
000
MC/DC0%0/3
defined $when and not ref $when and "$when" lt $cutoff
defined $when not ref $when "$when" lt $cutoff
Truth table0%0/4
A and not(B) and C
A= defined $whenB= not ref $whenC= "$when" lt $cutoff
ABCresult
0XX0
10X0
1100
1111
357 0 $self->delete_question($id);
358 0 push @gone, $id;
359 }
360 0 return @gone;
361 }
362  
363 # ---------------------------------------------------------------------------
364 # Reading refs
365 # ---------------------------------------------------------------------------
366  
367 0 ▶sub _read_yaml {
Subroutine _read_yaml: not called CC=4 SCAR=30.0
368 0 my ( $self, $ref, $what ) = @_;
369 0 my $content = $self->git->read_ref($ref);
370 0 ▶ return undef unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 0 0 0
Condition0%0/3
defined $content and length $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table0%0/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
371 0 ▶ my $data = try { yaml_load($content) } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
372 0 warn "karr-foundation: cannot read $what: " . clean_error($_) . "\n";
373 0 undef;
374 0 };
375 0 ▶ return ref $data eq 'HASH' ? $data : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 0 0 0
376 }
377  
378 1 1;
379  
380 __END__
↑ Summary blib/lib/App/karr/Foundation/ChainStore.pm »