Store filter names as symbols internally

This commit is contained in:
Dylan Thacker-Smith
2022-08-05 15:22:19 -04:00
parent f1846d63a3
commit 3af10eb58c
3 changed files with 18 additions and 6 deletions
+6 -2
View File
@@ -24,11 +24,11 @@ module Liquid
include(filter) include(filter)
filter_methods.merge(filter.public_instance_methods.map(&:to_s)) filter_methods.merge(filter.public_instance_methods)
end end
def invokable?(method) def invokable?(method)
filter_methods.include?(method.to_s) filter_methods.include?(method.to_sym)
end end
def inherited(subclass) def inherited(subclass)
@@ -36,6 +36,9 @@ module Liquid
subclass.instance_variable_set(:@filter_methods, @filter_methods.dup) subclass.instance_variable_set(:@filter_methods, @filter_methods.dup)
end end
# Assuming the filter name is a string is deprecated, explicitly
# cast to_s or to_sym for compatibility with liquid 6, where it is
# planned to return as a symbol.
def filter_method_names def filter_method_names
filter_methods.map(&:to_s).to_a filter_methods.map(&:to_s).to_a
end end
@@ -48,6 +51,7 @@ module Liquid
end end
def invoke(method, *args) def invoke(method, *args)
method = method.to_sym
if self.class.invokable?(method) if self.class.invokable?(method)
send(method, *args) send(method, *args)
elsif @context.strict_filters elsif @context.strict_filters
+11 -3
View File
@@ -18,7 +18,7 @@ module Liquid
JustTagAttributes = /\A#{TagAttributes}\z/o JustTagAttributes = /\A#{TagAttributes}\z/o
MarkupWithQuotedFragment = /(#{QuotedFragment})(.*)/om MarkupWithQuotedFragment = /(#{QuotedFragment})(.*)/om
attr_accessor :filters, :name, :line_number attr_accessor :name, :line_number
attr_reader :parse_context attr_reader :parse_context
alias_method :options, :parse_context alias_method :options, :parse_context
@@ -52,7 +52,7 @@ module Liquid
filters = Regexp.last_match(1).scan(FilterParser) filters = Regexp.last_match(1).scan(FilterParser)
filters.each do |f| filters.each do |f|
next unless f =~ /\w+/ next unless f =~ /\w+/
filtername = Regexp.last_match(0) filtername = Regexp.last_match(0).to_sym
filterargs = f.scan(FilterArgsRegex).flatten filterargs = f.scan(FilterArgsRegex).flatten
@filters << parse_filter_expressions(filtername, filterargs) @filters << parse_filter_expressions(filtername, filterargs)
end end
@@ -67,13 +67,21 @@ module Liquid
@name = parse_context.parse_expression(p.expression) @name = parse_context.parse_expression(p.expression)
while p.consume?(:pipe) while p.consume?(:pipe)
filtername = p.consume(:id) filtername = p.consume(:id).to_sym
filterargs = p.consume?(:colon) ? parse_filterargs(p) : [] filterargs = p.consume?(:colon) ? parse_filterargs(p) : []
@filters << parse_filter_expressions(filtername, filterargs) @filters << parse_filter_expressions(filtername, filterargs)
end end
p.consume(:end_of_string) p.consume(:end_of_string)
end end
# Assuming the filter name is a string is deprecated, explicitly
# cast to_s or to_sym for compatibility with liquid 6, where it is
# planned to return as a symbol.
def filters
# Remove to_s in liquid 6.0
@filters.map { |key, *args| [key.to_s, *args].freeze }.freeze
end
def parse_filterargs(p) def parse_filterargs(p)
# first argument # first argument
filterargs = [p.argument] filterargs = [p.argument]
+1 -1
View File
@@ -60,7 +60,7 @@ class StrainerTemplateUnitTest < Minitest::Test
with_global_filter do with_global_filter do
strainer = Context.new.strainer strainer = Context.new.strainer
strainer.class.add_filter(PublicMethodOverrideFilter) strainer.class.add_filter(PublicMethodOverrideFilter)
assert(strainer.class.send(:filter_methods).include?('public_filter')) assert(strainer.class.filter_method_names.include?('public_filter'))
end end
end end