File Coverage

blib/lib/Lingua/Awkwords/ListOf.pm
Criterion Covered Total %
statement 36 36 100.0
branch n/a
condition 1 2 50.0
subroutine 8 8 100.0
pod 4 4 100.0
total 49 50 98.0


line stmt bran cond sub pod time code
1             # -*- Perl -*-
2             #
3             # instances of this object hold a list of choices that are all rendered
4             # together as a string
5              
6             package Lingua::Awkwords::ListOf;
7              
8 4     4   89034 use strict;
  4         14  
  4         97  
9 4     4   18 use warnings;
  4         6  
  4         83  
10              
11 4     4   1241 use Moo;
  4         30728  
  4         14  
12 4     4   4991 use namespace::clean;
  4         31353  
  4         20  
13              
14             our $VERSION = '0.07';
15              
16             has filters => (
17             is => 'rwp',
18             default => sub { [] },
19             );
20             has filter_with => (
21             is => 'rw',
22             default => sub { '' },
23             );
24             has terms => (
25             is => 'rwp',
26             default => sub { [] },
27             );
28              
29             ########################################################################
30             #
31             # METHODS
32              
33             sub add {
34 1     1 1 2 my $self = shift;
35 1         1 push @{ $self->terms }, @_;
  1         4  
36 1         2 return $self;
37             }
38              
39 7     7 1 12 sub add_filters { my $self = shift; push @{ $self->filters }, @_; return $self }
  7         11  
  7         22  
  7         16  
40              
41             sub render {
42 80     80 1 1137 my ($self) = @_;
43              
44 80         96 my $str = '';
45 80         102 for my $term ( @{ $self->terms } ) {
  80         166  
46 103         231 $str .= $term->render;
47             }
48 80   50     176 my $filter_with = $self->filter_with // '';
49 80         86 for my $filter ( @{ $self->filters } ) {
  80         122  
50 16         138 $str =~ s/\Q$filter/$filter_with/g;
51             }
52 80         213 return $str;
53             }
54              
55             sub walk {
56 9     9 1 15 my ($self, $callback) = @_;
57 9         17 $callback->($self);
58 9         14 for my $term ( @{ $self->terms } ) {
  9         18  
59 13         27 $term->walk($callback);
60             }
61 9         17 return;
62             }
63              
64             1;
65             __END__