File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/If.pm
Criterion Covered Total %
statement 56 58 96.5
branch 26 32 81.2
condition 13 18 72.2
subroutine 11 13 84.6
pod 0 9 0.0
total 106 130 81.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 37     37   296 use strict;
  37         172  
  37         1086  
3 37     37   221 use warnings;
  37         101  
  37         1387  
4              
5             package WWW::Shopify::Liquid::Tag::If;
6 37     37   229 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
  37         104  
  37         3834  
7 37     37   261 use Scalar::Util qw(blessed);
  37         88  
  37         26479  
8              
9 0     0 0 0 sub min_arguments { return 1; }
10 0     0 0 0 sub max_arguments { return 1; }
11              
12             sub new {
13 190     190 0 493 my $package = shift;
14 190         1148 my $self = bless {
15             line => shift,
16             core => shift,
17             arguments => shift,
18             true_path => undef,
19             false_path => undef
20             }, $package;
21 190         477 $self->interpret_inner_tokens(@{$_[0]});
  190         964  
22 190         584 return $self;
23             }
24 353     353 0 1269 sub inner_tags { return qw(elsif else) }
25 7     7 0 33 sub subelements { qw(true_path false_path arguments); }
26              
27             sub interpret_inner_tokens {
28 190     190 0 629 my ($self, @tokens) = @_;
29             # Comes in [true_path], [tag, other_path], [tag, other_path], ...
30 190         434 my $token = shift(@tokens);
31 190 50       627 return undef unless $token;
32 190         634 $self->{true_path} = $token->[0];
33 190 100       858 if (int(@tokens) > 0) {
34 62 50 66     285 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;
35 62 100       244 if ($tokens[0]->[0]->tag eq "elsif") {
36 12         31 $token = shift(@tokens);
37 12         33 my $arguments = $token->[0]->{arguments};
38 12         65 $self->{false_path} = WWW::Shopify::Liquid::Tag::If->new($token->[0]->{line}, "if", $arguments, [[$token->[1]], @tokens]);
39             }
40             else {
41 50         173 $self->{false_path} = $tokens[0]->[1];
42             }
43             }
44             }
45              
46             sub render {
47 412     412 0 1961 my ($self, $renderer, $hash) = @_;
48 412 50 33     1367 return $renderer->state->value($self) if $renderer->state && $renderer->state->value($self);
49 412         1768 my $arguments = $self->render_subelement($renderer, $hash, $self->{arguments});
50 412         904 my $result;
51 412         810 eval {
52 412 100 100     1254 my $path = $self->{((!$self->inversion && $arguments->[0]) || ($self->inversion && !$arguments->[0])) ? 'true_path' : 'false_path'};
53 412 100 100     1806 $result = $path && !$self->is_processed($path) ? $path->render($renderer, $hash) : $path;
54             };
55 412 100       1269 if (my $exp = $@) {
56 4 50 33     34 $exp->value($self->{arguments}, $arguments) if blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control::Pause');
57 4         19 die $exp;
58             }
59 408 100       4650 return defined $result ? $result : '';
60             }
61              
62 754     754 0 4073 sub inversion { return 0; }
63              
64             sub optimize {
65 43     43 0 148 my ($self, $optimizer, $hash) = @_;
66 43 50       242 $self->{arguments}->[0] = $self->{arguments}->[0]->optimize($optimizer, $hash) if !$self->is_processed($self->{arguments}->[0]);
67 43 100       235 if ($self->is_processed($self->{arguments}->[0])) {
68 24 100 100     93 my $path = $self->{((!$self->inversion && $self->{arguments}->[0]) || ($self->inversion && !$self->{arguments}->[0])) ? 'true_path' : 'false_path'};
69 24 100       94 return $self->is_processed($path) ? $path : $path->optimize($optimizer, $hash);
70             }
71 19 100       89 if (!$self->is_processed($self->{false_path})) {
72 1         7 $optimizer->push_conditional_state;
73 1         4 $self->{false_path} = $self->{false_path}->optimize($optimizer, $hash);
74 1         6 $optimizer->pop_conditional_state($hash);
75             }
76 19 50       77 if (!$self->is_processed($self->{true_path})) {
77 19         108 $optimizer->push_conditional_state;
78 19         103 $self->{true_path} = $self->{true_path}->optimize($optimizer, $hash);
79 19         109 $optimizer->pop_conditional_state($hash);
80             }
81 19         73 return $self;
82             }
83              
84             1;