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

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 137 CC 7 SCAR 15.5
CC
7
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 sub file { return $_[0]->{file}; }
29  
30 package WWW::Shopify::Liquid::Analyzer;
31 sub new {
32 my ($package) = shift;
33 my $self = bless {
34 @_
35 }, $package;
36 $self->{liquid} = WWW::Shopify::Liquid->new unless $self->liquid;
37 return $self;
38 }
39 sub liquid { return $_[0]->{liquid}; }
40  
41 sub retrieve_includes_ast {
42 my ($self, $ast) = @_;
43 return () unless $ast;
44 return grep { defined $_ && $_->isa('WWW::Shopify::Liquid::Tag::Include') } $ast->tokens;
45 }
46  
47 sub expand_entity {
48 my ($self, $entity, $type, $used_list) = @_;
49 $used_list = { $entity->id => 1 } unless $used_list;
50 return map { $used_list->{$_->id} ? () : ($_, $self->expand_entity($_, $type, $used_list)) } @{$entity->{$type}};
51 }
52  
53 sub generate_include_entity {
54 my ($self, $include) = @_;
55 my ($path) = $include->retrieve_include({}, "render", $self->liquid->renderer, $include->include_literal);
56 my $entity = $self->generate_entity_file($path);
57 return $entity;
58
59 }
60  
61 use List::MoreUtils qw(uniq);
62 sub populate_dependencies {
63 my ($self, @entities) = @_;
64 my %entity_list = map { $_->id => $_ } @entities;
65 # Go through, and flatten out those lists.
66 for my $entity (grep { $_->ast } @entities) {
67 my @included_literals = $self->retrieve_includes_ast($entity->ast);
68 $entity_list{$_->include_literal} = $self->generate_include_entity($_) for (grep { !$entity_list{$_->include_literal} } @included_literals);
69 my @included_entities = map { $entity_list{$_->include_literal} } @included_literals;
70 push(@{$entity->{dependencies}}, @included_entities);
71 push(@{$_->{references}}, $entity) for (@included_entities);
72 }
73 for (@entities) {
74 $_->{full_dependencies} = [$self->expand_entity($_, 'dependencies')];
75 $_->{full_references} = [$self->expand_entity($_, 'references')];
76 }
77
78 return @entities;
79 }
80  
81 # If an entity is changed/added, then this should be called on the entity.
82 sub add_refresh_entity {
83 my ($self, $entity, @entities) = @_;
84 my %entity_list = map { $_->id => $_ } @entities;
85 if (!$entity_list{$entity->id}) {
86 my @included_literals = $self->retrieve_includes_ast($entity->ast);
87 $entity_list{$_->include_literal} = $self->generate_include_entity($_) for (grep { !$entity_list{$_->include_literal} } @included_literals);
88 my @included_entities = map { $entity_list{$_->include_literal} } @included_literals;
89 push(@{$entity->{dependencies}}, @included_entities);
90 push(@{$_->{references}}, $entity) for (@included_entities);
91 $entity->{full_dependencies} = [$self->expand_entity($entity, 'dependencies')];
92 $entity->{full_references} = [$self->expand_entity($entity, 'references')];
93 }
94 return $entity;
95 }
96  
97 sub add_refresh_path {
98 my ($self, $path, @entities) = @_;
99 return $self->add_refresh_entity($self->generate_entity_file($path), @entities);
100 }
101  
102 sub generate_entity_file {
103 my ($self, $path) = @_;
104 return WWW::Shopify::Liquid::Analyzer::Entity->new({
105 id => do { my $handle = $path; $handle =~ s/^.*\/([^\/]+?)(\.liquid)?$/$1/; $handle },
106 ast => $self->liquid->parse_file($path),
107 file => $path
108 })
109 }
110  
111 sub generate_entities_files {
112 my ($self, @paths) = @_;
113 return map {
114 $self->generate_entity_file($_);
115 } @paths;
116 }
117  
118 1;