| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package WWW::Shopify::Liquid::Tag::Capture; | ||
| 6 | use base 'WWW::Shopify::Liquid::Tag::Enclosing'; | ||
| 7 | sub min_arguments { return 1; } | ||
| 8 | sub max_arguments { return 1; } | ||
| 9 | use Scalar::Util qw(blessed looks_like_number); | ||
| 10 | sub process { | ||
| 11 | my ($self, $hash, $action, $pipeline) = @_; | ||
| 12 | my @vars = map { $self->is_processed($_) ? $_ : $_->$action($pipeline, $hash) } @{$self->{arguments}->[0]->{core}}; | ||
| 13 | return $self if $action eq "optimize" && int(grep { !$self->is_processed($_) } @vars) > 0; | ||
| 14 | |||
| 15 | my $inner_hash = $hash; | ||
| 16 | for (0..$#vars-1) { | ||
| 17 | return $self if ref($inner_hash) && ref($inner_hash) eq "HASH" && !exists $inner_hash->{$vars[$_]} && $action eq 'optimize'; | ||
| 18 | if (looks_like_number($vars[$_]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") { | ||
| 19 | $inner_hash->[$vars[$_]] = {} if !defined $inner_hash->[$vars[$_]]; | ||
| 20 | $inner_hash = $inner_hash->[$vars[$_]]; | ||
| 21 | } else { | ||
| 22 | $inner_hash->{$vars[$_]} = {} if !exists $inner_hash->{$vars[$_]}; | ||
| 23 | $inner_hash = $inner_hash->{$vars[$_]}; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | my $result = $self->{contents}->$action($pipeline, $hash); | ||
| 28 | |||
| 29 | return $self unless $self->is_processed($result); | ||
| 30 | |||
| 31 | # For now, only do renders. | ||
| 32 | if ($action eq "optimize") { | ||
| 33 | $pipeline->flag_conditional_uncertainty(\@vars); | ||
| 34 | $inner_hash->{$vars[-1]} = $result; | ||
| 35 | return undef if $pipeline->remove_assignment && !defined $pipeline->conditional_state; | ||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | if (looks_like_number($vars[-1]) && ref($inner_hash) && ref($inner_hash) eq "ARRAY") { | ||
| 39 | $inner_hash->[$vars[-1]] = $result; | ||
| 40 | } else { | ||
| 41 | $inner_hash->{$vars[-1]} = $result; | ||
| 42 | } | ||
| 43 | return ''; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub verify { | ||
| 47 | my ($self) = @_; | ||
| 48 | die new WWW::Shopify::Liquid::Exception::Parser::Arguments($self, "Requires a variable to be the capture target.") unless | ||
| 49 | $self->{arguments}->[0]->isa('WWW::Shopify::Liquid::Token::Variable'); | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | 1; |