File Coverage

blib/lib/Sieve/Generator/Lines/IfElse.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 10 100.0
condition 2 2 100.0
subroutine 5 5 100.0
pod 0 1 0.0
total 49 50 98.0


line stmt bran cond sub pod time code
1 1     1   9 use v5.36.0;
  1         3  
2             package Sieve::Generator::Lines::IfElse 0.001;
3             # ABSTRACT: a Sieve if/elsif/else conditional construct
4              
5 1     1   4 use Moo;
  1         1  
  1         4  
6             with 'Sieve::Generator::Lines';
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 24     24 0 2718 sub as_sieve ($self, $i = undef) {
  24         44  
  24         38  
  24         30  
44 24   100     110 $i //= 0;
45 24         48 my $indent = q{ } x $i;
46              
47 24         45 my $str = q{};
48              
49 24         42 my $in_else;
50              
51 1     1   333 use experimental qw(for_list);
  1         1  
  1         7  
52 24 100       217 for my ($cond, $block) ($self->cond, $self->true, ($self->elsifs ? $self->elsifs->@* : ())) {
53 25 100       98 my $cond_str = ref $cond ? $cond->as_sieve($i) : $cond;
54 25 100       222 $cond_str =~ s/\A\Q$indent\E// if ref $cond;
55 25         63 chomp $cond_str;
56              
57 25 100       55 if ($in_else) {
58 1         2 chomp $str;
59 1         6 $str .= " elsif $cond_str " . $block->as_sieve($i);
60             } else {
61 24         115 $str .= $indent . "if $cond_str " . $block->as_sieve($i);
62             }
63 25         55 $in_else = 1;
64             }
65              
66 24 100       69 if ($self->else) {
67 2         4 chomp $str;
68 2         7 $str .= " else " . $self->else->as_sieve($i);
69             }
70              
71 24         106 return $str;
72             }
73              
74 1     1   211 no Moo;
  1         1  
  1         3  
75             1;
76              
77             __END__