File Coverage

blib/lib/WWW/Shopify/Liquid/Tag/CreateFilter.pm
Criterion Covered Total %
statement 45 52 86.5
branch 8 16 50.0
condition 3 8 37.5
subroutine 11 16 68.7
pod 0 4 0.0
total 67 96 69.7


line stmt bran cond sub pod time code
1             # Creates a tag to be used in the system, much like a function call. Should probably disallow recursion in order to preserve the non-looping nature of liquid.
2              
3 37     37   14417 use strict;
  37         109  
  37         1090  
4 37     37   225 use warnings;
  37         101  
  37         1640  
5              
6              
7              
8             package WWW::Shopify::Liquid::Filter::CustomFilter;
9 37     37   7096 use parent 'WWW::Shopify::Liquid::Filter';
  37         7380  
  37         250  
10              
11             sub new {
12 2     2   6 my ($package, $parser, $line, $core, $operand, @arguments) = @_;
13 2 100       6 if (!ref($package)) {
14 1         4 my $self = bless {
15             name => $core,
16             contents => $arguments[0]
17             }, $package;
18             die new WWW::Shopify::Liquid::Exception("Cannot register custom filter " . $core . ", would conflict with buildin filter.") if
19 1 50 33     4 $parser->filters->{$core} && !$parser->filters->{$core}->isa('WWW::Shopify::Liquid::Filter::CustomFilter');
20 1 50 33     9 die new WWW::Shopify::Liquid::Exception("Recursion is disallowed in liquid.") unless int(grep { $_->isa('WWW::Shopify::Liquid::Filter::CustomFilter') && $_->name && $core eq $_->name } $arguments[0]->tokens) == 0;
  2 50       13  
21 1         5 $parser->custom_filters->{$self->name} = $self;
22 1 50       4 push(@{$parser->{transient_elements}}, $self) if $parser->transient_custom_operations;
  1         2  
23 1         6 return $self;
24             } else {
25 1         7 my $self = ref(shift)->SUPER::new(@_);
26 1         4 $self->{contents} = $parser->custom_filters->{$self->{core}}->{contents};
27 1         3 return $self;
28             }
29             }
30              
31 0     0   0 sub abstract { return !ref($_[0]); }
32 0     0   0 sub tags { return $_[0]->{name}; }
33 4     4   17 sub name { return $_[0]->{name}; }
34 1   50 1   10 sub min_arguments { return $_[0]->{min_arguments} || 0; }
35 1     1   5 sub max_arguments { return $_[0]->{max_arguments}; }
36              
37             # No optimization permitted for now.
38             sub optimize {
39 0     0   0 return $_[0];
40             }
41              
42             sub render {
43 1     1   4 my ($self, $renderer, $hash) = @_;
44 1 50       5 my $operand = !$self->is_processed($self->{operand}) ? $self->{operand}->render($renderer, $hash) : $self->{operand};
45 1 0       2 my @arguments = map { !$self->is_processed($_) ? $_->render($renderer, $hash) : $_ } @{$self->{arguments}};
  0         0  
  1         3  
46 1         3 my $contents = $self->{contents};
47 1         3 $hash->{arguments} = \@arguments;
48 1         2 $hash->{operand} = $operand;
49 1         5 $contents->render($renderer, $hash);
50 1 50       3 if ($renderer->{return_value}) {
51 1         5 my $value = delete $renderer->{return_value};
52 1         5 return $value;
53             }
54 0         0 return undef;
55             }
56              
57             package WWW::Shopify::Liquid::Tag::CreateFilter;
58 37     37   19737 use parent 'WWW::Shopify::Liquid::Tag::Enclosing';
  37         105  
  37         214  
59              
60 0     0 0 0 sub min_arguments { 2 }
61 0     0 0 0 sub max_arguments { 4 }
62              
63             sub new {
64 1     1 0 3 my ($package, $line, $tag, $arguments, $contents, $parser) = @_;
65 1         6 my $self = $package->SUPER::new($line, $tag, $arguments, $contents, $parser);
66 1         3 $parser->parent->register_filter(WWW::Shopify::Liquid::Filter::CustomFilter->new($parser, [], $arguments->[0]->{core}, $arguments, @{$contents->[0]}));
  1         15  
67 1         3 return $self;
68             }
69              
70 1     1 0 4 sub render { '' }
71              
72             1;