blib/lib/WWW/Shopify/Liquid/Tag/Case.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 110 CC 14 SCAR 31.3
CC
14
Cov
0%
optimize
42.0
interpret_inner_tokens
30.0
render
30.0
CRAP 22.8
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4  
5 package WWW::Shopify::Liquid::Tag::Case;
6 use base 'WWW::Shopify::Liquid::Tag::Enclosing';
7 sub min_arguments { return 1; }
8 sub max_arguments { return 1; }
9 sub new {
10 my $package = shift;
11 my $self = bless {
12 line => shift,
13 core => shift,
14 arguments => shift,
15 paths => undef,
16 else => undef
17 }, $package;
18 $self->interpret_inner_tokens(@{$_[0]});
19 return $self;
20 }
21 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 sub inner_ignore_whitespace { return 1; }
24 use List::Util qw(first);
25 sub interpret_inner_tokens {
26 my ($self, @tokens) = @_;
27 # Comes in [], [tag, other_path], [tag, other_path], ...
28 my $token = shift(@tokens);
29 die new WWW::Shopify::Liquid::Exception::Parser($self, "Case statements must start with a when statement.") if int(@$token) > 0;
30 for (0..$#tokens) {
31 $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 ref($token->[0]->{arguments}->[0]) ne "WWW::Shopify::Liquid::Token::Number")
36 );
37 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 if ($token->[0]->tag eq "when") {
40 $self->{paths}->{$token->[0]->{arguments}->[0]->{core}} = $token->[1];
41 }
42 else {
43 $self->{else} = $token->[1];
44 }
45 }
46 }
47  
48 sub render {
49 my ($self, $renderer, $hash) = @_;
50 my $arguments = $self->{arguments}->[0];
51 $arguments = $arguments->render($renderer, $hash) if !$self->is_processed($arguments);
52 my $path = $self->{paths}->{$arguments} ? $self->{paths}->{$arguments} : $self->{else};
53 $path = $path->render($renderer, $hash) if $path && !$self->is_processed($path);
54 return defined $path ? $path : '';
55 }
56  
57 sub optimize {
58 my ($self, $optimizer, $hash) = @_;
59 $self->{arguments}->[0] = $self->{arguments}->[0]->optimize($optimizer, $hash) if !$self->is_processed($self->{arguments}->[0]);
60 my $key = $self->{arguments}->[0];
61 if ($self->is_processed($key)) {
62 my $path = exists $self->{paths}->{$key} ? $self->{paths}->{$key} : $self->{else};
63 return $self->is_processed($path) ? $path : $path->optimize($optimizer, $hash);
64 }
65 else {
66 $self->{else} = $self->{else}->optimize($optimizer, $hash) if !$self->is_processed($self->{else});
67 for (grep { $self->is_processed($self->{paths}->{$_}) } keys(%{$self->{paths}})) {
68 $self->{paths}->{$_} = $self->{paths}->{$_}->optimize($optimizer, $hash);
69 }
70 }
71 return $self;
72 }
73  
74 1;