File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/Case.pm
Criterion Covered Total %
statement 35 49 71.4
branch 10 26 38.4
condition 6 12 50.0
subroutine 9 12 75.0
pod 0 8 0.0
total 60 107 56.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 37     37   15467 use strict;
  37         109  
  37         1112  
3 37     37   241 use warnings;
  37         91  
  37         1432  
4              
5             package WWW::Shopify::Liquid::Tag::Case;
6 37     37   327 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
  37         94  
  37         8412  
7 0     0 0 0 sub min_arguments { return 1; }
8 0     0 0 0 sub max_arguments { return 1; }
9             sub new {
10 6     6 0 19 my $package = shift;
11 6         40 my $self = bless {
12             line => shift,
13             core => shift,
14             arguments => shift,
15             paths => undef,
16             else => undef
17             }, $package;
18 6         18 $self->interpret_inner_tokens(@{$_[0]});
  6         38  
19 6         18 return $self;
20             }
21 91     91 0 412 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 23     23 0 122 sub inner_ignore_whitespace { return 1; }
24 37     37   306 use List::Util qw(first);
  37         104  
  37         22192  
25             sub interpret_inner_tokens {
26 6     6 0 24 my ($self, @tokens) = @_;
27             # Comes in [], [tag, other_path], [tag, other_path], ...
28 6         20 my $token = shift(@tokens);
29 6 50       28 die new WWW::Shopify::Liquid::Exception::Parser($self, "Case statements must start with a when statement.") if int(@$token) > 0;
30 6         24 for (0..$#tokens) {
31 17         37 $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 17 50 33     46 ref($token->[0]->{arguments}->[0]) ne "WWW::Shopify::Liquid::Token::Number")
      66        
36             );
37 17 50 66     54 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 17 100       46 if ($token->[0]->tag eq "when") {
40 11         61 $self->{paths}->{$token->[0]->{arguments}->[0]->{core}} = $token->[1];
41             }
42             else {
43 6         25 $self->{else} = $token->[1];
44             }
45             }
46             }
47              
48             sub render {
49 12     12 0 40 my ($self, $renderer, $hash) = @_;
50 12         35 my $arguments = $self->{arguments}->[0];
51 12 50       47 $arguments = $arguments->render($renderer, $hash) if !$self->is_processed($arguments);
52 12 100       61 my $path = $self->{paths}->{$arguments} ? $self->{paths}->{$arguments} : $self->{else};
53 12 50 33     59 $path = $path->render($renderer, $hash) if $path && !$self->is_processed($path);
54 12 50       66 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;