File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/For.pm
Criterion Covered Total %
statement 35 44 79.5
branch 12 26 46.1
condition 6 21 28.5
subroutine 5 7 71.4
pod 0 4 0.0
total 58 102 56.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 21     21   10288 use strict;
  21         42  
  21         718  
3 21     21   104 use warnings;
  21         33  
  21         688  
4              
5             package WWW::Shopify::Liquid::Tag::For;
6 21     21   100 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
  21         35  
  21         11061  
7              
8 0     0 0 0 sub min_arguments { return 1; }
9 0     0 0 0 sub max_arguments { return 1; }
10              
11             sub verify {
12 24     24 0 43 my ($self) = @_;
13 24 100       229 die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires in operator to be part of loop.") unless
14             $self->{arguments}->[0]->isa('WWW::Shopify::Liquid::Operator::In');
15 23         327 die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires the opening variable of a loop to be a simple variable.") unless
16             $self->{arguments}->[0]->{operands}->[0] && $self->{arguments}->[0]->{operands}->[0]->isa('WWW::Shopify::Liquid::Token::Variable') &&
17 23 50 33     253 int(@{$self->{arguments}->[0]->{operands}->[0]->{core}}) == 1 && $self->{arguments}->[0]->{operands}->[0]->{core}->[0]->isa('WWW::Shopify::Liquid::Token::String');
      33        
      33        
18             }
19              
20             # Should eventually support loop unrolling.
21             sub process {
22 2     2 0 5 my ($self, $hash, $action) = @_;
23 2         4 my ($op1, $op2) = @{$self->{arguments}->[0]->{operands}};
  2         8  
24 2         9 my $var = $op1->{core}->[0]->{core};
25 2 50       11 $op2 = $op2->$action($hash) if !$self->is_processed($op2);
26 2 50 33     9 $self->{arguments}->[0]->{operands}->[1] = $op2 if $self->is_processed($op2) && $action eq 'optimize';
27 2 50 33     8 return $self if (!$self->is_processed($op2) && $action eq "optimize");
28 2 50 33     7 return '' if (!$self->is_processed($op2) && $action eq "render");
29 2 50       8 die new WWW::Shopify::Liquid::Exception::Renderer::Arguments($self, "Requires an array in for loop.") unless ref($op2) eq "ARRAY";
30 2         7 my @array = @$op2;
31 2         3 my @texts = ();
32 2         5 my ($all_processed, $content) = (1, undef);
33            
34             # Since we're looping, we can't optimize the loop stuff, unless we unroll. Since we're not unrolling yet, only throw in the loop stuff during rendering.
35 2         5 for (0..$#array) {
36 12         13 my ($backup, $existed);
37 12 50       17 if ($action eq "render") {
38 12         20 $hash->{$var} = $array[$_];
39 12         69 $hash->{forloop} = {
40             index => ($_+1), index0 => $_, first => $_ == 0, last => $_ == $#array,
41             length => int(@array), rindex0 => (($#array - $_) + 1), rindex => (($#array - $_)),
42             };
43             }
44             else {
45 0         0 $existed = exists $hash->{$var};
46 0 0       0 if ($existed) {
47 0         0 $backup = $hash->{$var};
48 0         0 $hash->{$var} = undef;
49             }
50 0         0 $hash->{forloop} = undef;
51             }
52 12         73 $content = $self->{contents}->$action($hash);
53 12 50       22 if ($action ne "render") {
54 0 0 0     0 $hash->{$var} = $backup if !defined $hash->{$var} && $existed;
55             }
56 12 50       24 $all_processed = 0 if !$self->is_processed($content);
57 12         24 push(@texts, $content);
58             }
59 2 50       13 return join('', @texts) if $all_processed;
60 0           return $self;
61            
62             }
63              
64              
65              
66             1;