File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/Case.pm
Criterion Covered Total %
statement 12 49 24.4
branch 0 26 0.0
condition 0 12 0.0
subroutine 4 12 33.3
pod 0 8 0.0
total 16 107 14.9


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 30     30   17756 use strict;
  30         51  
  30         985  
3 30     30   135 use warnings;
  30         50  
  30         1258  
4              
5             package WWW::Shopify::Liquid::Tag::Case;
6 30     30   151 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
  30         55  
  30         6114  
7 0     0 0   sub min_arguments { return 1; }
8 0     0 0   sub max_arguments { return 1; }
9             sub new {
10 0     0 0   my $package = shift;
11 0           my $self = bless {
12             line => shift,
13             core => shift,
14             arguments => shift,
15             paths => undef,
16             else => undef
17             }, $package;
18 0           $self->interpret_inner_tokens(@{$_[0]});
  0            
19 0           return $self;
20             }
21 0     0 0   sub inner_tags { return qw(when else); }
22             # Used to tell the parser to discard all whitespace tags whilst at the inner tag level.
23 0     0 0   sub inner_ignore_whitespace { return 1; }
24 30     30   163 use List::Util qw(first);
  30         59  
  30         16630  
25             sub interpret_inner_tokens {
26 0     0 0   my ($self, @tokens) = @_;
27             # Comes in [], [tag, other_path], [tag, other_path], ...
28 0           my $token = shift(@tokens);
29 0 0         die new WWW::Shopify::Liquid::Exception::Parser($self, "Case statements must start with a when statement.") if int(@$token) > 0;
30 0           for (0..$#tokens) {
31 0           $token = $tokens[$_];
32             die new WWW::Shopify::Liquid::Exception::Parser($self, "Requires a constant when using a when statement.") if $token->[0]->tag eq "when" &&
33             (!$token->[0]->{arguments}->[0] || (
34             ref($token->[0]->{arguments}->[0]) ne "WWW::Shopify::Liquid::Token::String" &&
35 0 0 0       ref($token->[0]->{arguments}->[0]) ne "WWW::Shopify::Liquid::Token::Number")
      0        
36             );
37 0 0 0       die new WWW::Shopify::Liquid::Exception::Parser($self, "Else statements can only be used in the last block of a case statement.") if
38             $token->[0]->tag eq "else" && $_ < $#tokens;
39 0 0         if ($token->[0]->tag eq "when") {
40 0           $self->{paths}->{$token->[0]->{arguments}->[0]->{core}} = $token->[1];
41             }
42             else {
43 0           $self->{else} = $token->[1];
44             }
45             }
46             }
47              
48             sub render {
49 0     0 0   my ($self, $renderer, $hash) = @_;
50 0           my $arguments = $self->{arguments}->[0];
51 0 0         $arguments = $arguments->render($renderer, $hash) if !$self->is_processed($arguments);
52 0 0         my $path = $self->{paths}->{$arguments} ? $self->{paths}->{$arguments} : $self->{else};
53 0 0 0       $path = $path->render($renderer, $hash) if $path && !$self->is_processed($path);
54 0 0         return defined $path ? $path : '';
55             }
56              
57             sub optimize {
58 0     0 0   my ($self, $optimizer, $hash) = @_;
59 0 0         $self->{arguments}->[0] = $self->{arguments}->[0]->optimize($optimizer, $hash) if !$self->is_processed($self->{arguments}->[0]);
60 0           my $key = $self->{arguments}->[0];
61 0 0         if ($self->is_processed($key)) {
62 0 0         my $path = exists $self->{paths}->{$key} ? $self->{paths}->{$key} : $self->{else};
63 0 0         return $self->is_processed($path) ? $path : $path->optimize($optimizer, $hash);
64             }
65             else {
66 0 0         $self->{else} = $self->{else}->optimize($optimizer, $hash) if !$self->is_processed($self->{else});
67 0           for (grep { $self->is_processed($self->{paths}->{$_}) } keys(%{$self->{paths}})) {
  0            
  0            
68 0           $self->{paths}->{$_} = $self->{paths}->{$_}->optimize($optimizer, $hash);
69             }
70             }
71 0           return $self;
72             }
73              
74             1;