File Coverage

blib/lib/Bootylicious/ArticleByQueryIterator.pm
Criterion Covered Total %
statement 41 41 100.0
branch 6 6 100.0
condition 5 6 83.3
subroutine 8 8 100.0
pod 1 2 50.0
total 61 63 96.8


line stmt bran cond sub pod time code
1             package Bootylicious::ArticleByQueryIterator;
2              
3 5     5   789 use strict;
  5         11  
  5         197  
4 5     5   25 use warnings;
  5         6  
  5         165  
5              
6 5     5   27 use base 'Bootylicious::Decorator';
  5         9  
  5         945  
7              
8             __PACKAGE__->attr('query');
9             __PACKAGE__->attr('before_context' => 10);
10             __PACKAGE__->attr('after_context' => 10);
11             __PACKAGE__->attr('replace_string_before' => '');
12             __PACKAGE__->attr('replace_string_after' => '');
13              
14 5     5   35 use Mojo::ByteStream 'b';
  5         6  
  5         345  
15 5     5   508 use Bootylicious::IteratorSearchable;
  5         8  
  5         43  
16              
17             sub new {
18 3     3 1 25 my $self = shift->SUPER::new(@_);
19              
20 3         12 return $self->build;
21             }
22              
23             sub build {
24 3     3 0 5 my $self = shift;
25              
26 3         12 my $before_context = $self->before_context;
27 3         27 my $after_context = $self->after_context;
28              
29 3         103 my $replace_string_before = $self->replace_string_before;
30 3         25 my $replace_string_after = $self->replace_string_after;
31              
32 3         23 my $q = quotemeta $self->query;
33              
34             return Bootylicious::IteratorSearchable->new($self->object)->find_all(
35             sub {
36 6     6   30 my ($iterator, $article) = @_;
37              
38 6         8 my $found = 0;
39              
40 6         21 my $title = $article->title;
41 6 100 66     144 if ( $title
42             && $title
43             =~ s/($q)/$replace_string_before$1$replace_string_after/isg)
44             {
45 2         5 $found = 1;
46 2         10 $article->title($title);
47             }
48              
49 6         13 my $parts = [];
50 6         27 my $content = $article->content;
51 6   100     142 while ($content
52             && $content
53             =~ s/((?:.{$before_context})?$q(?:.{$after_context})?)//is)
54             {
55 1         4 my $part = $1;
56 1         6 $part = b($part)->xml_escape->to_string;
57 1         52 $part
58             =~ s/($q)/$replace_string_before$1$replace_string_after/isg;
59 1         5 push @$parts, $part;
60              
61 1         9 $found = 1;
62             }
63              
64 6 100       57 return unless $found;
65              
66 2 100       10 $article->content($parts) if @$parts;
67              
68 2         11 return $article;
69             }
70 3         25 );
71             }
72              
73             1;