mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
Context as accessor
This commit is contained in:
@@ -32,12 +32,6 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(context)
|
||||
@context = context
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :context
|
||||
attr_accessor :context
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,25 +23,30 @@ module Liquid
|
||||
raise(ArgumentError, "wrong argument type Proc (expected Liquid::Filter)")
|
||||
end
|
||||
|
||||
instance = filter.new
|
||||
|
||||
filter.invokable_methods.each do |method|
|
||||
filter_map[method] = filter
|
||||
filter_instances[method] = instance
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_filter(method)
|
||||
filter_map.fetch(method)
|
||||
def invokable?(method)
|
||||
filter_instances.key?(method)
|
||||
end
|
||||
|
||||
def invokable?(method)
|
||||
filter_map.key?(method)
|
||||
def invoke(method, context, *args)
|
||||
instance = filter_instances.fetch(method)
|
||||
instance.context = context
|
||||
instance.public_send(method, *args)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def filter_map
|
||||
@filter_map ||= {}
|
||||
def filter_instances
|
||||
@filter_instances ||= {}
|
||||
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)
|
||||
@filter_classes ||= {}
|
||||
@filter_classes[mod] ||= begin
|
||||
@@ -58,10 +63,7 @@ module Liquid
|
||||
|
||||
def invoke(method, *args)
|
||||
if self.class.invokable?(method)
|
||||
klass = self.class.fetch_filter(method)
|
||||
|
||||
instance = klass.new(@context)
|
||||
instance.public_send(method, *args)
|
||||
self.class.invoke(method, @context, *args)
|
||||
elsif @context.strict_filters
|
||||
raise(Liquid::UndefinedFilter, "undefined filter #{method}")
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user