Merge branch 'class_cache' of github.com:wildfireapp/liquid into wildfireapp-class_cache

This commit is contained in:
Florian Weingarten
2013-11-25 10:46:18 -05:00
3 changed files with 36 additions and 7 deletions

View File

@@ -25,6 +25,7 @@ module Liquid
squash_instance_assigns_with_environments
@interrupts = []
@filters = []
end
def resource_limits_reached?
@@ -34,7 +35,7 @@ module Liquid
end
def strainer
@strainer ||= Strainer.create(self)
@strainer ||= Strainer.create(self, @filters)
end
# Adds filters to this context.
@@ -43,11 +44,20 @@ module Liquid
# for that
def add_filters(filters)
filters = [filters].flatten.compact
filters.each do |f|
raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
Strainer.add_known_filter(f)
strainer.extend(f)
end
# If strainer is already setup then there's no choice but to use a runtime
# extend call. If strainer is not yet created, we can utilize strainers
# cached class based API, which avoids busting the method cache.
if @strainer
filters.each do |f|
strainer.extend(f)
end
else
@filters.concat filters
end
end

View File

@@ -11,6 +11,11 @@ module Liquid
@@filters = []
@@known_filters = Set.new
@@known_methods = Set.new
@@strainer_class_cache = Hash.new do |hash, filters|
hash[filters] = Class.new(Strainer) do
filters.each { |f| include f }
end
end
def initialize(context)
@context = context
@@ -32,10 +37,13 @@ module Liquid
end
end
def self.create(context)
strainer = Strainer.new(context)
@@filters.each { |m| strainer.extend(m) }
strainer
def self.strainer_class_cache
@@strainer_class_cache
end
def self.create(context, filters = nil)
filters = @@filters + (filters || [])
strainer_class_cache[filters].new(context)
end
def invoke(method, *args)