Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/liquid/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,18 @@ def freeze
# @strainer_template.freeze
super
end

def marshal_dump
filter_modules = @strainer_template.ancestors[1...@strainer_template.ancestors.index(StrainerTemplate)]
[@tags, @error_mode, @file_system, @default_resource_limits, filter_modules]
end

def marshal_load(data)
@tags, @error_mode, @file_system, @default_resource_limits, filter_modules = data
@exception_renderer = ->(exception) { exception }
@strainer_template = Class.new(StrainerTemplate)
filter_modules.reverse_each { |m| @strainer_template.add_filter(m) }
@strainer_template_class_cache = {}
end
end
end
9 changes: 9 additions & 0 deletions lib/liquid/parse_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,14 @@ def partial_options
end
end
end

def marshal_dump
instance_variables.reject { |v| v == :@string_scanner }.map { |v| [v, instance_variable_get(v)] }.to_h
end

def marshal_load(data)
data.each { |k, v| instance_variable_set(k, v) }
@string_scanner = StringScanner.new("")
end
end
end
22 changes: 22 additions & 0 deletions test/unit/template_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,26 @@ def test_invalid_utf8
error.message,
)
end

module UpFilter
def up(input) = input.upcase
end

def test_marshal_roundtrip
env = Liquid::Environment.build { |e| e.register_filter(UpFilter) }
t = Template.parse("Hello {{ name | up }}", environment: env)
t2 = Marshal.load(Marshal.dump(t))
assert_equal("Hello WORLD", t2.render("name" => "world"))
end

module ShoutFilter
def shout(input) = input.upcase + "!"
end

def test_marshal_roundtrip_with_custom_filter
env = Liquid::Environment.build { |e| e.register_filter(ShoutFilter) }
t = Template.parse("{{ name | shout }}", environment: env)
t2 = Marshal.load(Marshal.dump(t))
assert_equal("WORLD!", t2.render("name" => "world"))
end
end
Loading