mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 01:05:40 +03:00
Colocate Traversal classes with classes they traverse
This puts all knowledge of the traversal in the same file, and removes the need for a CASES registry.
This commit is contained in:
@@ -45,6 +45,7 @@ module Liquid
|
||||
end
|
||||
|
||||
require "liquid/version"
|
||||
require 'liquid/traversal'
|
||||
require 'liquid/lexer'
|
||||
require 'liquid/parser'
|
||||
require 'liquid/i18n'
|
||||
|
||||
@@ -128,6 +128,15 @@ module Liquid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
[
|
||||
@node.left, @node.right,
|
||||
@node.child_condition, @node.attachment
|
||||
].compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ElseCondition < Condition
|
||||
|
||||
@@ -47,6 +47,12 @@ module Liquid
|
||||
1
|
||||
end
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
[@node.from]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('assign'.freeze, Assign)
|
||||
|
||||
@@ -82,6 +82,12 @@ module Liquid
|
||||
block.attach(BlockBody.new)
|
||||
@blocks << block
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
[@node.left] + @node.blocks
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('case'.freeze, Case)
|
||||
|
||||
@@ -53,6 +53,12 @@ module Liquid
|
||||
$1 ? Expression.parse($1) : nil
|
||||
end.compact
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
Array(@node.variables)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('cycle', Cycle)
|
||||
|
||||
@@ -191,6 +191,12 @@ module Liquid
|
||||
def render_else(context)
|
||||
@else_block ? @else_block.render(context) : ''.freeze
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
(super + [@node.limit, @node.from, @node.collection_name]).compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('for'.freeze, For)
|
||||
|
||||
@@ -110,6 +110,12 @@ module Liquid
|
||||
Condition.new(a)
|
||||
end
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
@node.blocks
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('if'.freeze, If)
|
||||
|
||||
@@ -109,6 +109,15 @@ module Liquid
|
||||
|
||||
file_system.read_template_file(context.evaluate(@template_name_expr))
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
[
|
||||
@node.template_name_expr,
|
||||
@node.variable_name_expr
|
||||
] + @node.attributes.values
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('include'.freeze, Include)
|
||||
|
||||
@@ -50,6 +50,12 @@ module Liquid
|
||||
result << "</tr>\n"
|
||||
result
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
super + @node.attributes.values + [@node.collection_name]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('tablerow'.freeze, TableRow)
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
module Liquid
|
||||
class Traversal
|
||||
def self.for(node, callbacks = Hash.new(proc {}))
|
||||
kase = CASES.find { |(klass, _)| node.is_a?(klass) }&.last
|
||||
(kase || self).new(node, callbacks)
|
||||
if defined?(node.class::Traversal)
|
||||
node.class::Traversal
|
||||
else
|
||||
self
|
||||
end.new(node, callbacks)
|
||||
end
|
||||
|
||||
def initialize(node, callbacks)
|
||||
@@ -35,84 +38,5 @@ module Liquid
|
||||
def children
|
||||
@node.respond_to?(:nodelist) ? Array(@node.nodelist) : []
|
||||
end
|
||||
|
||||
class Assign < Traversal
|
||||
def children
|
||||
[@node.from]
|
||||
end
|
||||
end
|
||||
|
||||
class Case < Traversal
|
||||
def children
|
||||
[@node.left] + @node.blocks
|
||||
end
|
||||
end
|
||||
|
||||
class Condition < Traversal
|
||||
def children
|
||||
[
|
||||
@node.left, @node.right,
|
||||
@node.child_condition, @node.attachment
|
||||
].compact
|
||||
end
|
||||
end
|
||||
|
||||
class Cycle < Traversal
|
||||
def children
|
||||
Array(@node.variables)
|
||||
end
|
||||
end
|
||||
|
||||
class For < Traversal
|
||||
def children
|
||||
(super + [@node.limit, @node.from, @node.collection_name]).compact
|
||||
end
|
||||
end
|
||||
|
||||
class If < Traversal
|
||||
def children
|
||||
@node.blocks
|
||||
end
|
||||
end
|
||||
|
||||
class Include < Traversal
|
||||
def children
|
||||
[
|
||||
@node.template_name_expr,
|
||||
@node.variable_name_expr
|
||||
] + @node.attributes.values
|
||||
end
|
||||
end
|
||||
|
||||
class TableRow < Traversal
|
||||
def children
|
||||
super + @node.attributes.values + [@node.collection_name]
|
||||
end
|
||||
end
|
||||
|
||||
class Variable < Traversal
|
||||
def children
|
||||
[@node.name] + @node.filters.flatten
|
||||
end
|
||||
end
|
||||
|
||||
class VariableLookup < Traversal
|
||||
def children
|
||||
@node.lookups
|
||||
end
|
||||
end
|
||||
|
||||
CASES = {
|
||||
Liquid::Assign => Assign,
|
||||
Liquid::Case => Case,
|
||||
Liquid::Condition => Condition,
|
||||
Liquid::Cycle => Cycle,
|
||||
Liquid::For => For,
|
||||
Liquid::If => If,
|
||||
Liquid::Include => Include,
|
||||
Liquid::TableRow => TableRow,
|
||||
Liquid::Variable => Variable,
|
||||
Liquid::VariableLookup => VariableLookup
|
||||
}.freeze
|
||||
end
|
||||
end
|
||||
|
||||
@@ -138,5 +138,11 @@ module Liquid
|
||||
raise error
|
||||
end
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
[@node.name] + @node.filters.flatten
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -78,5 +78,11 @@ module Liquid
|
||||
def state
|
||||
[@name, @lookups, @command_flags]
|
||||
end
|
||||
|
||||
class Traversal < Liquid::Traversal
|
||||
def children
|
||||
@node.lookups
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
require 'liquid/traversal'
|
||||
|
||||
class TraversalTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
Reference in New Issue
Block a user