mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Move date coercion to #to_date
This commit is contained in:
@@ -163,7 +163,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Reformat a date using Ruby's core Time#strftime( string ) -> string
|
# Reformat a date using Ruby's core Time#strftime( string ) -> string
|
||||||
#
|
#
|
||||||
# %a - The abbreviated weekday name (``Sun'')
|
# %a - The abbreviated weekday name (``Sun'')
|
||||||
# %A - The full weekday name (``Sunday'')
|
# %A - The full weekday name (``Sunday'')
|
||||||
# %b - The abbreviated month name (``Jan'')
|
# %b - The abbreviated month name (``Jan'')
|
||||||
@@ -194,33 +194,11 @@ module Liquid
|
|||||||
#
|
#
|
||||||
# See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
|
# See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
|
||||||
def date(input, format)
|
def date(input, format)
|
||||||
|
return input if format.to_s.empty?
|
||||||
|
|
||||||
if format.to_s.empty?
|
return input unless date = to_date(input)
|
||||||
return input.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
if ((input.is_a?(String) && !/\A\d+\z/.match(input.to_s).nil?) || input.is_a?(Integer)) && input.to_i > 0
|
date.strftime(format.to_s)
|
||||||
input = Time.at(input.to_i)
|
|
||||||
end
|
|
||||||
|
|
||||||
date = if input.is_a?(String)
|
|
||||||
case input.downcase
|
|
||||||
when 'now'.freeze, 'today'.freeze
|
|
||||||
Time.now
|
|
||||||
else
|
|
||||||
Time.parse(input)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
input
|
|
||||||
end
|
|
||||||
|
|
||||||
if date.respond_to?(:strftime)
|
|
||||||
date.strftime(format.to_s)
|
|
||||||
else
|
|
||||||
input
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
input
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the first element of the passed in array
|
# Get the first element of the passed in array
|
||||||
@@ -296,6 +274,23 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_date(obj)
|
||||||
|
return obj if obj.respond_to?(:strftime)
|
||||||
|
|
||||||
|
case obj
|
||||||
|
when 'now'.freeze, 'today'.freeze
|
||||||
|
Time.now
|
||||||
|
when /\A\d+\z/, Integer
|
||||||
|
Time.at(obj.to_i)
|
||||||
|
when String
|
||||||
|
Time.parse(obj)
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
rescue ArgumentError
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
def apply_operation(input, operand, operation)
|
def apply_operation(input, operand, operation)
|
||||||
result = to_number(input).send(operation, to_number(operand))
|
result = to_number(input).send(operation, to_number(operand))
|
||||||
result.is_a?(BigDecimal) ? result.to_f : result
|
result.is_a?(BigDecimal) ? result.to_f : result
|
||||||
|
|||||||
@@ -186,7 +186,6 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
|
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_first_last
|
def test_first_last
|
||||||
assert_equal 1, @filters.first([1,2,3])
|
assert_equal 1, @filters.first([1,2,3])
|
||||||
assert_equal 3, @filters.last([1,2,3])
|
assert_equal 3, @filters.last([1,2,3])
|
||||||
|
|||||||
Reference in New Issue
Block a user