File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/Assign.pm
Criterion Covered Total %
statement 15 58 25.8
branch 0 42 0.0
condition 0 39 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 20 152 13.1


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 30     30   15944 use strict;
  30         58  
  30         857  
3 30     30   130 use warnings;
  30         44  
  30         714  
4              
5 30     30   120 use WWW::Shopify::Liquid::Tag;
  30         57  
  30         1048  
6              
7             package WWW::Shopify::Liquid::Tag::Assign;
8 30     30   122 use base 'WWW::Shopify::Liquid::Tag::Free';
  30         43  
  30         2264  
9 30     30   155 use Scalar::Util qw(looks_like_number);
  30         44  
  30         20111  
10 0     0 0   sub min_arguments { return 1; }
11 0     0 0   sub max_arguments { return 1; }
12             sub verify {
13 0     0 0   my ($self) = @_;
14             die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires assignment operator to be the first thing in an assign tag.") unless
15 0 0         $self->{arguments}->[0]->isa('WWW::Shopify::Liquid::Operator::Assignment');
16             die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires variable for what you're assigning to.") unless
17 0 0         $self->{arguments}->[0]->{operands}->[0]->isa('WWW::Shopify::Liquid::Token::Variable');
18             }
19             sub process {
20 0     0 0   my ($self, $hash, $action, $pipeline) = @_;
21 0 0         my @vars = map { $self->is_processed($_) ? $_ : $_->$action($pipeline, $hash) } @{$self->{arguments}->[0]->{operands}->[0]->{core}};
  0            
  0            
22 0 0 0       return $self if $action eq "optimize" && int(grep { !$self->is_processed($_) } @vars) > 0;
  0            
23            
24            
25 0           my $inner_hash = $hash;
26 0           for (0..$#vars-1) {
27 0 0 0       return $self if ref($inner_hash) && ref($inner_hash) eq "HASH" && !exists $inner_hash->{$vars[$_]} && $action eq 'optimize';
      0        
      0        
28 0 0 0       if (looks_like_number($vars[$_]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") {
      0        
29 0 0         $inner_hash->[$vars[$_]] = {} if !defined $inner_hash->[$vars[$_]];
30 0           $inner_hash = $inner_hash->[$vars[$_]];
31             } else {
32 0 0         $inner_hash->{$vars[$_]} = {} if !exists $inner_hash->{$vars[$_]};
33 0           $inner_hash = $inner_hash->{$vars[$_]};
34             }
35             }
36            
37            
38 0           my $result = $self->{arguments}->[0]->{operands}->[1];
39 0 0         $result = $result->$action($pipeline, $hash) if !$self->is_processed($result);
40 0 0         return $self unless $self->is_processed($result);
41            
42             # For now, only do renders.
43 0 0         if ($action eq "optimize") {
44 0           $self->{arguments}->[0]->{operands}->[1] = $result;
45             # If we run across something that should be assigned, we must delete it in the hash to preserve uncertainty.
46             # OK, no. We still return ourselves, but we do a bit of a deeper analysis. If the assignment is out the in the open, we assign.
47             # If the assignment is contingent upon a conditional, (i.e. inside something that isn't fully resolved). Then we delete it.
48 0           $pipeline->flag_conditional_uncertainty(\@vars);
49 0           $inner_hash->{$vars[-1]} = $result;
50 0 0 0       return undef if $pipeline->remove_assignment && !defined $pipeline->conditional_state;;
51 0           return $self;
52             }
53            
54 0           my $assignment = $self->{arguments}->[0];
55 0 0 0       if (looks_like_number($vars[-1]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") {
      0        
56 0 0         if ($assignment->isa('WWW::Shopify::Liquid::Operator::PlusAssignment')) {
    0          
    0          
    0          
57 0   0       $inner_hash->[$vars[-1]] += ($result || 0);
58             } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MinusAssignment')) {
59 0   0       $inner_hash->[$vars[-1]] -= ($result || 0);
60             } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MultiplyAssignment')) {
61 0   0       $inner_hash->[$vars[-1]] *= ($result || 0);
62             } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::DivideAssignment')) {
63 0           $inner_hash->[$vars[-1]] /= $result;
64             } else {
65 0           $inner_hash->[$vars[-1]] = $result;
66             }
67             } else {
68 0 0         if ($assignment->isa('WWW::Shopify::Liquid::Operator::PlusAssignment')) {
    0          
    0          
    0          
69 0   0       $inner_hash->{$vars[-1]} += ($result || 0);
70             } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MinusAssignment')) {
71 0   0       $inner_hash->{$vars[-1]} -= ($result || 0);
72             } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::MultiplyAssignment')) {
73 0   0       $inner_hash->{$vars[-1]} *= ($result || 0);
74             } elsif ($assignment->isa('WWW::Shopify::Liquid::Operator::DivideAssignment')) {
75 0           $inner_hash->{$vars[-1]} /= $result;
76             } else {
77 0           $inner_hash->{$vars[-1]} = $result;
78             }
79             }
80 0           return '';
81             }
82              
83              
84              
85             1;