blib/lib/WWW/Shopify/Liquid/Analyzer.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 126 CC 6 SCAR 15.5
CC
6
Cov
0%
expand_entity
12.0
new
6.0
new
6.0
CRAP 4.7
1 #!/usr/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 # Analyzes liquid in a non-trivial manner to perform useful calculations.
7 # Generaly for dependncy analysis.
8 package WWW::Shopify::Liquid::Analyzer::Entity;
9 sub new {
10 my ($package, $hash) = @_;
11 $hash = {} unless $hash;
12 return bless {
13 id => undef,
14 # Things we include.
15 dependencies => [],
16 # Things that include us.
17 references => [],
18 # Flattened lists of dependencies and references.
19 full_dependencies => [],
20 full_references => [],
21 file => undef,
22 ast => undef,
23 %$hash
24 }, $package;
25 }
26 sub ast { return $_[0]->{ast}; }
27 sub id { return $_[0]->{id}; }
28  
29 package WWW::Shopify::Liquid::Analyzer;
30 sub new {
31 my ($package) = shift;
32 my $self = bless {
33 @_
34 }, $package;
35 $self->{liquid} = WWW::Shopify::Liquid->new unless $self->liquid;
36 return $self;
37 }
38 sub liquid { return $_[0]->{liquid}; }
39  
40 sub retrieve_includes_ast {
41 my ($self, $ast) = @_;
42 return grep { defined $_ } map { $_->include_literal } grep { defined $_ && $_->isa('WWW::Shopify::Liquid::Tag::Include') } $ast->tokens;
43 }
44  
45 sub expand_entity {
46 my ($self, $entity, $type, $used_list) = @_;
47 $used_list = { $entity->id => 1 } unless $used_list;
48 return map { $used_list->{$_->id} ? () : ($_, $self->expand_entity($_, $type, $used_list)) } @{$entity->{$type}};
49 }
50  
51 use List::MoreUtils qw(uniq);
52 sub populate_dependencies {
53 my ($self, @entities) = @_;
54 my %entity_list = map { $_->id => $_ } @entities;
55
56 # Go through, and flatten out those lists.
57 for my $entity (grep { $_->ast } @entities) {
58 my @included_literals = $self->retrieve_includes_ast($entity->ast);
59 $entity_list{$_} = WWW::Shopify::Liquid::Analyzer::Entity->new({ id => $_ }) for (grep { !$entity_list{$_} } @included_literals);
60 my @included_entities = map { $entity_list{$_} } @included_literals;
61 push(@{$entity->{dependencies}}, @included_entities);
62 push(@{$_->{references}}, $entity) for (@included_entities);
63 }
64 for (@entities) {
65 $_->{full_dependencies} = [$self->expand_entity($_, 'dependencies')];
66 $_->{full_references} = [$self->expand_entity($_, 'references')];
67 }
68
69 return @entities;
70 }
71  
72 # If an entity is changed/added, then this should be called on the entity.
73 sub add_refresh_entity {
74 my ($self, $entity, @entities) = @_;
75 my %entity_list = map { $_->id => $_ } @entities;
76 if (!$entity_list{$entity->id}) {
77 my @included_literals = $self->retrieve_includes_ast($entity->ast);
78 $entity_list{$_} = WWW::Shopify::Liquid::Analyzer::Entity->new({ id => $_ }) for (grep { !$entity_list{$_} } @included_literals);
79 my @included_entities = map { $entity_list{$_} } @included_literals;
80 push(@{$entity->{dependencies}}, @included_entities);
81 push(@{$_->{references}}, $entity) for (@included_entities);
82 $entity->{full_dependencies} = [$self->expand_entity($entity, 'dependencies')];
83 $entity->{full_references} = [$self->expand_entity($entity, 'references')];
84 }
85 return $entity;
86 }
87  
88 sub add_refresh_path {
89 my ($self, $path, @entities) = @_;
90 return $self->add_refresh_entity($self->generate_entity_file($path), @entities);
91 }
92  
93 sub generate_entity_file {
94 my ($self, $path) = @_;
95 return WWW::Shopify::Liquid::Analyzer::Entity->new({
96 id => do { my $handle = $path; $handle =~ s/^.*\/([^\/]+?)(\.liquid)?$/$1/; $handle },
97 ast => $self->liquid->parse_file($path),
98 file => $path
99 })
100 }
101  
102 sub generate_entities_files {
103 my ($self, @paths) = @_;
104 return map {
105 $self->generate_entity_file($_);
106 } @paths;
107 }
108  
109 1;