mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Context as accessor
This commit is contained in:
@@ -32,12 +32,6 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(context)
|
attr_accessor :context
|
||||||
@context = context
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :context
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,25 +23,30 @@ module Liquid
|
|||||||
raise(ArgumentError, "wrong argument type Proc (expected Liquid::Filter)")
|
raise(ArgumentError, "wrong argument type Proc (expected Liquid::Filter)")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
instance = filter.new
|
||||||
|
|
||||||
filter.invokable_methods.each do |method|
|
filter.invokable_methods.each do |method|
|
||||||
filter_map[method] = filter
|
filter_instances[method] = instance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_filter(method)
|
def invokable?(method)
|
||||||
filter_map.fetch(method)
|
filter_instances.key?(method)
|
||||||
end
|
end
|
||||||
|
|
||||||
def invokable?(method)
|
def invoke(method, context, *args)
|
||||||
filter_map.key?(method)
|
instance = filter_instances.fetch(method)
|
||||||
|
instance.context = context
|
||||||
|
instance.public_send(method, *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def filter_map
|
def filter_instances
|
||||||
@filter_map ||= {}
|
@filter_instances ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Caching here is most likely not required anymore since we cache instances and there is only one instance per filter class.
|
||||||
def convert_mod_to_filter(mod)
|
def convert_mod_to_filter(mod)
|
||||||
@filter_classes ||= {}
|
@filter_classes ||= {}
|
||||||
@filter_classes[mod] ||= begin
|
@filter_classes[mod] ||= begin
|
||||||
@@ -58,10 +63,7 @@ module Liquid
|
|||||||
|
|
||||||
def invoke(method, *args)
|
def invoke(method, *args)
|
||||||
if self.class.invokable?(method)
|
if self.class.invokable?(method)
|
||||||
klass = self.class.fetch_filter(method)
|
self.class.invoke(method, @context, *args)
|
||||||
|
|
||||||
instance = klass.new(@context)
|
|
||||||
instance.public_send(method, *args)
|
|
||||||
elsif @context.strict_filters
|
elsif @context.strict_filters
|
||||||
raise(Liquid::UndefinedFilter, "undefined filter #{method}")
|
raise(Liquid::UndefinedFilter, "undefined filter #{method}")
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user