« blib/lib/WWW/Shopify/Liquid/Tag/Decrement.pm
↑ Summary
blib/lib/WWW/Shopify/Liquid/Precompiler.pm »
| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package WWW::Shopify::Liquid::Tag::Increment; | ||
| 6 | use base 'WWW::Shopify::Liquid::Tag::Free'; | ||
| 7 | sub max_arguments { return 1; } | ||
| 8 | sub min_arguments { return 1; } | ||
| 9 | sub verify { | ||
| 10 | my ($self) = @_; | ||
| 11 | die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires variable to increment.") unless | ||
| 12 | $self->{arguments}->[0]->isa('WWW::Shopify::Liquid::Token::Variable'); | ||
| 13 | } | ||
| 14 | |||
| 15 | use Scalar::Util qw(looks_like_number); | ||
| 16 | sub process { | ||
| 17 | my ($self, $hash, $action, $pipeline) = @_; | ||
| 18 | my @vars = map { $self->is_processed($_) ? $_ : $_->$action($pipeline, $hash) } @{$self->{arguments}->[0]->{core}}; | ||
| 19 | |||
| 20 | my $inner_hash = $hash; | ||
| 21 | for (0..$#vars-1) { | ||
| 22 | return $self if ref($inner_hash) && ref($inner_hash) eq "HASH" && !exists $inner_hash->{$vars[$_]} && $action eq 'optimize'; | ||
| 23 | if (looks_like_number($vars[$_]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") { | ||
| 24 | $inner_hash->[$vars[$_]] = {} if !defined $inner_hash->[$vars[$_]]; | ||
| 25 | $inner_hash = $inner_hash->[$vars[$_]]; | ||
| 26 | } else { | ||
| 27 | $inner_hash->{$vars[$_]} = {} if !exists $inner_hash->{$vars[$_]}; | ||
| 28 | $inner_hash = $inner_hash->{$vars[$_]}; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | if (exists $inner_hash->{$vars[-1]} && looks_like_number($inner_hash->{$vars[-1]})) { | ||
| 32 | $inner_hash->{$vars[-1]}++; | ||
| 33 | } | ||
| 34 | return ''; | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | 1; |