File Coverage

blib/lib/Sieve/Generator/Element/IfElse.pm
Criterion Covered Total %
statement 36 36 100.0
branch 8 10 80.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 0 2 0.0
total 52 56 92.8


line stmt bran cond sub pod time code
1 1     1   10 use v5.36.0;
  1         2  
2             package Sieve::Generator::Element::IfElse 0.003;
3             # ABSTRACT: a Sieve if/elsif/else conditional construct
4              
5 1     1   3 use Moo;
  1         1  
  1         4  
6             with 'Sieve::Generator::Element';
7              
8             #pod =head1 DESCRIPTION
9             #pod
10             #pod An C object renders a Sieve C/C/C construct. It
11             #pod consists of a required condition and true-branch, optional additional
12             #pod condition/branch pairs for C clauses, and an optional final C
13             #pod branch.
14             #pod
15             #pod =attr cond
16             #pod
17             #pod This attribute holds the condition for the C clause. It may be a plain
18             #pod string or an object doing L.
19             #pod
20             #pod =attr true
21             #pod
22             #pod This attribute holds the block or command to execute when the C condition
23             #pod is true. It should be an object doing L.
24             #pod
25             #pod =attr elsifs
26             #pod
27             #pod This attribute holds an arrayref of alternating condition/block pairs for
28             #pod C clauses. Each pair follows the same rules as C and C.
29             #pod If not provided, no C clauses are rendered.
30             #pod
31             #pod =attr else
32             #pod
33             #pod This attribute holds the block or command for the plain C clause. If
34             #pod not provided, no C clause is rendered.
35             #pod
36             #pod =cut
37              
38             has cond => (is => 'ro', required => 1);
39             has true => (is => 'ro', required => 1, init_arg => 'true');
40             has elsifs => (is => 'ro');
41             has else => (is => 'ro');
42              
43 4     4 0 4 sub children ($self) {
  4         4  
  4         5  
44 4         22 my @children = ($self->cond, $self->true);
45 4 50       11 push @children, $self->elsifs->@* if $self->elsifs;
46 4 50       8 push @children, $self->else if $self->else;
47 4         8 return @children;
48             }
49              
50 25     25 0 1770 sub as_sieve ($self, $i = undef) {
  25         26  
  25         29  
  25         23  
51 25   100     67 $i //= 0;
52 25         41 my $indent = q{ } x $i;
53              
54 25         25 my $str = q{};
55              
56 25         25 my $in_else;
57              
58 1     1   434 use experimental qw(for_list);
  1         3  
  1         8  
59 25 100       167 for my ($cond, $block) ($self->cond, $self->true, ($self->elsifs ? $self->elsifs->@* : ())) {
60 26         65 my $cond_str = $cond->as_sieve($i);
61 26         247 $cond_str =~ s/\A\Q$indent\E//;
62              
63 26 100       48 if ($in_else) {
64 1         4 $str .= " elsif $cond_str " . $block->as_sieve($i);
65             } else {
66 25         78 $str .= $indent . "if $cond_str " . $block->as_sieve($i);
67             }
68 26         60 $in_else = 1;
69             }
70              
71 25 100       51 if ($self->else) {
72 2         5 $str .= " else " . $self->else->as_sieve($i);
73             }
74              
75 25         43 return $str;
76             }
77              
78 1     1   169 no Moo;
  1         1  
  1         3  
79             1;
80              
81             __END__