File Coverage

blib/lib/WWW/Shopify/Liquid/Operator/Concatenate.pm
Criterion Covered Total %
statement 61 61 100.0
branch 24 26 92.3
condition 14 18 77.7
subroutine 11 11 100.0
pod 0 7 0.0
total 110 123 89.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 37     37   15681 use strict;
  37         108  
  37         1130  
4 37     37   258 use warnings;
  37         98  
  37         1453  
5              
6             # Used in our AST to keep things simple; represents the concatenation of text and other stuff.
7             # Making this a many-dimensional operator, so that we avoid going too far down the callstack rabbithole.
8             package WWW::Shopify::Liquid::Operator::Concatenate;
9 37     37   229 use base 'WWW::Shopify::Liquid::Operator';
  37         90  
  37         3959  
10 37     37   295 use Scalar::Util qw(blessed);
  37         131  
  37         27594  
11 1321     1321 0 4234 sub symbol { return ('~'); }
12 342     342 0 1447 sub arity { return "nary"; }
13 10625     10625 0 24735 sub priority { return 9; }
14             sub operate {
15 379     379 0 1630 my ($self, $hash, $action, @ops) = @_;
16 379         1665 $ops[$_] = '' for (grep { !defined $ops[$_] } 0..$#ops);
  2150         6473  
17            
18 379         1135 return join("", grep { defined $_ } @ops);
  2150         7725  
19             }
20              
21             sub process {
22 428     428 0 1341 my ($self, $hash, $action, $pipeline) = @_;
23 428         890 my @ops = @{$self->{operands}};
  428         1636  
24 428         1793 for my $idx (0..$#ops) {
25 2329         4871 eval {
26 2329 100       8043 $ops[$idx] = $ops[$idx]->$action($pipeline, $hash) unless $self->is_processed($ops[$idx]);
27 2320 100       8910 $self->{operands}->[$idx] = $ops[$idx] if $action eq "optimize";
28             };
29 2329 100       7846 if (my $exp = $@) {
30 9 50 66     127 $exp->initial_render(@ops[0..($idx-1)]) if $idx > 0 && blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control');
      66        
31 9 100 66     77 if (blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control::Pause')) {
32 5         39 $exp->register_value($self->{operands}->[$_], $ops[$_]) for (0..($idx-1));
33             }
34 9         51 die $exp;
35             }
36             }
37            
38 419 100 66     1231 if (int(grep { !$self->is_processed($_) } @ops) > 0 && $action eq "optimize") {
  2309         6916  
39 40         106 my $result;
40             my @new_ops;
41 40 100 100     101 for (map { blessed($_) && $_->isa('WWW::Shopify::Liquid::Operator::Concatenate') ? (@{$_->{operands}}) : ($_) } @ops) {
  159         970  
  3         15  
42 164 100       467 if ($self->is_processed($_)) {
43 92 100       327 $result .= $_ if defined $_;
44             } else {
45 72         241 push(@new_ops, $result);
46 72         147 $result = '';
47 72         199 push(@new_ops, $_);
48             }
49            
50             }
51 40 100       168 push(@new_ops, $result) if $result ne '';
52 40         133 $self->{operands} = \@new_ops;
53 40         168 return $self;
54             }
55            
56 379         1575 return $self->operate($hash, $action, @ops);
57             }
58              
59             sub optimize {
60 61     61 0 226 my ($self, $optimizer, $hash) = @_;
61 61         310 my $result = $self->SUPER::optimize($optimizer, $hash);
62 61 100 100     153 $self->{operands} = [grep { !$self->is_processed($_) || (defined $_ && $_ ne '') } @{$self->{operands}}];
  222         665  
  61         181  
63 61 100       191 return $self->{operands}->[0] if int(@{$self->{operands}}) == 1;
  61         238  
64 54         231 return $result;
65             }
66              
67             sub new {
68 224     224 0 560 my $package = shift;
69 224         458 my $line = shift;
70 224         458 my $core = shift;
71 224         1064 my $self = bless { line => $line, core => $core, operands => undef }, $package;
72 224 50       1266 $self->{operands} = [@_] if int(@_) >= 1;
73 224         810 return $self;
74             }
75              
76             1;