mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 02:40:41 -07:00
Allow Utils.to_number to work with anything that responds to #to_number
This commit is contained in:
+5
-1
@@ -52,7 +52,11 @@ module Liquid
|
|||||||
when String
|
when String
|
||||||
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
|
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
|
||||||
else
|
else
|
||||||
0
|
if obj.respond_to?(:to_number)
|
||||||
|
obj.to_number
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,16 @@ class TestEnumerable < Liquid::Drop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class NumberLikeThing < Liquid::Drop
|
||||||
|
def initialize(amount)
|
||||||
|
@amount = amount
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_number
|
||||||
|
@amount
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class StandardFiltersTest < Minitest::Test
|
class StandardFiltersTest < Minitest::Test
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
@@ -391,6 +401,8 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_raises(Liquid::ZeroDivisionError) do
|
assert_raises(Liquid::ZeroDivisionError) do
|
||||||
assert_template_result "4", "{{ 1 | modulo: 0 }}"
|
assert_template_result "4", "{{ 1 | modulo: 0 }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assert_template_result "5", "{{ price | divided_by:2 }}", 'price' => NumberLikeThing.new(10)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_modulo
|
def test_modulo
|
||||||
|
|||||||
Reference in New Issue
Block a user