Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for custom < Hash classes to override #to_s #1896

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.6
3.4.1
46 changes: 28 additions & 18 deletions lib/liquid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,38 @@ def self.to_liquid_value(obj)
obj
end

if RUBY_VERSION >= '3.4'
def self.to_s(obj, seen = {})
case obj
when Hash
def self.to_s(obj, seen = {})
case obj
when Hash
# If the custom hash implementation overrides `#to_s`, use their
# custom implementation. Otherwise we use Liquid's default
# implementation.
if obj.class.instance_method(:to_s) == HASH_TO_S_METHOD
hash_inspect(obj, seen)
when Array
array_inspect(obj, seen)
else
obj.to_s
end
when Array
array_inspect(obj, seen)
else
obj.to_s
end
end

def self.inspect(obj, seen = {})
case obj
when Hash
def self.inspect(obj, seen = {})
case obj
when Hash
# If the custom hash implementation overrides `#inspect`, use their
# custom implementation. Otherwise we use Liquid's default
# implementation.
if obj.class.instance_method(:inspect) == HASH_INSPECT_METHOD
hash_inspect(obj, seen)
when Array
array_inspect(obj, seen)
else
obj.inspect
end
end
else
def self.to_s(obj, seen = nil)
obj.to_s
end

def self.inspect(obj, seen = nil)
when Array
array_inspect(obj, seen)
else
obj.inspect
end
end
Expand Down Expand Up @@ -175,5 +179,11 @@ def self.hash_inspect(hash, seen = {})
ensure
seen.delete(hash.object_id)
end

HASH_TO_S_METHOD = Hash.instance_method(:to_s)
private_constant :HASH_TO_S_METHOD

HASH_INSPECT_METHOD = Hash.instance_method(:inspect)
private_constant :HASH_INSPECT_METHOD
end
end
17 changes: 17 additions & 0 deletions test/integration/hash_rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,21 @@ def test_render_hash_with_array_values_hash
def test_render_hash_with_hash_key
assert_template_result("{{\"foo\"=>\"bar\"}=>42}", "{{ my_hash }}", { "my_hash" => { Hash["foo" => "bar"] => 42 } })
end

def test_rendering_hash_with_custom_to_s_method_uses_custom_to_s
my_hash = Class.new(Hash) do
def to_s
"kewl"
end
end.new

assert_template_result("kewl", "{{ my_hash }}", { "my_hash" => my_hash })
end

def test_rendering_hash_without_custom_to_s_uses_default_inspect
my_hash = Class.new(Hash).new
my_hash[:foo] = :bar

assert_template_result("{:foo=>:bar}", "{{ my_hash }}", { "my_hash" => my_hash })
end
end
Loading