File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/Capture.pm
Criterion Covered Total %
statement 37 41 90.2
branch 13 22 59.0
condition 13 27 48.1
subroutine 6 8 75.0
pod 0 4 0.0
total 69 102 67.6


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