File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/If.pm
Criterion Covered Total %
statement 12 55 21.8
branch 0 28 0.0
condition 0 12 0.0
subroutine 4 13 30.7
pod 0 9 0.0
total 16 117 13.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 30     30   182 use strict;
  30         49  
  30         897  
3 30     30   122 use warnings;
  30         45  
  30         1141  
4              
5             package WWW::Shopify::Liquid::Tag::If;
6 30     30   126 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
  30         44  
  30         2383  
7 30     30   158 use List::Util qw(first);
  30         53  
  30         20113  
8              
9 0     0 0   sub min_arguments { return 1; }
10 0     0 0   sub max_arguments { return 1; }
11              
12             sub new {
13 0     0 0   my $package = shift;
14 0           my $self = bless {
15             line => shift,
16             core => shift,
17             arguments => shift,
18             true_path => undef,
19             false_path => undef
20             }, $package;
21 0           $self->interpret_inner_tokens(@{$_[0]});
  0            
22 0           return $self;
23             }
24 0     0 0   sub inner_tags { return qw(elsif else) }
25             sub interpret_inner_tokens {
26 0     0 0   my ($self, @tokens) = @_;
27             # Comes in [true_path], [tag, other_path], [tag, other_path], ...
28 0           my $token = shift(@tokens);
29 0 0         return undef unless $token;
30 0           $self->{true_path} = $token->[0];
31 0 0         if (int(@tokens) > 0) {
32 0 0 0       die new WWW::Shopify::Liquid::Exception::Parser($self, "else cannot be anywhere, except the end tag of an if statement.") if $tokens[0]->[0]->tag eq "else" && int(@tokens) > 1;
33 0 0         if ($tokens[0]->[0]->tag eq "elsif") {
34 0           $token = shift(@tokens);
35 0           my $arguments = $token->[0]->{arguments};
36 0           $self->{false_path} = WWW::Shopify::Liquid::Tag::If->new($token->[0]->{line}, "if", $arguments, [[$token->[1]], @tokens]);
37             }
38             else {
39 0           $self->{false_path} = $tokens[0]->[1];
40             }
41             }
42             }
43              
44 0     0 0   sub tokens { return ($_[0], map { $_->tokens } grep { defined $_ } ($_[0]->{true_path}, $_[0]->{false_path}, @{$_[0]->{arguments}})) }
  0            
  0            
  0            
45              
46             sub render {
47 0     0 0   my ($self, $renderer, $hash) = @_;
48 0 0         my $arguments = $self->is_processed($self->{arguments}->[0]) ? $self->{arguments}->[0] : $self->{arguments}->[0]->render($renderer, $hash);
49 0 0 0       my $path = $self->{((!$self->inversion && $arguments) || ($self->inversion && !$arguments)) ? 'true_path' : 'false_path'};
50 0 0 0       $path = $path->render($renderer, $hash) if $path && !$self->is_processed($path);
51 0 0         return defined $path ? $path : '';
52             }
53              
54 0     0 0   sub inversion { return 0; }
55              
56             sub optimize {
57 0     0 0   my ($self, $optimizer, $hash) = @_;
58 0 0         $self->{arguments}->[0] = $self->{arguments}->[0]->optimize($optimizer, $hash) if !$self->is_processed($self->{arguments}->[0]);
59 0 0         if ($self->is_processed($self->{arguments}->[0])) {
60 0 0 0       my $path = $self->{((!$self->inversion && $self->{arguments}->[0]) || ($self->inversion && !$self->{arguments}->[0])) ? 'true_path' : 'false_path'};
61 0 0         return $self->is_processed($path) ? $path : $path->optimize($optimizer, $hash);
62             }
63 0 0         if (!$self->is_processed($self->{false_path})) {
64 0           $optimizer->push_conditional_state;
65 0           $self->{false_path} = $self->{false_path}->optimize($optimizer, $hash);
66 0           $optimizer->pop_conditional_state($hash);
67             }
68 0 0         if (!$self->is_processed($self->{true_path})) {
69 0           $optimizer->push_conditional_state;
70 0           $self->{true_path} = $self->{true_path}->optimize($optimizer, $hash);
71 0           $optimizer->pop_conditional_state($hash);
72             }
73 0           return $self;
74             }
75              
76             1;