diff --git a/lib/liquid/variable_lookup.rb b/lib/liquid/variable_lookup.rb index 4fba2a65..4ccd277f 100644 --- a/lib/liquid/variable_lookup.rb +++ b/lib/liquid/variable_lookup.rb @@ -7,10 +7,6 @@ module Liquid attr_reader :name, :lookups def self.parse(markup, string_scanner = StringScanner.new(""), cache = nil) - new(markup, string_scanner, cache) - end - - def initialize(markup, string_scanner = StringScanner.new(""), cache = nil) lookups = markup.scan(VariableParser) name = lookups.shift @@ -21,12 +17,10 @@ module Liquid cache, ) end - @name = name - @lookups = lookups - @command_flags = 0 + command_flags = 0 - @lookups.each_index do |i| + lookups.each_index do |i| lookup = lookups[i] if lookup&.start_with?('[') && lookup&.end_with?(']') lookups[i] = Expression.parse( @@ -35,9 +29,17 @@ module Liquid cache, ) elsif COMMAND_METHODS.include?(lookup) - @command_flags |= 1 << i + command_flags |= 1 << i end end + + new(name, lookups, command_flags) + end + + def initialize(name, lookups, command_flags) + @name = name + @lookups = lookups + @command_flags = command_flags end def lookup_command?(lookup_index) diff --git a/test/unit/condition_unit_test.rb b/test/unit/condition_unit_test.rb index e9add4c8..08cb4686 100644 --- a/test/unit/condition_unit_test.rb +++ b/test/unit/condition_unit_test.rb @@ -82,7 +82,7 @@ class ConditionUnitTest < Minitest::Test def test_contains_works_on_arrays @context = Liquid::Context.new @context['array'] = [1, 2, 3, 4, 5] - array_expr = VariableLookup.new("array") + array_expr = VariableLookup.parse("array") assert_evaluates_false(array_expr, 'contains', 0) assert_evaluates_true(array_expr, 'contains', 1) @@ -96,8 +96,8 @@ class ConditionUnitTest < Minitest::Test def test_contains_returns_false_for_nil_operands @context = Liquid::Context.new - assert_evaluates_false(VariableLookup.new('not_assigned'), 'contains', '0') - assert_evaluates_false(0, 'contains', VariableLookup.new('not_assigned')) + assert_evaluates_false(VariableLookup.parse('not_assigned'), 'contains', '0') + assert_evaluates_false(0, 'contains', VariableLookup.parse('not_assigned')) end def test_contains_return_false_on_wrong_data_type @@ -149,7 +149,7 @@ class ConditionUnitTest < Minitest::Test @context = Liquid::Context.new @context['one'] = @context['another'] = "gnomeslab-and-or-liquid" - assert_evaluates_true(VariableLookup.new("one"), '==', VariableLookup.new("another")) + assert_evaluates_true(VariableLookup.parse("one"), '==', VariableLookup.parse("another")) end def test_default_context_is_deprecated diff --git a/test/unit/variable_unit_test.rb b/test/unit/variable_unit_test.rb index 6379c6ec..b6f1c9aa 100644 --- a/test/unit/variable_unit_test.rb +++ b/test/unit/variable_unit_test.rb @@ -7,20 +7,20 @@ class VariableUnitTest < Minitest::Test def test_variable var = create_variable('hello') - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) end def test_filters var = create_variable('hello | textileze') - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['textileze', []]], var.filters) var = create_variable('hello | textileze | paragraph') - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['textileze', []], ['paragraph', []]], var.filters) var = create_variable(%( hello | strftime: '%Y')) - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['strftime', ['%Y']]], var.filters) var = create_variable(%( 'typo' | link_to: 'Typo', true )) @@ -44,11 +44,11 @@ class VariableUnitTest < Minitest::Test assert_equal([['repeat', [3, 3, 3]]], var.filters) var = create_variable(%( hello | strftime: '%Y, okay?')) - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['strftime', ['%Y, okay?']]], var.filters) var = create_variable(%( hello | things: "%Y, okay?", 'the other one')) - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['things', ['%Y, okay?', 'the other one']]], var.filters) end @@ -60,15 +60,15 @@ class VariableUnitTest < Minitest::Test def test_filters_without_whitespace var = create_variable('hello | textileze | paragraph') - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['textileze', []], ['paragraph', []]], var.filters) var = create_variable('hello|textileze|paragraph') - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['textileze', []], ['paragraph', []]], var.filters) var = create_variable("hello|replace:'foo','bar'|textileze") - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['replace', ['foo', 'bar']], ['textileze', []]], var.filters) end @@ -99,8 +99,8 @@ class VariableUnitTest < Minitest::Test end def test_dashes - assert_equal(VariableLookup.new('foo-bar'), create_variable('foo-bar').name) - assert_equal(VariableLookup.new('foo-bar-2'), create_variable('foo-bar-2').name) + assert_equal(VariableLookup.parse('foo-bar'), create_variable('foo-bar').name) + assert_equal(VariableLookup.parse('foo-bar-2'), create_variable('foo-bar-2').name) assert_raises(Liquid::SyntaxError) { create_variable('foo - bar') } assert_raises(Liquid::SyntaxError) { create_variable('-foo') } @@ -114,12 +114,12 @@ class VariableUnitTest < Minitest::Test def test_string_dot var = create_variable(%( test.test )) - assert_equal(VariableLookup.new('test.test'), var.name) + assert_equal(VariableLookup.parse('test.test'), var.name) end def test_filter_with_keyword_arguments var = create_variable(%( hello | things: greeting: "world", farewell: 'goodbye')) - assert_equal(VariableLookup.new('hello'), var.name) + assert_equal(VariableLookup.parse('hello'), var.name) assert_equal([['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters) end @@ -163,7 +163,7 @@ class VariableUnitTest < Minitest::Test end def test_variable_lookup_interface - lookup = VariableLookup.new('a.b.c') + lookup = VariableLookup.parse('a.b.c') assert_equal('a', lookup.name) assert_equal(['b', 'c'], lookup.lookups) end