File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/Case.pm
Criterion Covered Total %
statement 29 49 59.1
branch 5 26 19.2
condition 5 12 41.6
subroutine 8 12 66.6
pod 0 8 0.0
total 47 107 43.9


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 21     21   10509 use strict;
  21         41  
  21         924  
3 21     21   107 use warnings;
  21         35  
  21         724  
4              
5             package WWW::Shopify::Liquid::Tag::Case;
6 21     21   99 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
  21         34  
  21         3850  
7 0     0 0 0 sub min_arguments { return 1; }
8 0     0 0 0 sub max_arguments { return 1; }
9             sub new {
10 3     3 0 10 my $package = shift;
11 3         41 my $self = bless {
12             line => shift,
13             core => shift,
14             arguments => shift,
15             paths => undef,
16             else => undef
17             }, $package;
18 3         7 $self->interpret_inner_tokens(@{$_[0]});
  3         20  
19 3         12 return $self;
20             }
21 26     26 0 97 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 11     11 0 77 sub inner_ignore_whitespace { return 1; }
24 21     21   128 use List::Util qw(first);
  21         114  
  21         10772  
25             sub interpret_inner_tokens {
26 3     3 0 11 my ($self, @tokens) = @_;
27             # Comes in [], [tag, other_path], [tag, other_path], ...
28 3         8 my $token = shift(@tokens);
29 3 50       16 die new WWW::Shopify::Liquid::Exception::Parser($self, "Case statements must start with a when statement.") if int(@$token) > 0;
30 3         14 for (0..$#tokens) {
31 8         14 $token = $tokens[$_];
32 8 50 33     35 die new WWW::Shopify::Liquid::Exception::Parser($self, "Requires a constant when using a when statement.") if $token->[0]->tag eq "when" &&
      66        
33             (!$token->[0]->{arguments}->[0] || (
34             ref($token->[0]->{arguments}->[0]) ne "WWW::Shopify::Liquid::Token::String" &&
35             ref($token->[0]->{arguments}->[0]) ne "WWW::Shopify::Liquid::Token::Number")
36             );
37 8 50 66     118 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 8 100       23 if ($token->[0]->tag eq "when") {
40 5         45 $self->{paths}->{$token->[0]->{arguments}->[0]->{core}} = $token->[1];
41             }
42             else {
43 3         49 $self->{else} = $token->[1];
44             }
45             }
46             }
47              
48             sub render {
49 0     0 0   my ($self, $hash) = @_;
50 0           my $arguments = $self->{arguments}->[0];
51 0 0         $arguments = $arguments->render($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($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, $hash) = @_;
59 0 0         $self->{arguments}->[0] = $self->{arguments}->[0]->optimize($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($hash);
64             }
65             else {
66 0 0         $self->{else} = $self->{else}->optimize($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($hash);
69             }
70             }
71 0           return $self;
72             }
73              
74             1;