Shopify/Model/SmartCollection/Rule.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 164 CC 20 SCAR 57.4
CC
20
Cov
0%
product_matches
420.0
fields
2.0
BEGIN
2.0
CRAP 311.6
1 #!/usr/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 package WWW::Shopify::Model::SmartCollection::Rule;
7 use parent 'WWW::Shopify::Model::NestedItem';
8  
9 my $fields; sub fields { return $fields; }
10 BEGIN { $fields = {
11 "column" => new WWW::Shopify::Field::String::Enum(["title", "type", "vendor", "tag", "variant_weight", "variant_title", "variant_compare_at_price", "variant_inventory"]),
12 "relation" => new WWW::Shopify::Field::String::Enum(["equals", "greater_than", "less_than", "starts_with", "ends_with", "contains"]),
13 "condition" => new WWW::Shopify::Field::String(),
14 }; }
15  
16 sub identifier { return ("column", "relation", "condition"); }
17  
18 use Scalar::Util qw(blessed);
19 sub product_matches {
20 my ($self, $product) = @_;
21 die new WWW::Shopify::Exception("Requires a WWW::Shopify::Model::Product.") unless blessed($product) && $product->isa('WWW::Shopify::Model::Product');
22
23 my $field = $self->column;
24 my $operand = $self->condition;
25 my $operator = $self->relation;
26 $field = "tags" if $field eq "tag";
27 $field = "variant_inventory_quantity" if $field eq "variant_inventory";
28 $field = "product_type" if $field eq "type";
29
30 my %operators = (
31 "equals" => sub {
32 my ($field, $value, $operand, $item) = @_;
33 $value = '' unless defined $value;
34 if ($field eq "tags") {
35 $operand = quotemeta($operand);
36 return $value =~ m/(^|, )$operand($|,)/i;
37 }
38 return $value eq $operand && $item->inventory_management && $item->inventory_management eq "shopify" if $field eq "inventory_quantity";
39 return $value eq $operand;
40 },
41 "not_equals" => sub {
42 my ($field, $value, $operand, $item) = @_;
43 $value = '' unless defined $value;
44 if ($field eq "tags") {
45 $operand = quotemeta($operand);
46 return $value !~ m/(^|,)$operand($|,)/i;
47 }
48 return $value ne $operand && $item->inventory_management && $item->inventory_management eq "shopify" if $field eq "inventory_quantity";
49 return $value ne $operand;
50 },
51 "less_than" => sub {
52 my ($field, $value, $operand, $item) = @_;
53 $value = 0 unless defined $value;
54 return $value < $operand && $item->inventory_management && $item->inventory_management eq "shopify" if $field eq "inventory_quantity";
55 return $value < $operand;
56 },
57 "greater_than" => sub {
58 my ($field, $value, $operand, $item) = @_;
59 $value = 0 unless defined $value;
60 return $value > $operand && $item->inventory_management && $item->inventory_management eq "shopify" if $field eq "inventory_quantity";
61 return $value > $operand;
62 },
63 "starts_with" => sub { my ($field, $value, $operand) = @_; $value = 0 unless defined $value; return index(lc($value), lc($operand)) == 0; },
64 "ends_with" => sub { my ($field, $value, $operand) = @_; $value = 0 unless defined $value; return index(lc($value), lc($operand)) == length($value) - length($operand); },
65 "contains" => sub { my ($field, $value, $operand) = @_; $value = 0 unless defined $value; return index(lc($value), lc($operand)) != -1; },
66 "not_contains" => sub { my ($field, $value, $operand) = @_; $value = 0 unless defined $value; return index(lc($value), lc($operand)) == -1; }
67 );
68 if ($field =~ m/^variant_(.*)/) {
69 $field = $1;
70 return int(grep { $operators{$operator}->($field, $_->$field, $operand, $_) } $product->variants) > 0;
71 }
72 return $operators{$operator}->($field, $product->$field, $operand, $product);
73 }
74  
75 eval(__PACKAGE__->generate_accessors); die $@ if $@;
76  
77 1;