Skip to content
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
5 changes: 4 additions & 1 deletion lib/holidays/definition/repository/proc_result_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def validate!(function, function_arguments)
end

def build_proc_key(function, function_arguments)
Digest::MD5.hexdigest("#{function.to_s}_#{function_arguments.join('_')}")
args_string = function_arguments.map { |arg|
arg.is_a?(Date) ? arg.iso8601 : arg.to_s
}.join("_")
Digest::MD5.hexdigest("#{function.to_s}_#{args_string}")
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/holidays/definition/repository/test_proc_result_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ def test_accepts_mix_of_integers_and_dates_for_multiple_function_arguments
assert_equal(Date.civil(2016, 1, 6), @subject.lookup(function, date, modifier))
end

def test_build_proc_key_uses_iso8601_for_date_arguments
# This test ensures Date arguments are formatted using iso8601 rather than
# implicit to_s, which avoids Ruby 3.1+ deprecation warnings about
# Date#to_s without a format argument.
function = lambda { |date| date + 1 }
date = Date.civil(2016, 1, 15)

# Call lookup twice with the same date - should hit the cache
result1 = @subject.lookup(function, date)
result2 = @subject.lookup(function, date)

assert_equal(result1, result2)
assert_equal(Date.civil(2016, 1, 16), result1)
end

def test_lookup_raises_error_if_function_argument_is_not_valid
function = lambda { |year| Date.civil(year, 2, 1) - 1 }
function_argument = "2015"
Expand Down