Allow Utils.to_number to work with anything that responds to #to_number

This commit is contained in:
Ismael Celis
2016-03-30 01:57:21 -03:00
parent 021bafd260
commit d03c4ae8e8
2 changed files with 17 additions and 1 deletions

View File

@@ -52,7 +52,11 @@ module Liquid
when String
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
else
0
if obj.respond_to?(:to_number)
obj.to_number
else
0
end
end
end

View File

@@ -41,6 +41,16 @@ class TestEnumerable < Liquid::Drop
end
end
class NumberLikeThing < Liquid::Drop
def initialize(amount)
@amount = amount
end
def to_number
@amount
end
end
class StandardFiltersTest < Minitest::Test
include Liquid
@@ -391,6 +401,8 @@ class StandardFiltersTest < Minitest::Test
assert_raises(Liquid::ZeroDivisionError) do
assert_template_result "4", "{{ 1 | modulo: 0 }}"
end
assert_template_result "5", "{{ price | divided_by:2 }}", 'price' => NumberLikeThing.new(10)
end
def test_modulo