File Coverage

blib/lib/WWW/Shopify/Liquid/Operator/Concatenate.pm
Criterion Covered Total %
statement 12 59 20.3
branch 0 24 0.0
condition 0 18 0.0
subroutine 4 11 36.3
pod 0 7 0.0
total 16 119 13.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 30     30   15463 use strict;
  30         63  
  30         910  
4 30     30   127 use warnings;
  30         42  
  30         1039  
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 30     30   120 use base 'WWW::Shopify::Liquid::Operator';
  30         43  
  30         2147  
10 30     30   153 use Scalar::Util qw(blessed);
  30         49  
  30         19239  
11 0     0 0   sub symbol { return ('~'); }
12 0     0 0   sub arity { return "nary"; }
13 0     0 0   sub priority { return 9; }
14             sub operate {
15 0     0 0   my ($self, $hash, $action, @ops) = @_;
16 0           $ops[$_] = '' for (grep { !defined $ops[$_] } 0..$#ops);
  0            
17            
18 0           return join("", grep { defined $_ } @ops);
  0            
19             }
20              
21             sub process {
22 0     0 0   my ($self, $hash, $action, $pipeline) = @_;
23 0           my @ops = @{$self->{operands}};
  0            
24 0           for my $idx (0..$#ops) {
25 0           eval {
26 0 0         $ops[$idx] = $ops[$idx]->$action($pipeline, $hash) unless $self->is_processed($ops[$idx]);
27 0 0         $self->{operands}->[$idx] = $ops[$idx] if $action eq "optimize";
28             };
29 0 0         if (my $exp = $@) {
30 0 0 0       $exp->initial_render(@ops[0..($idx-1)]) if $idx > 0 && defined $exp && blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control');
      0        
      0        
31 0           die $exp;
32             }
33             }
34            
35 0 0 0       if (int(grep { !$self->is_processed($_) } @ops) > 0 && $action eq "optimize") {
  0            
36 0           my $result;
37             my @new_ops;
38 0 0 0       for (map { blessed($_) && $_->isa('WWW::Shopify::Liquid::Operator::Concatenate') ? (@{$_->{operands}}) : ($_) } @ops) {
  0            
  0            
39 0 0         if ($self->is_processed($_)) {
40 0 0         $result .= $_ if defined $_;
41             } else {
42 0           push(@new_ops, $result);
43 0           $result = '';
44 0           push(@new_ops, $_);
45             }
46            
47             }
48 0 0         push(@new_ops, $result) if $result ne '';
49 0           $self->{operands} = \@new_ops;
50 0           return $self;
51             }
52            
53 0           return $self->operate($hash, $action, @ops);
54             }
55              
56             sub optimize {
57 0     0 0   my ($self, $optimizer, $hash) = @_;
58 0           my $result = $self->SUPER::optimize($optimizer, $hash);
59 0 0 0       $self->{operands} = [grep { !$self->is_processed($_) || (defined $_ && $_ ne '') } @{$self->{operands}}];
  0            
  0            
60 0 0         return $self->{operands}->[0] if int(@{$self->{operands}}) == 1;
  0            
61 0           return $result;
62             }
63              
64             sub new {
65 0     0 0   my $package = shift;
66 0           my $line = shift;
67 0           my $core = shift;
68 0           my $self = bless { line => $line, core => $core, operands => undef }, $package;
69 0 0         $self->{operands} = [@_] if int(@_) >= 1;
70 0           return $self;
71             }
72              
73             1;